An asynchronous operation cannot be started at this time Exception occurs on calling WebService?

我的未来我决定 提交于 2021-01-26 20:01:08

问题


In my ASP.NET MVC 3 project I'm calling a web service for login authentication. But it throws an exception:

Asynchronous 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!