我们有Windows服务,该服务托管Web api并在后台执行一些计算工作。 我们有时会收到例外情况:
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()
启动时,我们没有内置DI的任何例外,因此依赖项已正确配置。
我进行了调查。 MediatR的ServiceFactory是Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService
的委托
方法。
ServiceProviderEngineScope
在MediatR调用时被处理。
https://github.com/aspnet/DependencyInjection/blob/master/src/DI/ServiceLookup/ServiceProviderEngineScope.cs
在MediatR调用ServiceProviderEngineScope
之前为何要处置它的任何想法?
MediatR 版本7.0.0。
Asp.Net核心 3.0。