Getting an Exception while calling some async methods from outside the class

隐身守侯 提交于 2019-12-02 23:29:34

问题


lets say i have class called x and y like this

class x
{
    public x()
    {
         p();
    }
    private async p()
    {
      await q();
    }
    private async p()
    {
     //some logic is there  
    }
}

in test.aspx.cs file

i am trying to create an instance of this class

            x object =new x();

when i run this i get an exception at runtime saying :

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" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.

can any body explain me why ?


回答1:


I found the solution (that is i followed the instruction the @Richard gave )

and added Async="true" to my aspx page in side the Page tag(the one that looks like this <%@ Page %> )

and it worked !



来源:https://stackoverflow.com/questions/29096865/getting-an-exception-while-calling-some-async-methods-from-outside-the-class

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