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 ?
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