有没有一种方法可以将两个自动容器合并成一个?我的意思是,新容器中将包含来自两个容器的所有注册信息。或者,可以升级其中一个容器以包含另一个容器的信息。
例如:
我曾尝试遍历“container.ComponentRegistry.Registrations”,并通过使用containerBuilder注册组件和升级第二个容器,但由于某些原因存在一些冲突。
任何想法?
例如:
public IContainer Merge(IContainer container1, IContainer container2)
{
return ???;
}
我曾尝试遍历“container.ComponentRegistry.Registrations”,并通过使用containerBuilder注册组件和升级第二个容器,但由于某些原因存在一些冲突。
Autofac.Core.DependencyResolutionException : An exception was thrown while executing a resolve operation. See the InnerException for details. ---> The provided instance has already been used in an activation request. Did you combine a provided instance with non-root/single-instance lifetime/sharing? (See inner exception for details.)
----> System.InvalidOperationException : The provided instance has already been used in an activation request. Did you combine a provided instance with non-root/single-instance lifetime/sharing?
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Container.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, ref Object instance)
at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.Resolve(IComponentContext context, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.Resolve(IComponentContext context)
at Bootstrap.Bootstrapper.CreatePersistentTimerAsyncExecuter(IContainer container)
at Bootstrap.Bootstrapper.Monitor(IContainer container)
at Bootstrap.Bootstrapper.Boot(Assembly[] assemblies)
at Soluto.Telemetry.Processing.Core.IntegrationTests.TelemetryProcessingBootstrapperTests.Boot_With2TelemetryHandlersInAssembly_ResolvesSubscriberWithItsHandlers() in TelemetryProcessingBootstrapperTests.cs: line 88
--InvalidOperationException
at Autofac.Core.Activators.ProvidedInstance.ProvidedInstanceActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.<Execute>b__0()
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.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
任何想法?
update
方法被重载,可以接受IComponentRegistry
或者IContainer
作为参数。 - Stephen Ross