Request.Content.ReadAsMultipartAsync throws a System.IO exception

I upload the image to the ASP WebAPI service and then upload it to windows azure. everything works fine, but suddenly I started getting the following exception:

System.IO.IOException: The process cannot access the file 'C:\DWASFiles\Sites\Tasteat\Temp\BodyPart_a5c79910-6077-4c24-b814-10fdc0e0b3d4' because it is being used by another process. 

This is the code that throws the exception:

  var provider = new BlobStorageProvider(container); Trace.TraceInformation("Uploading raw image to blob"); await Request.Content.ReadAsMultipartAsync(provider); Trace.TraceInformation("Uploading finished"); 

I know him this line is await Request.Content.ReadAsMultipartAsync(provider); because I see the line before it in the log, but not the line after it.

Any ideas?

Everything worked fine until a few days

+5
source share
1 answer

So, it seems to me that the code that I posted above actually saves the local file and only then uploads it to the server, which causes an error, but also slower. after many attempts, I finally move on to the next solution, and it all started, and it was even faster!

first create a streamprovider:

 public class BlobStorageMultipartStreamProvider : MultipartStreamProvider { private readonly string _containerName; private readonly string _fileName; public BlobStorageMultipartStreamProvider(string containerName, string fileName) { _containerName = containerName; _fileName = fileName; } public override Stream GetStream(HttpContent parent, HttpContentHeaders headers) { Stream stream = null; if (!String.IsNullOrWhiteSpace(_fileName)) { string connectionString = ConfigurationManager.ConnectionStrings["BlobStorage"].ConnectionString; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(_containerName); CloudBlockBlob blob = blobContainer.GetBlockBlobReference(_fileName); stream = blob.OpenWrite(); } return stream; } } 

Download Code:

  string fileName = Guid.NewGuid()+".Png"; MultipartStreamProvider provider = new BlobStorageMultipartStreamProvider("container",fileName); Trace.TraceInformation("Uploading raw image to blob"); await Request.Content.ReadAsMultipartAsync(provider); Trace.TraceInformation("Uploading finished"); 
+1
source

Source: https://habr.com/ru/post/1200951/


All Articles