平台:NetCore 2.0
问题位置:在服务构造函数中获取IOptionsSnapshot
。
描述:在高压负载测试下,我有一个例外(很少):“集合被修改;枚举操作可能无法执行。”
它发生在class \ function Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config)
第269行
if(config != null && config.GetChildren().Any())
关于跟踪日志,它发生在linq函数Any()
中。看起来ConfigurationBinder
在这里不是线程安全的。
有什么想法可以修复吗?
跟踪日志:
[2017-12-22T09:37:38.3955213Z] Level=Error, Category=Autofac, Id=qq,
Message= Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = PlaybackService (ReflectionActivator), Services = [XXXXXXXXXXXXXXXXXXXXXXXXXX.IPlaybackService], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope --->
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = IntegrationService (ReflectionActivator), Services = [XXXXXXXXXXXXXXXXXXXXXXXXXX.IIntegrationService], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope --->
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = TokenRepository (ReflectionActivator), Services = [XXXXXXXXXXXXXXXXXXXXXXXXXX.ITokenRepository], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope --->
An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptionsSnapshot`1[XXXXXXXXXXXXXXXXXXXXXXXXXX.DXXXXConfig], XXXXXXXXXXXXXXXXXXXXXXXXXX.ICertificateRepository)' on type 'TokenRepository'. --->
Collection was modified; enumeration operation may not execute. (See inner exception for details.) --->
Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = IntegrationService (ReflectionActivator), Services = [XXXXXXXXXXXXXXXXXXXXXXXXXX.IIntegrationService], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope --->
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = TokenRepository (ReflectionActivator), Services = [XXXXXXXXXXXXXXXXXXXXXXXXXX.ITokenRepository], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope --->
An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptionsSnapshot`1[XXXXXXXXXXXXXXXXXXXXXXXXXX.DXXXXXXConfig], XXXXXXXXXXXXXXXXXXXXXXXXXX.ICertificateRepository)' on type 'TokenRepository'. --->
Collection was modified; enumeration operation may not execute. (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) --->
Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = TokenRepository (ReflectionActivator), Services = [XXXXXXXXXXXXXXXXXXXXXXXXXX.ITokenRepository], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope --->
An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptionsSnapshot`1[XXXXXXXXXXXXXXXXXXXXXXXXXX.DXXXXConfig], XXXXXXXXXXXXXXXXXXXXXXXXXX.ICertificateRepository)' on type 'TokenRepository'. --->
Collection was modified; enumeration operation may not execute. (See inner exception for details.) (See inner exception for details.) --->
Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptionsSnapshot`1[XXXXXXXXXXXXXXXXXXXXXXXXXX.DXXXXConfig], XXXXXXXXXXXXXXXXXXXXXXXXXX.ICertificateRepository)' on type 'TokenRepository'. --->
Collection was modified; enumeration operation may not execute. (See inner exception for details.) --->
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.SparseArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.SparseArrayBuilder`1.ReserveOrAdd(IEnumerable`1 items)
at System.Linq.Enumerable.Concat2Iterator`1.ToArray()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.OrderedEnumerable`1.<GetEnumerator>d_3.MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.SparseArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.SparseArrayBuilder`1.ReserveOrAdd(IEnumerable`1 items)
at System.Linq.Enumerable.Concat2Iterator`1.ToArray()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.OrderedEnumerable`1.<GetEnumerator>d3.MoveNext()
at System.Linq.Enumerable.DistinctIterator`1.MoveNext()
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo property, Object instance, IConfiguration config)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindNonScalar(IConfiguration configuration, Object instance)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config)
at Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration configuration, Object instance)
at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at lambda_method(Closure , Object[] )
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() — End of inner exception stack trace —
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) — End of inner exception stack trace —
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) — End of inner exception stack trace —
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) — End of inner exception stack trace —
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)
at Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>cDisplayClass5_0.<CreateControllerFactory>gCreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d_14.MoveNext() — End of stack trace from previous location where exception was thrown ---... MESSAGE Truncated=True DUE TO EXCEEDING MaxAllowedCharacterLength=10000, ActualCharacterLength=11707