Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 266
Jasper Manickaraj Avatar asked Jun 14 '13 05:06

Jasper Manickaraj


2 Answers

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.

like image 125
Patrick Magee Avatar answered Oct 19 '22 05:10

Patrick Magee


You are calling an ASYNC method therefore you must add Async="true" inside your page declaration <%@ Page .....%>.

like image 20
Yahya-Imam Munir Avatar answered Oct 19 '22 06:10

Yahya-Imam Munir