efcore: The host was aborted exception when adding new migration

In the latest versions of the dotnet-ef cli (7.0.0 and 7.0.1) whenever I want to add a new migration, the process gets done properly, but also it shows me an exception as bellow

An error occurred when starting up the Application.
Microsoft.Extensions.Hosting.HostAbortedException: The host was aborted.
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.ThrowHostAborted()
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.OnNext(KeyValuePair`2 value)
   at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
   at Microsoft.Extensions.Hosting.HostBuilder.ResolveHost(IServiceProvider serviceProvider, DiagnosticListener diagnosticListener)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args) in ...\Program.cs:line 17

And the following is my code in Program.cs

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

    var config = new Startup();
    config.ConfigureServices(builder.Configuration, builder.Services);

    var app = builder.Build(); // Here is the line 17 which is mentioned in the stacktrace
    config.Configure(app);

    app.Run();
}
catch (Exception ex)
{
    string type = ex.GetType().Name;
    if (type.Equals("StopTheHostException", StringComparison.Ordinal))
    {
        throw;
    }

    Log.Fatal(ex, "An error occurred when starting up the Application.");
}

The command that I run to create the migration is:

dotnet ef migrations add MigrationName
  • Note 1: I have created the project using the Visual Studio templates
  • Note 2: I have another project with exactly the same structure but with the dotnet version 6.0.0, it doesn’t have this issue
  • Note 3: The expected result (adding new migration) gets done properly, but the exception makes confusions sometimes
  • Note 4: The exception raises also in migration removal

EF Core version: 7.0.0 and 7.0.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 Pro IDE: Visual Studio 2022 17.4.0)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (5 by maintainers)

Most upvoted comments

In case you wrap the var app = builder.Build(); in a try/catch block you have to make sure to not catch the HostAbortedException that is thrown by Entity Framework.

For example:

try
{
	var builder = WebApplication.CreateBuilder(args);
	// ...
	var app = builder.Build();
	// ...
	app.Run();
	return 0;
}
catch (Exception ex) when (ex is not HostAbortedException && ex.Source != "Microsoft.EntityFrameworkCore.Design") // see https://github.com/dotnet/efcore/issues/29923
{
	_logger.Fatal(ex, "Web host terminated unexpectedly");
	return 1;
}

Is there any chance for a better-looking fix? That would ask questions during Code Review and could confuse other devs.

@ajcvickers I have the same problem as jwbats, the program is started even though I have a DesignTimeDbContextFactory for the DbContext in question.

Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'BedaDbContextFactory'.
Found DbContext 'BedaDbContext'.
Finding application service provider in assembly 'GR.Integrations.Beda'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.

Microsoft.Extensions.Hosting.HostAbortedException: The host was aborted.
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.ThrowHostAborted()
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.OnNext(KeyValuePair`2 value)
   at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
   at Microsoft.Extensions.Hosting.HostBuilder.ResolveHost(IServiceProvider serviceProvider, DiagnosticListener diagnosticListener)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()

I hit this problem when I had my

var host = builder.Build();
            host.Run(); 

code in try/catch block.

@ajcvickers I am now experiencing this as well.

Upon adding a migration, EF Core says it’s using both:

Using DbContext factory 'DesignTimeDbContextFactory'.
Using context 'MyApplicationContext'.

I have a DesignTimeDbContextFactory. The whole idea of having this is to ensure that EF can add migrations without running the program, isn’t it?

Then why is it starting my app, anyway?

My program.cs indeed starts hosted services, but the exception doesn’t say which one it was that got aborted.

EF didn’t always do this. What caused this change in behavior?