I am getting an ObjectDisposedException (Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed) during lazy instantiation of an object injected via Autofac in an async ASP.NET Web API method.
This is the Web API method:
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
}
The Lazy instance is injected via Web API controller constructor. The Autofac registration is as follows:
Type hubSender = ...
containerBuilder.RegisterType(hubSender)
.AsSelf()
.PropertiesAutowired()
.InstancePerRequest();
If the ASP.NET Web API method is not async the exception does not occur.