問題
在使用 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: 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
解決
可以使用 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();
}
}