从 class 外部调用某些异步方法时出现异常

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

假设我 class 像这样调用 x 和 y

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

在 test.aspx.cs 文件中

我正在尝试创建此 class

的实例
            x object =new x();

当我 运行 时,我在 运行 时得到一个异常说:

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.

谁能解释一下为什么?

我找到了解决方案(即我按照 @Richard 给出的说明进行操作)

并将 Async="true" 添加到我的 aspx 页面中的 Page 标签(看起来像这样 <%@ Page %> 的标签)

成功了!