简体   繁体   English

使用OWIN启动类时,如何在注册依赖项后处置Castle Windsor容器?

[英]When using an OWIN startup class, how do I dispose of my Castle Windsor container after registering my dependencies?

I was having issues whereby my container was not being called in when registered in my global.asax, so I thought that this could be because I am now using an OWIN startup class. 我遇到了在我的global.asax中注册时没有调用我的容器的问题,所以我认为这可能是因为我现在正在使用OWIN启动类。 I've moved the instantiation of the container to this class now. 我现在已经将容器的实例化移动到了这个类。 I'm wondering whether this is appropriate to do: 我想知道这是否合适:

[assembly: OwinStartup(typeof(Startup))]
namespace Namespace.WebApi

public class Startup : IDisposable 
{
    private readonly WindsorContainer _Container;
    public Startup()
    {
        _Container = new WindsorContainer();
        _Container.Install(FromAssembly.Named("Namespace.WebApi.Installers"));
    }

    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();
        config.Services.Replace(typeof(IHttpControllerActivator),
            new WindsorHttpControllerActivator(_Container));
        /// other configuration
    }

    public void Dispose()
    {
        _Container.Dispose();
    }
}

You can include the Owino NuGet package and use the RegisterForDisposal extension like this: 您可以包含Owino NuGet包并使用RegisterForDisposal扩展名,如下所示:

public void Configuration(IAppBuilder app)
{
    /// (...)

    app.RegisterForDisposal(_Container);
}

or you can look at how the method is implemented and make your own implementation. 或者你可以看看如何实现该方法并进行自己的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM