Issue with BuildServiceProvider() in ConfigureServices()
See original GitHub issueWhen 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?
Issue Analytics
- State:
- Created 4 years ago
- Comments:22 (11 by maintainers)
Top Results From Across the Web
Calling 'BuildServiceProvider' from application code results ...
ASP.NET Core will build the ServiceProvider automatically at runtime, therefore don't call the BuildServiceProvider() manually. I read Adam ...
Read more >ASP0000: Do not call 'IServiceCollection. ...
Rule description. Calling 'BuildServiceProvider' from application code results in more than one copy of singleton services being created which ...
Read more >Calling BuildServiceProvider from application code results ...
ConfigureServices(). Solution. First, the options pattern is a good way to solve the “fetch a dynamic value using a registered service” problem, ...
Read more >Access services inside ConfigureServices using ...
This post shows how you can inject services when configuring IOptions implementations in your Startup class using the IConfigureOptions ...
Read more >C# – Calling 'BuildServiceProvider' from application code ...
I thinks correct version is to use ApplicationServices property of app, which app is IApplicationBuilder in Configure method's parameter.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m also getting this message when attempting to use AddSwaggerGen:
All the documentation/examples I’ve seen for this online uses this code. Is there now a better way to do it?
You can suppress the warning in that case. It’s an anti pattern in general. You can do it if you understand what exactly is happening and if you’re ok with it but there’s no support for building the container while trying to build it. What I mean is you have 2 containers and one of them will never be disposed.
PS: it was always an issue, we just warn you about it in 3.0