获得一个异常调用时从类之外的一些异步方法 [英] Getting an Exception while calling some async methods from outside the class

查看:186
本文介绍了获得一个异常调用时从类之外的一些异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有类,称为X和Y像这样

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

在test.aspx.cs文件

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 :

这是异步操作不能在这个时候开始。异步操作只能在异步处理程序或模块或在在页面生命周期的某些事件来启动。如果在执行一个页面出现此异常,请确保该页面被标记为LT;%@页面异步=真正的%>。此异常也可能表明有人企图所说的异步作废的方法,这是在ASP.NET请求处理一般不支持。相反,异步方法应该返回一个任务,调用者应等待吧。

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 )

和增加异步=真正的来我的aspx页面中端页面标签(一个看起来像这样&LT;%@页%&GT;

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

和它的工作!

这篇关于获得一个异常调用时从类之外的一些异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆