簡體   English   中英

Autofac注冊多個容器

[英]Autofac Registering Multiple Containers

我有一個MVC應用程序,並且我正在使用Autofac來解決依賴關系。

我遇到的情況是必須創建2個容器,運行時應根據條件決定使用哪個容器。

條件是是否調用了控制器Home,我需要使用container1,否則我必須使用container2。

Application_Start是我注冊容器的地方。

我不確定如何在運行時做到這一點。 非常感謝您的幫助。

謝謝

讓控制器從不同的容器中解析的原因之一是您的應用程序是否包含多個隔離的模塊。 在這種情況下,您可能不希望模塊相互影響,並且每個模塊都有一個容器是有意義的。 但是,在幾乎所有其他情況下,擁有多個容器實例沒有任何意義。

因此,如果需要此功能,則需要構建自己的自定義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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM