好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

为什么不要使用 async void的原因分析

问题

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

原始代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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() 所拿到的同步上下文,所捕获异常的代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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 来监听异常,不过你是没办法从异常状态恢复的。

解决

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

?

1

2

3

4

5

6

7

8

9

10

11

12

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();

   }

}

总结

以上所述是小编给大家介绍的为什么不要使用 async void的原因分析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

查看更多关于为什么不要使用 async void的原因分析的详细内容...

  阅读:14次