1

I have a task that needs executing after a request has been processed by the controller and the response is on the way back to the client. To that end, I have overriden the OnActionExecuted() event on my controller and call the task inside this method.

As the task can take some time to run and also returns void, I want to call it asynchronously so that the overridden OnActionExecuted() method can return immediately and the response can be sent to the client while the task is executing. I have tried to implement this with the code below, using Task.Run() (in .net 4.5)

protected override void OnActionExecuted(ActionExecutedContext filterContext) { Task.Run(() => WrapUp()); } 

However, this does not product the desired effect. When I step through the code it still executes synchronously - everything inside the WrapUp() method is called before the OnActionExecuted() method exits. When the WrapUp() method hangs, the client is left waiting for the task to complete before the response is received.

What do I need to do to make this work as intended?

1 Answer 1

3

The OnActionExecuted will return immediately because inside you are only creating and starting a task. The behavior you are observing is not normal. What happens if you try the following:

protected override void OnActionExecuted(ActionExecutedContext filterContext) { Task.Run(() => Thread.Sleep(10000)); } 

The client is not waiting 10 seconds to get the result, is it?

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

4 Comments

Yes, you are correct that the client does not have to wait for a Thread.Sleep() at all. The behaviour I am observing is quite strange in that it does not seem to be consistent each time a request is made - but there is always some degree of waiting time while I am stepping through. I can see the OnActionExecuted method exit, but the browser is still waiting of the response even after this point.
To elaborate, the WrapUp method actually calls a method on an injected dependency on the controller, which in turn calls methods on more injected dependencies (I'm using StructureMap for IoC). The response finally seems to be delivered when I step through the latter stages of the call stack. It seems like its waiting until some or all of the injected dependencies have been released from memory - is this possible?
Well, it's normal that the browser should wait even after the OnActionExecuted method. That's not the last method in the execution pipeline. There's so much more to be done afterwards. Things like finding, preparing and rendering the view itself which could take some time as well. For example methods like OnResultExecuting, OnResultExecuted, ...
Yes, I would expect some delay. But I am triggering the response being received by allowing the call stack to continue from inside a thread started by Task.Run(). The weird thing is that its not always the same point in the call stack.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.