Question

To improve performance of our MVC3 application we want to mark some Controller's to AsyncController. We use EF 4.x (latest stable), and need some advices, links how to make async calls to EF in a proper way. How to Dispose Entities etc.

If its possible at all and make sense. Thanks.

Was it helpful?

Solution

Don't be fooled into thinking that by making your controllers Async you will improve performance. In most cases you will make things worse. The only scenario where Async controllers could improve performance is when you take advantage of IO/Completion Ports. This could happen when you have IO intensive operations such as database or web service calls. Only by using the async versions of the ADO.NET BeginXXX and EndXXX you will get benefit from async controllers.

If you just create an async controller and inside it you invoke a synchronous operation on your DataContext you are loosing all the benefits and even making things worse.

Another useful scenario where async controllers could be used to improve performance is when you want to parallelize multiple operations. This could be used only if the work you are performing could be parallelized.

I would recommend you taking a look at the following article on MSDN. It's about async ASP.NET pages, but the concepts are exactly the same in MVC (where the pages are controllers). Under the covers asynchronous controllers are implemented using the same mechanism. So make sure you have understood very well the concepts of IO/Completion Ports before diving into asynchronous programming.

Another advice: don't do any premature optimizations. Start by load testing your application and only if you find out that your synchronous database calls are actually a bottleneck you should take a look at async controllers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top