2017-08-22 18 views
1

ASP.NET Boilerplateエンジン内でサービスを開発していて、その対象からエラーが発生しています。ドキュメントの示唆しているように、ApplicationServiceを継承しているので、エラーの性質は明確ではありません。コード:エラー「使用する前にUnitOfWorkManagerを設定する必要があります」

namespace MyAbilities.Api.Blob 
{ 
    public class BlobService : ApplicationService, IBlobService 
    { 
     public readonly IRepository<UserMedia, int> _blobRepository; 

     public BlobService(IRepository<UserMedia, int> blobRepository) 
     { 
      _blobRepository = blobRepository; 
     } 

     public async Task<List<BlobDto>> UploadBlobs(HttpContent httpContent) 
     { 
      var blobUploadProvider = new BlobStorageUploadProvider(); 

      var list = await httpContent.ReadAsMultipartAsync(blobUploadProvider) 
       .ContinueWith(task => 
       { 
        if (task.IsFaulted || task.IsCanceled) 
        { 
         if (task.Exception != null) throw task.Exception; 
        } 

        var provider = task.Result; 
        return provider.Uploads.ToList(); 
       }); 

      // store blob info in the database 

      foreach (var blobDto in list) 
      { 
       SaveBlobData(blobDto); 
      } 

      return list; 
     } 

     public void SaveBlobData(BlobDto blobData) 
     { 
      UserMedia um = blobData.MapTo<UserMedia>(); 
      _blobRepository.InsertOrUpdateAndGetId(um); 
      CurrentUnitOfWork.SaveChanges(); 
     } 

     public async Task<BlobDto> DownloadBlob(int blobId) 
     { 
      // TODO: Implement this helper method. It should retrieve blob info 
      // from the database, based on the blobId. The record should contain the 
      // blobName, which should be returned as the result of this helper method. 
      var blobName = GetBlobName(blobId); 

      if (!String.IsNullOrEmpty(blobName)) 
      { 
       var container = BlobHelper.GetBlobContainer(); 
       var blob = container.GetBlockBlobReference(blobName); 

       // Download the blob into a memory stream. Notice that we're not putting the memory 
       // stream in a using statement. This is because we need the stream to be open for the 
       // API controller in order for the file to actually be downloadable. The closing and 
       // disposing of the stream is handled by the Web API framework. 
       var ms = new MemoryStream(); 
       await blob.DownloadToStreamAsync(ms); 

       // Strip off any folder structure so the file name is just the file name 
       var lastPos = blob.Name.LastIndexOf('/'); 
       var fileName = blob.Name.Substring(lastPos + 1, blob.Name.Length - lastPos - 1); 

       // Build and return the download model with the blob stream and its relevant info 
       var download = new BlobDto 
       { 
        FileName = fileName, 
        FileUrl = Convert.ToString(blob.Uri), 
        FileSizeInBytes = blob.Properties.Length, 
        ContentType = blob.Properties.ContentType 
       }; 

       return download; 
      } 

      // Otherwise 
      return null; 
     } 


     //Retrieve blob info from the database 
     private string GetBlobName(int blobId) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

アプリフローが 'SaveBlobData'メソッドにジャンプする前でもエラーが表示されます。私は何かを逃したのですか?

+0

[ '[UnitOfWorkの]'](https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#unitofwork-attribute) –

+0

スタックトレースを共有する必要があります。 – hikalkan

答えて

0

私の質問に答えるのは嫌ですが、ここは...しばらくして、何らかの理由でUnitOfWorkManagerが利用できない場合、IUnitOfWorkManagerをコンストラクタで初期化してコード内でインスタンス化できます。その後、単にあなたのSaveメソッドで次のような構成を使用することができます。

using (var unitOfWork = _unitOfWorkManager.Begin()) 
    { 
     //Save logic... 

     unitOfWork.Complete(); 
    } 
+1

おそらく、クラス参照を介してコントローラからappサービスを使用しています。その場合、UploadBlobsメソッドを仮想にすることができます。それから、UOWインターセプターがそれを処理できます。 – hikalkan

+1

コードには悪い習慣があります。コントローラでアップロードを行い、アプリケーションサービスでHttpContextを使用しないでください。アプリケーションサービスは、Web参照から分離する必要があります。 –

関連する問題