profile
viewpoint

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:

services.AddEntityFrameworkNpgsql()
    .AddDbContext<AppDbContext>(options =>
   options.UseNpgsql(Configuration.GetConnectionString(EnvironmentContants.ConnectionString)))
    .BuildServiceProvider();

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?

aspnet/AspNetCore

Answer questions davidfowl

@Frankenleg

@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.

useful!

Related questions

HTTP Error 500.31 - ANCM Failed to Find Native Dependencies hot 523
ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY when clean webapi template hot 505
Restore MvcJsonOptions to ASP.NET Core 3.0 as a type forward? hot 420
The SPA default page middleware could not return the default page '/index.html' in production application hot 310
HTTP Error 500.30 - ANCM In-Process Start Failure hot 282
HTTP Error 500.30 - ANCM In-Process Start Failure hot 249
Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing. To continue using 'UseMvc', please set 'MvcOptions.EnableEndpointRounting = false' inside 'ConfigureServices'. hot 241
Calling Request.EnableRewind throw on 3.0.0-preview7 hot 219
ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY when clean webapi template hot 206
DataProtection - CryptographicException when using EntityFramework as Key Storage Provider hot 181
error CS1503: Argument 2: cannot convert from 'method group' to 'EventCallback' hot 160
Error: Server returned handshake error: Handshake was canceled. hot 152
Blazor - rzc generate exited with code 1. DotNet Core 3.0.100-preview8-013656 hot 150
IIS in-process hosting incorrectly throws ConnectionResetException for HTTP POST hot 149
NullReferenceException in IISHttpContext hot 135
Github User Rank List