2
在通過Autofac注入的對象的惰性實例化過程中,我得到一個ObjectDisposedException(實例無法解析並且嵌套生命期無法從此LifetimeScope中創建,因爲它已經處理完畢)一個異步的ASP.NET Web API方法。Autofac異步ASP.NET Web API方法中的ObjectDisposedException
這是Web API方法:
Lazy<PrintHubSender> _printHubSender;
public async void Print(Guid orderId)
{
var order = await _orderNoSqlDbRepository.GetSingleAsync(o => o.Id == orderId);
_printHubSender.Value.PrintOrder(order); // This triggers the exception
}
懶惰實例通過網絡API的控制器構造注入。 Autofac註冊如下:
Type hubSender = ...
containerBuilder.RegisterType(hubSender)
.AsSelf()
.PropertiesAutowired()
.InstancePerRequest();
如果ASP.NET Web API方法不是異步,則不會發生異常。