为什么不要使用 async void
問題
在使用 Abp 框架的后臺(tái)作業(yè)時(shí),當(dāng)后臺(tái)作業(yè)拋出異常,會(huì)導(dǎo)致整個(gè)程序崩潰。在 Abp 框架的底層執(zhí)行后臺(tái)作業(yè)的時(shí)候,有 try/catch 語句塊用來捕獲后臺(tái)任務(wù)執(zhí)行時(shí)的異常,但是在這里沒有生效。
原始代碼如下:
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 "執(zhí)行完成。";
}
}
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();
}
}
調(diào)用接口時(shí)的效果:
原因
出現(xiàn)這種情況是因?yàn)槿魏萎惒椒椒ǚ祷?void?時(shí),拋出的異常都會(huì)在?async void?方法啟動(dòng)時(shí),處于激活狀態(tài)的同步上下文 (SynchronizationContext) 觸發(fā),我們的所有 Task 都是放在線程池執(zhí)行的。
所以在上述樣例當(dāng)中,此時(shí)?AsyncVoidMethodBuilder.Create()?使用的同步上下文為?null?,這個(gè)時(shí)候?ThreadPool 就不會(huì)捕獲異常給原有線程處理,而是直接拋出。
線程池在底層使用?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 來監(jiān)聽異常,不過你是沒辦法從異常狀態(tài)恢復(fù)的。
參考文章:
Stephen Cleary:https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
Jerome Laban's:https://jaylee.org/archive/2012/07/08/c-sharp-async-tips-and-tricks-part-2-async-void.html
布魯克石:https://www.cnblogs.com/brookshi/p/5240510.html
解決
可以使用?AsyncBackgroundJob<TArgs>?替換掉之前的?BackgroundJob<TArgs>?,只需要實(shí)現(xiàn)它的?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();
}
}
原文地址:https://www.cnblogs.com/myzony/p/10647460.html
.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總?http://www.csharpkit.com?
總結(jié)
以上是生活随笔為你收集整理的为什么不要使用 async void的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【微服务学习】Polly:熔断降级组件
- 下一篇: 微软推出新语言Bosque,超越结构化程