当前位置: 移动技术网 > IT编程>开发语言>.net > 为什么不要使用 async void?

为什么不要使用 async void?

2019年04月04日  | 移动技术网IT编程  | 我要评论

煤矿井下窒息事故,刘雅婷瘦的照片,卡酷精灵官网

问题

在使用 abp 框架的后台作业时,当后台作业抛出异常,会导致整个程序崩溃。在 abp 框架的底层执行后台作业的时候,有 try/catch 语句块用来捕获后台任务执行时的异常,但是在这里没有生效。

原始代码如下:

public class testappservice : itestappservice
{
    private readonly ibackgroundjobmanager _backgroundjobmanager;

    public testappservice(ibackgroundjobmanager backgroundjobmanager)
    {
        _backgroundjobmanager = backgroundjobmanager;
    }

    public task getinvalidoperationexception()
    {
        throw new invalidoperationexception("模拟无效操作异常。");
    }

    public async task<string> enqueuejob()
    {
        await _backgroundjobmanager.enqueueasync<bg, string>("测试文本。");

        return "执行完成。";
    }
}

public class bg : backgroundjob<string>, itransientdependency
{
    private readonly testappservice _testappservice;

    public bg(testappservice testappservice)
    {
        _testappservice = testappservice;
    }

    public override async void execute(string args)
    {
        await _testappservice.getinvalidoperationexception();
    }
}

调用接口时的效果:

原因

出现这种情况是因为任何异步方法返回 void 时,抛出的异常都会在 async void 方法启动时,处于激活状态的同步上下文 (synchronizationcontext) 触发,我们的所有 task 都是放在线程池执行的。

所以在上述样例当中,此时 asyncvoidmethodbuilder.create() 使用的同步上下文为 null ,这个时候 threadpool 就不会捕获异常给原有线程处理,而是直接抛出。

线程池在底层使用 asyncvoidmethodbuilder.craete() 所拿到的同步上下文,所捕获异常的代码如下:

internal static void throwasync(exception exception, synchronizationcontext targetcontext)
{
    var edi = exceptiondispatchinfo.capture(exception);

    // 同步上下文是空的,则不会做处理。
    if (targetcontext != null)
    {
        try
        {
            targetcontext.post(state => ((exceptiondispatchinfo)state).throw(), edi);
            return;
        }
        catch (exception postexception)
        {
            edi = exceptiondispatchinfo.capture(new aggregateexception(exception, postexception));
        }
    }
}

虽然你可以通过挂载 appdoamin.current.unhandledexception 来监听异常,不过你是没办法从异常状态恢复的。

参考文章:

stephen cleary:

jerome laban's:

布鲁克石:

解决

可以使用 asyncbackgroundjob<targs> 替换掉之前的 backgroundjob<targs> ,只需要实现它的 task executeasync(targs args) 方法即可。

public class bgasync : asyncbackgroundjob<string>,itransientdependency
{
    private readonly testappservice _testappservice;

    public bgasync(testappservice testappservice)
    {
        _testappservice = testappservice;
    }

    protected override async task executeasync(string args)
    {
        await _testappservice.getinvalidoperationexception();
    }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网