问题
在使用 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的原因分析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
相关文章
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 87
-
Spring Cloud Alibaba和Dubbo融合实现
2025-05-29 63 -
2025-05-25 86
-
宝塔Linux面板是做什么用的?宝塔Linux面板主要功能作用
2025-05-25 14 -
2025-06-04 36