1

I'm building an mvc 4 application that makes file uploading. When I get HttpPostedFileBase range, I get streams on them and pass to my business logic layer to save them and bind with database records. This is done without any problems. But when displaying current state of uploading progress comes in, I'm a bit confused. I make a second request while an uploading runs, but it waits for first request to be executed completely.

I know that in same browser client instance (actually same session), my requests are synchronized. But there is a solution that I read about, asynchronous actions.

To try asynchronous actions, I used Stream.CopyToAsync(..) instead of Stream.CopyTo(..). I'm also using Task.Delay(10000) to simulate file uploading progress. Then while asynchronous UploadFile action runs, I invoked synchronous UploadProgress on same browser instance. Result is still waiting for first request to complete. Below is the code I use. Where am I wrong?

Here is async action to upload files;

[HttpPost]
public async Task<ActionResult> Upload(PFileUploadModel files)
{
    if (!Session.GetIsLoggedIn())
        return RedirectToAction("Login", "User");

    var fileRequest = Session.CreateRequest<PAddFileRequest, bool>(); //Creates a business logic request
    fileRequest.Files.Add(...);
    var result = await Session.HandleRequestAsync(fileRequest); //Handles and executes a business logic request by checking authority

    if (result)
        return RedirectToAction("List");

    return RedirectToError();
}

And upload progress action is simple as below for now :) :

public ActionResult UploadProgress()
{
    //var saveProgressRequest = Session.CreateRequest<PSaveFileProgressInfoRequest, PSaveFileProgressInfoResponse>();
    //saveProgressRequest.ProgressHandle = progressHandle;
    //var response = Session.HandleRequest(saveProgressRequest);
    return Content("Test!");
}

Thanks for helping.

1 Answer 1

1

async doesn't change the HTTP protocol. The Upload request is in progress until you return the result.

You're probably running into the ASP.NET session lock, which ensures that multiple request handlers don't interfere with each other when reading/writing session state. I believe that MVC always takes a write lock on the session state by default, preventing any other actions from executing simultaneously (in the same session).

You can override this behavior by specifying the SessionState attribute on your controller.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried SessionState now, and it worked. But what asynchronous actions for if I can't make asynchronous request from same client?
Asynchronous handlers allow your server to scale better; when they await an incomplete operation, they return the thread to the thread pool for other requests to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.