Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ASP.NET MVC 2 asynchronous controllers, do Action Filters execute asynchronously?

In ASP.NET MVC 2 asynchronous controllers, we can do something like this:

public class SomeController : AsyncController
{
    [Blurb]
    public void FooAsync()
    {
        this.AsyncManager.OutstandingOperations.Increment();

        Task.Factory.StartNew(
            () =>
            {
                this.AsyncManager.Parameters["result"] = new BazResult();
                this.AsyncManager.OutstandingOperations.Decrement();
            });
    }

    public ActionResult FooCompleted(ActionResult result)
    {
        return result;
    }
}

My question is, does the action filter "Blurb" in this case execute asynchronously? In other words, is its synchronous nature automatically wrapped into an asynchronous call?

like image 755
dreadwail Avatar asked May 05 '11 20:05

dreadwail


People also ask

When filters are executed in MVC?

Action Filter is an attribute that you can apply to a controller action or an entire controller. This filter will be called before and after the action starts executing and after the action has executed. Action filters implement the IActionFilter interface that has two methods OnActionExecuting andOnActionExecuted.

What is the primary purpose of using an asynchronous controller action?

The asynchronous controller enables you to write asynchronous action methods. It allows you to perform long running operation(s) without making the running thread idle. It does not mean it will take lesser time to complete the action.

Which action filter executes last in an MVC application?

Exception Filters − Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results.

How do you call async method in MVC action?

STEP 01 Create new MVC Application project, named as "Async". In the File menu, click New Project. In the "New Project" dialog box, under Project types, expand Visual C#, and then click "Web". In the Name box, type "Async", then click on Ok.


1 Answers

I took a look under the covers at AsyncControllerActionInvoker and it looks like it does indeed wrap them into a set of asynchronous calls and continuations. It makes a call to BeginInvokeActionMethodWithFilters which in turn sets up InvokeActionMethodFilterAsynchronously.

For those who are curious, the source is on codeplex.

like image 159
dreadwail Avatar answered Oct 28 '22 04:10

dreadwail