question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with BuildServiceProvider() in ConfigureServices()

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
Smoothjedicommented, Oct 9, 2019

I’m also getting this message when attempting to use AddSwaggerGen:

services.AddSwaggerGen((options) =>
{
	var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
	...
}

All the documentation/examples I’ve seen for this online uses this code. Is there now a better way to do it?

1reaction
davidfowlcommented, Oct 24, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found