Skip to content

System.InvalidOperationException: Unable to build IHost when creating EF Migration with Minimal API dotnet 6 #60891

@IanBuck-dev

Description

@IanBuck-dev

Description

I have an issue when creating ef migrations for the newest release candidate 6.0.0-rc.2.21480.5 for dotnet 6 with the minimal apis. It seems to be a similar issue as explained here dotnet/aspnetcore#33886

The timeout for creating a WebApplicationFactory at design time seems to be 5 sec and that is not enough for me when I add new migrations, because I need to setup a lot of configuration before. I get this exception when trying to run any command that requires the project to be build like dotnet ef migrations add AddedPlanningSessions -v and the DbContext to be fetched from the service provider. I would expect there to be no timeout at all or at least a very large one when using WebApplicationFactory at design time.

System.InvalidOperationException: Unable to build IHost
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost()
   at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass8_0.<ResolveHostFactory>b__0(String[] args)
   at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass11_0.<ResolveServiceProviderFactory>b__3(String[] args)
   at Microsoft.EntityFrameworkCore.Design.Internal.AppServiceProviderFactory.CreateFromHosting(String[] args)

The issue can be easily reproduced by creating an empty project with minimal api dotnet new web -o service, adding a dbcontext to the DI and adding a sleep of 6 sec.

Include your code

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<ApplicationDbContext>(
    options => options.UseSqlServer("localhost"));

Thread.Sleep(TimeSpan.FromSeconds(6));

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

Include verbose output

Output when building my project where the steps until builder.Build() take too long.

dotnet exec --depsfile /Users/ian/Repos/work/planner-service/service/bin/Debug/net6.0/service.deps.json --additionalprobingpath /Users/ian/.nuget/packages --additionalprobingpath /usr/local/share/dotnet/sdk/NuGetFallbackFolder --runtimeconfig /Users/ian/Repos/work/planner-service/service/bin/Debug/net6.0/service.runtimeconfig.json /Users/ian/.dotnet/tools/.store/dotnet-ef/6.0.0-rc.2.21480.5/dotnet-ef/6.0.0-rc.2.21480.5/tools/netcoreapp3.1/any/tools/netcoreapp2.0/any/ef.dll dbcontext info --assembly /Users/ian/Repos/work/planner-service/service/bin/Debug/net6.0/service.dll --project /Users/ian/Repos/work/planner-service/service/service.csproj --startup-assembly /Users/ian/Repos/work/planner-service/service/bin/Debug/net6.0/service.dll --startup-project /Users/ian/Repos/work/planner-service/service/service.csproj --project-dir /Users/ian/Repos/work/planner-service/service/ --root-namespace PlannerService --language C# --framework net6.0 --nullable --working-dir /Users/ian/Repos/work/planner-service/service --verbose
Using assembly 'service'.
Using startup assembly 'service'.
Using application base '/Users/ian/Repos/work/planner-service/service/bin/Debug/net6.0'.
Using working directory '/Users/ian/Repos/work/planner-service/service'.
Using root namespace 'PlannerService'.
Using project directory '/Users/ian/Repos/work/planner-service/service/'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'service'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Loading config from: global, planner-service.
System.InvalidOperationException: Unable to build IHost
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost()
   at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass8_0.<ResolveHostFactory>b__0(String[] args)
   at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass11_0.<ResolveServiceProviderFactory>b__3(String[] args)
   at Microsoft.EntityFrameworkCore.Design.Internal.AppServiceProviderFactory.CreateFromHosting(String[] args)
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Unable to build IHost
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'PlannerContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'PlannerContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[PlannerService.DAL.PlannerContext]' while attempting to activate 'PlannerService.DAL.PlannerContext'.
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13()
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.GetContextInfo(String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.GetContextInfoImpl(String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.GetContextInfo.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create an object of type 'PlannerContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Include provider and version information

EF Core version: 6.0.0-rc.2.21480.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer 6.0.0-rc.2.21480.5
Target framework: NET 6.0
Operating system:
IDE: VS Code Version: 1.61.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-HostingenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions