Use Task.Factory.StartNew in MVC 4 async ApiController?

I am using MVC4 ApiController to load data into Azure Blob. Here is a sample code:

public Task PostAsync(int id)
{
    return Task.Factory.StartNew(() => 
    {
        // CloudBlob.UploadFromStream(stream);
    });
}

Does this code make sense? I think ASP.NET is already processing the request in the workflow, so running UploadFromStream on another thread does not make sense, since it now uses two threads to start this method (I assume the original workflow is waiting for this UploadFromStream to finish?)

So, I understand that async ApiController makes sense only if we use built-in async methods such as HttpClient.GetAsync or SqlCommand.ExecuteReaderAsync. These methods probably use internal I / O ports so that it can free the thread while doing the actual work. So should I change the code to this?

public Task PostAsync(int id)
{
    // only to show it using the proper async version of the method.
    return TaskFactory.FromAsync(BeginUploadFromStream, EndUploadFromStream...)
}

, Post CPU/, PostAsync . , public public void Post (int id), ?

. , ASP.NET MVC. .

+5
1

, , , . ..

:

, UploadFromStream?

. , . , . ( HttpClient.GetAsync).

+1

All Articles