Autofac注册多个容器

时间:2014-11-04 01:17:54

标签: c# dependency-injection ioc-container autofac

我有一个MVC应用程序,我正在使用Autofac来解决依赖关系。

我有一种情况,我必须创建2个容器,运行时应根据条件决定使用哪个容器。

条件是如果调用控制器Home,我需要使用container1,否则我必须使用container2。

Application_Start是我注册容器的地方。

我不确定如何在运行时实现这一点。任何帮助都非常感谢。

由于

1 个答案:

答案 0 :(得分:1)

让控制器从不同容器中解析的一个原因是,您的应用程序是由多个独立模块组成的。在这种情况下,您可能不希望模块相互影响,并且每个模块的容器都有意义。然而,在几乎所有其他情况下,拥有多个容器实例是没有意义的。

因此,如果您需要,您需要构建自己的自定义IControllerFactory,可以根据控制器类型切换容器。例如,像这样:

internal sealed class MultiplContainerControllerFactory : IControllerFactory
{
    public IController CreateController(RequestContext requestContext, string controllerName)
    {
        var factory = this.GetFactory(requestContext);
        var controller = factory.CreateController(requestContext, controllerName);

        // By storing the factory in the request items for this controller, 
        // we allow it to be easily retrieved
        // during ReleaseController and delegate releasing to the correct controller.
        HttpContext.Current.Items["ContrFct_" + controller.GetType().FullName] = factory;

        return controller;
    }

    public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext,
        string controllerName)
    {
        var factory = this.GetFactory(requestContext);
        return factory.GetControllerSessionBehavior(requestContext, controllerName);
    }

    public void ReleaseController(IController controller)
    {
        var controllerFactory = (IControllerFactory)HttpContext.Current.Items["ContrFct_" +
            controller.GetType().FullName];

        controllerFactory.ReleaseController(controller);
    }

    private IControllerFactory GetFactory(RequestContext context)
    {
        // return the module specific factory based on the requestcontext
    }
}

除此之外,每个容器需要一个特殊的AutofacControllerFactory。这可能看起来像这样:

public sealed class AutofacControllerFactory : DefaultControllerFactory
{
    public readonly IContainer Container;
    private readonly string moduleName;

    public AutofacControllerFactory(IContainer container, string moduleName)
    {
        this.Container = container;
        this.moduleName = moduleName;
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            // The base method throws an expressive 404 HTTP error.
            base.GetControllerInstance(requestContext, controllerType);
        }

        // We need to start a new lifetime scope when resolving a controller.
        // NOTE: We can apply MatchingScopeLifetimeTags.RequestLifetimeScopeTag to the BeginLifetimeScope
        // method and in this case we can use .InstancePerRequest(), but in that case it becomes impossible to
        // verify the DI configuration in an integration test.
        ILifetimeScope lifetimeScope = this.Container.BeginLifetimeScope();

        // We need to store this lifetime scope during the request to allow to retrieve it when the controller
        // is released and to allow to dispose the scope. Memory leaks will be ensured if we don't do this.
        HttpContext.Current.Items[controllerType.FullName + "_lifetimeScope"] = lifetimeScope;

        // This call will throw an exception when we start making registrations with .InstancePerRequest, 
        // because the WebRequest functionality of Autofac is tied to the AutofacDependencyResolver, which we
        // don't use here. We can't use the AutofacDependencyResolver here, since it stores the created lifetime 
        // scope in the HttpContext.Items, but it uses a fixed key, which means that if we resolve multiple
        // controllers for different application modules, they will all reuse the same lifetime scope, while
        // this scope originates from a single container.
        try
        {
            return (IController)lifetimeScope.Resolve(controllerType);
        }
        catch (Exception ex)
        {
            lifetimeScope.Dispose();

            throw new InvalidOperationException("The container of module '" + this.moduleName +
                "' failed to resolve controller " + controllerType.FullName + ". " + ex.Message, ex);
        }
    }

    [DebuggerStepThrough]
    public override void ReleaseController(IController controller)
    {
        try
        {
            base.ReleaseController(controller);
        }
        finally
        {
            var scope = (ILifetimeScope)HttpContext.Current
                .Items[controller.GetType().FullName + "_lifetimeScope"];

            scope.Dispose();
        }
    }
}