我们有托管 web api 的 windows 服务,并在后台做一些计算工作。我们不时得到异常:
2019-12-27 11:49:15.525 +01:00 [Error] [Serilog.Log.Write] Unhandled exception caught. Runtime is terminating : True
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at MediatR.ServiceFactoryExtensions.GetInstances[T](ServiceFactory factory)
at MediatR.Internal.RequestHandlerWrapperImpl`2.Handle(IRequest`1 request, CancellationToken cancellationToken, ServiceFactory serviceFactory)
at MediatR.Mediator.Send[TResponse](IRequest`1 request, CancellationToken cancellationToken)
at DataProcessor.Enricher.ChangeTracking.LegacyDataChangeTracker.SendForEnrichment(TurbineTimeCounters turbineTimeCounters, Unit unit)
at DataProcessor.Enricher.ChangeTracking.LegacyDataChangeTracker.<ProcessEventsAndHistData>b__29_1(Unit unit)
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
Run Code Online (Sandbox Code Playgroud)
启动时内置 DI 没有任何异常,因此依赖项配置正确。
我做了调查。MediatR 的 ServiceFactory 是Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService
方法的委托。
ServiceProviderEngineScope在 MediatR 调用它时处理。
https://github.com/aspnet/DependencyInjection/blob/master/src/DI/ServiceLookup/ServiceProviderEngineScope.cs
任何想法为什么ServiceProviderEngineScope在 …