-
Notifications
You must be signed in to change notification settings - Fork 697
Description
INSTALL STEPS
- Clean machine: Win11 x64 23h2 ENU
- Install VS 17.9 P1 from build [34307.189.d17.9]
- Apply NuGet Feeds
REPRO STEPS
- In the VS, create a new project > ASP.NET Core Web App(Razor Page) > .NET 8.0 > check 'Enlist in Aspire Orchestration' > Create
- 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; }
}
-
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
-
Right-click the razor project > Add > Aspire component > Aspire.Npgsql.EntityFrameworkCore.PostgreSQL using matched version.
-
Open appsettings.json file under Razor project, type following codes:
"Aspire": {
"Npgsql": {
"EntityFrameworkCore": {
"PostgreSQL": {
"DbContextPooling": true,
"HealthChecks": false,
"Tracing": false
}
}
}
}
- Then open Program.cs of razor project, type following codes and replace with the DbContext created in step 3:
builder.AddNpgsqlDbContext<MyDbContext>("myConnection");
- 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"
}
- 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);
- F5
ACTUAL
F5 failed with an exception
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.DbContextPool
1[WebApplication4.Data.WebApplication4Context]': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[WebApplication4.Data.WebApplication4Context]' from singleton 'Microsoft.EntityFrameworkCore.Internal.IDbContextPool
1[WebApplication4.Data.WebApplication4Context]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.Internal.IScopedDbContextLease1[WebApplication4.Data.WebApplication4Context] Lifetime: Scoped ImplementationType: Microsoft.EntityFrameworkCore.Internal.ScopedDbContextLease
1[WebApplication4.Data.WebApplication4Context]': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[WebApplication4.Data.WebApplication4Context]' from singleton 'Microsoft.EntityFrameworkCore.Internal.IDbContextPool
1[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.
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.DbContextPool
1[WebApplication4.Data.WebApplication4Context]': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[WebApplication4.Data.WebApplication4Context]' from singleton 'Microsoft.EntityFrameworkCore.Internal.IDbContextPool
1[WebApplication4.Data.WebApplication4Context]'.
Inner Exception 2:
InvalidOperationException: Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[WebApplication4.Data.WebApplication4Context]' from singleton 'Microsoft.EntityFrameworkCore.Internal.IDbContextPool
1[WebApplication4.Data.WebApplication4Context]'.
EXPECTED
Running the solution succeed and can show the dashboard.