asp.net-web-apidependency-injectioncastle-windsorlight-inject

What's the equivalent of CastleWindsor's container.Release in LightInject?


I saw ASP.NET Web API dependency injection in Seemann's site. It uses CastleWindsor though.

request.RegisterForDispose(
            new Release(
                () => this.container.Release(controller)));

What's the equivalent of CastleWindsor's container.Release in LightInject?

http://blog.ploeh.dk/2012/10/03/DependencyInjectioninASP.NETWebAPIwithCastleWindsor/


Solution

  • There is not really a Release method in LightInject. Disposable services are registered either with the PerScopeLifetime og the PerRequestLifetime.

    These services are disposed when the surrounding Scope is disposed.

    container.Register<IFoo, DisposableFoo>(new PerScopeLifetime())
    
    using(container.BeginScope())
    {
        var foo = container.GetInstance<IFoo>()
    } -- foo is disposed here
    

    LightInject.WebApi provides an integration for Web Api that takes care of disposing controllers when the web request ends.