Skip to content

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

Description

@tstedel

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;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions