0

I'm trying to implement an asynchronous action in ASP.NET MVC. The code looks like follows:

[HttpGet]
public async Task<string> HeavyAction(int holdTime)
{
      await Task.Delay(holdTime);
      return "Now I'm done!";
}

This action shows the main problem, that's when the user calls the action with a big parameter value (10000) the application isn't responsive - the request is sent but the browser is waiting for the response. I think so shouldn't look an asynchronous request. In my opinion the user should be able to call other actions in the mean time.

Could somebody help me with my problem? Thanks! :-)

6
  • How are you calling this method? Button or JavaScript? Commented Nov 15, 2015 at 20:31
  • From the query string, because im testing the async-await mechanism. The action get called, but I didn't see the asynchronous behavior. Is it possible in web applications to get it with async-await? Commented Nov 15, 2015 at 20:35
  • @sm4artGuy async does not mean that the method will run in the background asynchronously. It means that while awaiting the job that you do with 'await' keyword, it will not occupy a thread, allowing the thread to be used by another request. When the job is done, the rest of the code will then occupy a thread again to finish the request. So it is not about doing work asynchronously, it is about waiting asynchronously to use the system resources more efficiently. Commented Nov 15, 2015 at 21:02
  • @serhatozgel Oh now I understand. In this case the last question. Is there a way to do the upload asynchronously (the upload logic executes so that the user is able to do other things, and when the download completes he get e message or something)? Commented Nov 15, 2015 at 21:23
  • My suggestion would be to use an iframe, then host your page in that iframe and upload the files. Ensure you use readonly session state, or no session state at all. This way you can upload the files all in parallel. And you can query the server to see the status of files upload as well, as you aren't blocking the session. Have a look at the JavaScript plugin upload-at-click (old but still works perfectly). Https://code.google.com/p/upload-at-click Commented Nov 15, 2015 at 21:59

2 Answers 2

3

I think so shouldn't look an asynchronous request.

You have a misunderstanding of how "async-await" works. When your method awaits on an async method, control is yielded back to the caller, in this case the ASP.NET runtime. But, async-await does not change the request-response nature of the HTTP protocol. Thus, only once Task.Delay completes, the next line of code executes and returns a response to the caller.

What you're thinking of is a "fire and forget" style of execution where you queue a delegate on the threadpool and immediately return control. Such nature isn't really suited for ASP.NET and will certainly hurt performance if your app needs any sort of scale.

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

2 Comments

Oh in this case it's not possible to accomplish the behavior that I want? Btw. the real problem looks as follows: I'm implementing an upload service for small files (pictures, text etc.). For the upload I've got done the code and it's working. But during the upload the user must wait until the action finishes. Is there a way to make it more elastic? So that the user is able to explore his uploaded files or do other things on the service? And when the upload finishes he get a prompt or something like that?
@sm4rtGuy I'm not sure using ASP.NET MVC is the right solution. Perhaps look into queues or pub/sub services. There are plenty implementations available, such as RabbitMQ or Kafka.
0

When you use 'await', you are telling C# to wait for the action to finish. Try calling your async method without the 'await' keyword. Like:

[HttpGet]
public async Task<string> HeavyAction(int holdTime)
{
      Task.Delay(holdTime);
      return "Now I'm done!";
}

so the method will return immediately and execute Task.Delay later.

4 Comments

Task.Delay has no meaning here at all. It'll simply kick off the internal timer and continue execution to the next line. There will be no execution later.
I think op uses Task.Delay as a decoy for what he/she actually does in the app.
I understand that, but using any sort of task returning method as a fire and forget mechanism is usually bad, especially if the work being done should be returned ti the caller or should be verified for completion. This is especially in an environment where arbitrary threadpool threads have no guarantee for completion.
Yes, you are right. But that is another problem and that problem is with the design of the system, which would be beyond the scope of this question.

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.