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;
}
The following code will cause this host to hang forever (if you try to stop your app by closing the main window)