Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any issues with always using ASP.NET MVC AsyncController instead of Controller?

Tags:

asp.net-mvc

We have a series of ASP.NET MVC controllers that all inherit from a single base controller (that inherits from the Controller class). We are now looking at creating some asynchronous actions, and was wondering if we'd run into any trouble if we just changed the base controller to inherit from AsyncController instead of Controller (meaning all of our controllers would inherit from AsyncController).

like image 401
Beep beep Avatar asked Dec 29 '22 03:12

Beep beep


2 Answers

Jess,

In my opinion, you'll do no harm as the asynch functionality is only called into play when you follow the conventions of:

public class PortalController : AsyncController
{
    public void NewsAsync(string city)
    {

        AsyncManager.OutstandingOperations.Increment();
        NewsService newsService = new NewsService();
        newsService.GetHeadlinesCompleted += (sender, e) =>
        {
            AsyncManager.Parameters["headlines"] = e.Value;
            AsyncManager.OutstandingOperations.Decrement();
        };
        newsService.GetHeadlinesAsync(city);
    }

    public ActionResult NewsCompleted(string[] headlines)
    {
        return View("News", new ViewStringModel
        {
            NewsHeadlines = headlines
        });
    }
}

the convention being the addition of the News*Async* and the News*Completed* portions in the naming.

see:

async controllers in asp.net mvc 2

Observe that the controller class now derives from AsyncController rather than Controller. In addition, the News action method has been split into methods named NewsAsync and NewsCompleted, which are analagous to the Begin and End methods in asynchronous pages. Logically, the controller still exposes a single action method named News. But physically, the method implementation has been broken up using a variation on the async pattern used throughout the .NET framework.

If you don't change anything in your inherited controller code, then no async activty will be initiated. However, as stated by Robert above (or below maybe :-)), you could decorate the actions on a needs must basis to keep the intention clear, tho', i personally think the convention should show that up clearly.

certainly worthy of debate.

like image 58
jim tollan Avatar answered Jan 17 '23 16:01

jim tollan


MVC 4 doesn't have an asynch controller - see the source:

    namespace System.Web.Mvc
{
    // Controller now supports asynchronous operations.
    // This class only exists 
    // a) for backwards compat for callers that derive from it,
    // b) ActionMethodSelector can detect it to bind to ActionAsync/ActionCompleted patterns. 
    public abstract class AsyncController : Controller
    {
    }
}

See my tutorial Using Asynchronous Methods in ASP.NET MVC 4

like image 22
RickAndMSFT Avatar answered Jan 17 '23 17:01

RickAndMSFT