c# - 将 2 个 Autofac 容器合并为一个

标签 c# inversion-of-control autofac

有没有办法将 2 个 autoface 容器合并为 1 个? 我的意思是来自两个容器的所有注册都将包含在新容器中 或者,可以将其中一个升级为另一个

例如:

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)

有什么想法吗?

最佳答案

您可以使用自定义 IRegistrationSource。此接口(interface)包含一个 RegistrationsFor 方法,该方法返回特定 IService

IComponentRegistration 列表
public class CrossContainerRegistrationSource : IRegistrationSource
{
    public CrossContainerRegistrationSource(IComponentContext componentContext)
    {
        this._componentContext = componentContext;
    }

    private readonly IComponentContext _componentContext;

    public Boolean IsAdapterForIndividualComponents
    {
        get
        {
            return false;
        }
    }

    public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, 
        Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
    {
        return this._componentContext.ComponentRegistry.RegistrationsFor(service);
    }
}

你可以这样使用它:

container2.ComponentRegistry.AddRegistrationSource(new CrossContainerRegistrationSource(container1));

但请注意此解决方案,当您使用复杂范围场景时,它可能会引入问题。

关于c# - 将 2 个 Autofac 容器合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33210448/

相关文章:

javascript - C# 如何将复杂的 JSON 对象的子对象转换为模型

c# - 使用 AutoFac 根据构造函数参数名传递不同的实例

c# - 如何在 Simple Injector 中创建 Tagged Litetimes?

c# - 我如何对使用 IoC 容器类的类进行单元测试

c# - 在 System.Windows.Form 派生类中的何处配置资源?

c# - 有效文件权限工具的windows api

c# - 在 javascript/jquery 中创建动态 TreeView ?

c# - Ninject 工厂扩展和 InCallScope 没有给出预期的结果

c# - 使用属性使用Autofac创建委托(delegate)工厂

c# - 在基础设施层使用依赖注入(inject)