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

This package causes a hang when using a hostbuilder in a WPF app #41

Open
tstedel opened this issue Nov 22, 2020 · 3 comments
Open

This package causes a hang when using a hostbuilder in a WPF app #41

tstedel opened this issue Nov 22, 2020 · 3 comments

Comments

@tstedel
Copy link

tstedel commented Nov 22, 2020

The following code will cause this host to hang forever (if you try to stop your app by closing the main window)

    public App()
    {
        Startup += App_Startup;
        Exit += App_Exit;
    }

    private async void App_Exit(object sender, ExitEventArgs e)
    {
        try
        {
            await this.host.StopAsync(TimeSpan.FromSeconds(5));
        }
        catch (Exception ex)
        {
            if (this.logger == null)
                Trace.TraceError("Error when stopping app: {0}", ex);
            else
                this.logger.LogError(ex, "Error when stopping app");
        }
        finally
        {
            this.host.Dispose();
            this.host = null;
        }
    }

    private async void App_Startup(object sender, StartupEventArgs e)
    {
        host = CreateHostBuilder().Build();

        this.logger = host.Services.GetService<ILogger<App>>();

        try
        {
            await this.host.StartAsync();

            var mainWindow = host.Services.GetRequiredService<MainWindow>();
            mainWindow.Show();
        }
        catch (Exception ex)
        {
            if (this.logger == null)
                Trace.TraceError("Error when starting app: {0}", ex);
            else
                this.logger.LogError(ex, "Error when starting app");

            Current.Shutdown(-1);
        }
    }

    public IHostBuilder CreateHostBuilder()
    {
        var hostBuilder = new HostBuilder()
            .UseServiceProviderFactory(new WindsorServiceProviderFactory());

        return hostBuilder;
    }
@maliming
Copy link
Member

hi

Can you confirm this is related to WindsorServiceProviderFactory?

@tstedel
Copy link
Author

tstedel commented Nov 23, 2020

WpfApp2.zip

If you run this application, and then close the main window, the application will never shut down.

If I use the official package located at https://www.nuget.org/packages/Castle.Windsor.Extensions.DependencyInjection/ the problem goes away. In this case, you can change the using statements.

@maliming
Copy link
Member

Now that there is the Castle.Windsor.Extensions.DependencyInjection package, maybe you can use it directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants