Tests fail only when runned in parallel

Viewed 172

Running tests together causes on random ones System.InvalidOperationException : The entry point exited without ever building an IHost.

When debugging everything is Ok, no exception is thrown problem appeared after migrating to .net 6 new hosting model, but with old Startup.cs I found a similar issue caused by serilog, but this is not my case

Program.cs

Log.Logger = new LoggerConfiguration().CreateBootstrapLoggerWithDefaults();

try
{
    var builder = WebApplication.CreateBuilder(args);

    builder.Services.AddRazorPages();
    builder.Services.AddServerSideBlazor();

    builder.Services.AddSharedServices(builder.Configuration, builder.Environment.IsDevelopment());
    builder.Services.Configure<UiUrls>(builder.Configuration.GetSection(nameof(UiUrls)));

    builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
    builder.Host.ConfigureContainer<ContainerBuilder>((ctx, containerBuilder) =>
    {
        containerBuilder.RegisterShared(builder.Configuration);

    });
    builder.Host.UseLogDefaults();

    var app = builder.Build();

    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        app.UseAzureAppConfiguration();
    }

    app.UseStaticFiles();
    app.UseMediatorEntityExistence()
        .UseMediatorValidation();
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
    });

    app.Run();
}
catch (Exception e) when (e is not OperationCanceledException && e.GetType().Name != "StopTheHostException")
{
    Log.Fatal(e, "Failed to start");
}
finally
{
    Log.CloseAndFlush();
}
1 Answers
Related