2017-08-22 29 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来在代码中实例化它。然后,你可以简单地使用下面的建筑你的保存方法:

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

     unitOfWork.Complete(); 
    } 
+1

您可能使用控制器通过类引用的应用程序服务。在这种情况下,您可以使UploadBlobs方法变为虚拟。然后UOW intercepter可以处理它。 – hikalkan

+1

代码中有一个不好的做法。在控制器中进行上载,并且在应用程序服务中不使用HttpContext。应用程序服务需要从Web引用中分离出来。 –

相关问题