问题
In my ASP.NET MVC 3 project I'm calling a web service for login authentication. But it throws an exception:
Exception Details:
An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.
How to fix this issue?
回答1:
Make sure that your Controller method returns an async Task.
public class ServiceController : Controller
{
public async Task<ActionResult> Index()
{
var service = new Service();
await service.CallMethodAsync();
return View();
}
}
Basically, the documentation is written in a way where they believe you are only using ASP.NET WebForms, however obviously you can use this in MVC applications too, so their documentation needs to be updated.
回答2:
You are calling an ASYNC method therefore you must add Async="true" inside your page declaration <%@ Page .....%>.
来源:https://stackoverflow.com/questions/17101708/an-asynchronous-operation-cannot-be-started-at-this-time-exception-occurs-on-cal