Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScopedWindsorServiceProvider should implement IDisposable #33

Open
jdwoolcock opened this issue May 13, 2019 · 2 comments
Open

ScopedWindsorServiceProvider should implement IDisposable #33

jdwoolcock opened this issue May 13, 2019 · 2 comments
Milestone

Comments

@jdwoolcock
Copy link

Description
I am using Castle.Windsor.MsDependencyInjection with Microsoft.Extensions.Hosting HostBuilder. When the host is terminated the Windsor container is not disposed and the application fails to shutdown.

var host = new HostBuilder()
                .UseServiceProviderFactory(new WindsorServiceProviderFactory())
                .ConfigureContainer<IWindsorContainer>(hostContext, container) =>
                {
                                //Register Special Stuff
                })
                .RunConsoleAsync();

Proposed Solution

The following code changes resolve the issue

Make ScopedWindsorServiceProvider implement IDisposable

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

Dependencies
Microsoft.Extensions.Hosting 2.2
Castle.Windsor.MsDependencyInjection 3.3.1

@chrisheil
Copy link

@hikalkan Did you ever find a solution to this? I am having the same issue. I see the IDisposable proposal was rejected, but that still leaves an issue of the Windsor container not being disposed of properly.

@dquist
Copy link

dquist commented Feb 10, 2020

I solved this problem by creating a hosted service to manage the lifetime of the container. The container gets disposed on the ApplicationStopped event.

See: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1#ihostapplicationlifetime

Inject the IHostApplicationLifetime (formerly IApplicationLifetime) service into any class to handle post-startup and graceful shutdown tasks. Three properties on the interface are cancellation tokens used to register app start and app stop event handler methods. The interface also includes a StopApplication method.

public class WindsorHostedService : IHostedService
{
    private readonly IWindsorContainer _container;
    private readonly IHostApplicationLifetime _applicationLifetime;

    public WindsorHostedService(IWindsorContainer container, IHostApplicationLifetime applicationLifetime)
    {
        _container = container ?? throw new ArgumentNullException(nameof(container));
        _applicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime));
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _applicationLifetime.ApplicationStopped.Register(OnStopping);

        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }

    private void OnStopped()
    {
        _container.Dispose();
    }
}

@hikalkan hikalkan modified the milestones: v3.4, v3.5 Aug 19, 2020
@ismcagdas ismcagdas modified the milestones: v4.0, v4.2 Nov 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants