Ask questionsIssue with BuildServiceProvider() in ConfigureServices()

<!-- Describe what isn't working as expected --> When setting up the database in the ConfigureServices() function there's a warning that says:

Calling 'BuildServiceProvider' from application code results in an additional copy of singleton services being created. Consider alternatives such as dependency injecting services as parameters to 'Configure'.

Steps to reproduce

Here's following the piece of code that's references to that warning:

    .AddDbContext<AppDbContext>(options =>

Further technical details

EF Core version: 3.0 Database provider: PostgreSQL Target framework: .NET Core 3.0 Operating system: Windows 10 x64 IDE: Visual Studio 2019 16.3.1

Is there any way to fix it?


Answer questions davidfowl


@davidfowl, if I try to use dependency injection, the IOptions<AppSettings> is null. I could try to resolve it the static methods, but I would need to resolve it 3 different times, once for each static call.

It can't be null. Can you show me exactly what code you wrote and what exactly is null?

Take one of those methods and make a minimal example of the pattern you're using.


Related questions

HTTP Error 500.31 - ANCM Failed to Find Native Dependencies hot 8
MissingMethodException: Method not found Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder
Migrate Asp.Net Core 2.2 Integration Tests to 3.0 - The TestServer constructor was not called with a IWebHostBuilder so IWebHost is not available. hot 4
UseStaticFiles with StaticFileOptions/FileExtensionContentTypeProvider breaks server side Blazor hot 3
SignalR .NetCore 3.0-- HubConnectionBuilder' does not contain a definition for 'WithUrl' hot 3
HTTP Error 500.30 - ANCM In-Process Start Failure hot 3
With core 3.0 SignInManager.IsSignedIn(User) allways reuturn false?but in 2.2 it works fine.Is that a debug? hot 3
CSS Isolation in Blazor Components hot 3
How to retrieve the HttpContext from Blazor server-side ? hot 3
Oauth2 authentication erroring with "The oauth state was missing or invalid." hot 3
Failed to gracefully shutdown application (IIS Hang) hot 3
BadHttpRequestException: Reading the request body timed out due to data arriving too slowly hot 2
HTTP Error 500.0 - ANCM In-Process Handler Load Failure hot 2
Method not found: 'Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasIndex(System.Linq.Expressions.Expression`1<System.Func`2<System.__Canon,System.Object>>)'. hot 2
Github User Rank List