【问题标题】:How to dispose of Autofac container?如何处理 Autofac 容器?
【发布时间】:2014-11-11 00:15:31
【问题描述】:

我有一个静态类来设置 Autofac 注册,它的方法在 Application_Start 中调用。像这样的:

public static class RegisterAutofac
{
    public static void Setup()
    {
        var config = GlobalConfiguration.Configuration;
        var builder = new ContainerBuilder();

        //Do registration here...

        var container = builder.Build();
        var resolver = new AutofacWebApiDependencyResolver(container);

        GlobalConfiguration.Configuration.DependencyResolver = resolver;
    }
}

到目前为止,我还没有找到处理容器的示例。

这够了吗,还是我应该退回容器,然后在 Global.asax 的 Dispose 方法中处理容器?

【问题讨论】:

    标签: c# asp.net-web-api autofac


    【解决方案1】:

    在这种情况下,您不需要调用它,因为 AutofacWebApiDependencyResolver 已经是 IDisposable 并且接收容器作为依赖项。当您处理 AutofacWebApiDependencyResolver

    时,它正在调用 Dispose

    AutofacWebApiDependencyResolver 将由系统自动释放,因为 Configuration 中的 HttpConfiguration 对象已经释放了它。

    希望对你有帮助。

    【讨论】:

    • 没有。我是否应该从此方法返回解析器并将其丢弃在 global.asax 中?
    • 我认为问题在于如何以及是否有必要处置 container 本身
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 2020-05-29
    相关资源
    最近更新 更多