Skip to content

[WebToolsE2E][Aspire]After adding 'Aspire.Npgsql.EntityFrameworkCore.PostgreSQL' component and updating related codes, F5 failed with an exception: 'Some services are not able to be constructed ...' #744

@v-cazhang

Description

@v-cazhang

INSTALL STEPS

  1. Clean machine: Win11 x64 23h2 ENU
  2. Install VS 17.9 P1 from build [34307.189.d17.9]
  3. Apply NuGet Feeds

REPRO STEPS

  1. In the VS, create a new project > ASP.NET Core Web App(Razor Page) > .NET 8.0 > check 'Enlist in Aspire Orchestration' > Create
  2. Right-click the Models folder > Add > Class, named Movie.cs and paste following codes:
	public class Movie
	{
	    public int ID { get; set; }
	    public string Title { get; set; } = string.Empty;
	    public DateTime ReleaseDate { get; set; }
	    public string Genre { get; set; } = string.Empty;
	    public decimal Price { get; set; }
	}
  1. Right-click the Controllers folder > add > Controller > Mvc Controller with views, using Entity Framework:

    • Model class: Movies.cs
    • DbContext class: click '+' to create a new db context
    • Database provider: select PstgreSQL
  2. Right-click the razor project > Add > Aspire component > Aspire.Npgsql.EntityFrameworkCore.PostgreSQL using matched version.

  3. Open appsettings.json file under Razor project, type following codes:

"Aspire": {
	    "Npgsql": {
	      "EntityFrameworkCore": {
	        "PostgreSQL": {
	          "DbContextPooling": true,
	          "HealthChecks": false,
	          "Tracing": false
	        }
	      }
	    }
	  }
  1. Then open Program.cs of razor project, type following codes and replace with the DbContext created in step 3:
    builder.AddNpgsqlDbContext<MyDbContext>("myConnection");
  2. Open appsettings.json of xxx.AppHost project, add following codes and replace with the connection string created in step 3:
"ConnectionStrings": {
	        "myConnection": "Host=myserver;Database=test"
	      }
  1. Open Program.cs of xxx.AppHost project, add following codes:
var postgresdb = builder.AddPostgresContainer("pg").AddDatabase("postgresdb");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(postgresdb);
  1. F5

ACTUAL
F5 failed with an exception
image
System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.Internal.IDbContextPool1[WebApplication4.Data.WebApplication4Context] Lifetime: Singleton ImplementationType: Microsoft.EntityFrameworkCore.Internal.DbContextPool1[WebApplication4.Data.WebApplication4Context]': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[WebApplication4.Data.WebApplication4Context]' from singleton 'Microsoft.EntityFrameworkCore.Internal.IDbContextPool1[WebApplication4.Data.WebApplication4Context]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.Internal.IScopedDbContextLease1[WebApplication4.Data.WebApplication4Context] Lifetime: Scoped ImplementationType: Microsoft.EntityFrameworkCore.Internal.ScopedDbContextLease1[WebApplication4.Data.WebApplication4Context]': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[WebApplication4.Data.WebApplication4Context]' from singleton 'Microsoft.EntityFrameworkCore.Internal.IDbContextPool1[WebApplication4.Data.WebApplication4Context]'.)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
at Program.

$(String[] args) in C:\Users\v-cazhang\source\repos\WebApplication4\WebApplication4\Program.cs:line 14

This exception was originally thrown at this call stack:
[External Code]

Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.Internal.IDbContextPool1[WebApplication4.Data.WebApplication4Context] Lifetime: Singleton ImplementationType: Microsoft.EntityFrameworkCore.Internal.DbContextPool1[WebApplication4.Data.WebApplication4Context]': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[WebApplication4.Data.WebApplication4Context]' from singleton 'Microsoft.EntityFrameworkCore.Internal.IDbContextPool1[WebApplication4.Data.WebApplication4Context]'.

Inner Exception 2:
InvalidOperationException: Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[WebApplication4.Data.WebApplication4Context]' from singleton 'Microsoft.EntityFrameworkCore.Internal.IDbContextPool1[WebApplication4.Data.WebApplication4Context]'.

EXPECTED
Running the solution succeed and can show the dashboard.

Metadata

Metadata

Assignees

Labels

area-integrationsIssues pertaining to Aspire Integrations packages

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions