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'.
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();
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.