Any problems using ASP.NET MVC AsyncController instead of Controller?

We have a series of ASP.NET MVC controllers that all inherit from one base controller (which inherits from the Controller class). Now we are looking at creating some asynchronous actions and are wondering if we could run into any problem if we just changed the base controller to inherit from AsyncController instead of Controller (which means that all our controllers inherit from AsyncController).

+4
source share
5 answers

Jess,

In my opinion, you will not do much harm, since asynchronous functionality is called only if you follow the conventions:

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 }); } } 

agreement to add new news * Async * and News * Completed * in the names.

cm

asynchronous controllers in asp.net mvc 2

Note that the controller class now derives from AsyncController, and not from Controller. In addition, the News action method has been split into methods called NewsAsync and NewsCompleted, which are similar to Begin and End methods on asynchronous pages. Logically, the controller still provides one action method called News. But physically, the implementation of the method was broken using variations of the asynchronous template used within the framework of the .NET platform.

If you do not change anything in your inherited controller code, then no asynchronous activity will be triggered. However, as Robert stated above (or below, maybe :-)), you can decorate needs-based actions to keep the intention clear, "I personally believe that the agreement should clearly show this.

definitely worth the debate.

+6
source

MVC 4 does not have an asynchronous controller - see 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

+2
source

You should be fine, since AsyncController already inherits from Controller .

See here for more details.

The notes on this page are very helpful in determining whether you are doing the right thing, inheriting from AsyncController and offering a nice guide to keep you up to date.

+1
source

If you set a breakpoint in your action method and watch the stop code (with the "Show external code" code enabled), you will see that a few additional steps are required to invoke the synchronous action residing in AsyncController and in the standard controller. Namely, AsyncControllerActionInvoker calls Begin / End style methods ending in BeginInvokeSynchronousActionMethod. I don’t know how much overhead these additional calls make, but this is what you need to know about before blindly expanding all controllers from AsyncController even where there are no Async actions.

+1
source

Using an Async controller depends on whether you want to wait for a specific task or step to complete before moving on to the next task. The problem is buying something from Amazon before the money from your paycheck gets into your current account.

For typical web applications, I would say that this is not recommended. The web server is already very parallel, so the only benefit will be faster response to the user. For many operations, this advantage would be negligible.

I would reserve Async controllers for lengthy processes in the background where it is impractical to wait for the task to complete before returning the web page back to the user control.

NOTE: If you have a copy of the .NET disassembler (or the source of ASP.NET MVC ), you can open the AsyncController class and see the code. This should give you a pretty good idea: can you use AsyncController as a regular Controller .

The following article says: "The controllers that are manufactured by AsyncController allow ASP.NET to handle asynchronous requests, and they can still serve synchronous action methods."

http://msdn.microsoft.com/en-us/library/ee728598.aspx

0
source

Source: https://habr.com/ru/post/1334377/


All Articles