I am planning to use ASP.NET async pattern in my Sitecore project, but I'm getting the error:
Asynchronous method which returns task as result cannot be executed synchronously
Is there a way to achieve async in Sitecore 8.1?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.Sign up to join this community
Async controller rendering is not supported . Starting with Sitecore 8.2 async Mvc controllers is supported.
As of 8.2 we support async MVC controllers (directly routed). But async is still not supported for Controller Renderings.
Async only really works when it top to bottom across the stack and there are a number of challenges with various Sitecore APIs not being async (pipelines and data APIs being the big ones).
Ideally SitecoreController.Index should be async and ditto should all calls down to db level. We will need to piecemeal this one.
More information you can find here:
You can not use async pattern in Sitecore as it does not support it. If you will try to make it work, you will get exceptions or it will work synchronously. There is a very good article from kamsar on this. Please go through it to understand better. A quick excerpt from the article -
Looking around in the ASP.NET MVC source code, you can see that there are two interfaces for action invokers: IActionInvoker and IAsyncActionInvoker. Guess what? Sitecore’s ActionInvoker implements IActionInvoker but NOT IAsyncActionInvoker - which means that even though the default MVC action invoker it was wrapping supported async invocation, the wrapper’s lack of IAsyncActionInvoker implementation meant ASP.NET MVC wouldn’t use async invocation at all - and would instead throw an error that you cannot return a Task from a synchronous controller