2

i have some action inside controller likes:

public class ValuesController : Controller
{
    [HttpPost]
    public string GetInfo()
    {
        Thread.Sleep(30000); // logics imitation
        return "result";
    }
}

when I send request from client-side on this action I'll receive "Main Thread blocking" (like deadlock) while awaiting "logics imitations" how i can prevent it? already tried:

public class ValuesController : Controller
{
    [HttpPost]
    public async Task<string> GetInfo()
    {
        return await Task.Factory.StartNew(() =>
        {
            Thread.Sleep(30000);
            return "result";
        });;
    }
}

Not working...

already looked (ASP.NET MVC and Ajax, concurrent requests?), but SessionState.ReadOnly way is not for me...

also tried using .svc service instead controller-action but have same troubles.

un.spike
  • 4,857
  • 2
  • 18
  • 38
  • Why would you want the thread to sleep at all? – Dean.DePue Feb 11 '15 at 14:05
  • cause i'll 'await' another result from outside. – un.spike Feb 11 '15 at 14:21
  • Just to clarify - what is it you are really trying to accomplish? Do you just want the client to "fire and forget"? Do you want to make sure threads are available in the ASP.Net thread pool for requests (as opposed to long-running tasks)? – M Smearer Feb 11 '15 at 16:45

2 Answers2

2

MVC by default blocks parallel sessions, and there is only one way to avoid it:

[SessionState(SessionStateBehavior.ReadOnly)]

Attribute on Controller and clearly separating logics with using writing and reading sessions.

un.spike
  • 4,857
  • 2
  • 18
  • 38
1

This question was posted by my colleague. Yes, we have the situation where we need to "fire and forget" (we are trying to call actions asynchronically, when different actions are executed at the same time, but still all we have managed to get is to call actions one after another)

Denis
  • 90
  • 1
  • 1
  • 5
  • If you want the client to just make requests and not worry about a meaningful response, I would suggest looking at [this question](http://stackoverflow.com/questions/6374860/fire-and-forget-with-asp-net-mvc). In a nutshell - no worries about async controller. The thread that handles the request spins up a new Task (or Thread) and immediately returns. That new Task or Thread goes about it's business. The downside is that you don't really know, unless you are storing it somewhere, the result of the long-running logic. – M Smearer Feb 11 '15 at 17:50
  • need to able "fire and forget" but also need available recieve result after awaiting "without returning empty result" – un.spike Feb 12 '15 at 07:29