Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve scoped service from root provider when "ASPNETCORE_ENVIRONMENT": "Development"

I'm getting exception when trying to resolve service like this IApplicationBuilder.ApplicationServices.GetServices<AdminPanelDbContext>(); and "ASPNETCORE_ENVIRONMENT": "Development" is set to development.

Exception:

Cannot resolve scoped service 'AdminPanel.DAL.DbContexts.AdminPanel.AdminPanelDbContext' from root provider.

but when i set "ASPNETCORE_ENVIRONMENT": "Production" everything works fine.

I looked under appsettnings.Development.json and there is nothing different from appsettings.json. Is there any other setting that is affecting this?

like image 361
Genato Avatar asked Sep 24 '20 23:09

Genato


People also ask

What is aspnetcore_environment?

ASP.NET Core configures app behavior based on the runtime environment using an environment variable. ASP.NET Core reads the environment variable ASPNETCORE_ENVIRONMENT at app startup and stores the value in IHostingEnvironment.EnvironmentName. ASPNETCORE_ENVIRONMENT can be set to any value, but three values are provided by the framework:

How does ASP core determine the runtime environment of an app?

ASP.NET Core configures app behavior based on the runtime environment using an environment variable. To determine the runtime environment, ASP.NET Core reads from the following environment variables: ASPNETCORE_ENVIRONMENT when ConfigureWebHostDefaults is called. The default ASP.NET Core web app templates call ConfigureWebHostDefaults.

When does the aspnetcore_environment variable load?

The ASP.NET Core Configuration system automatically loads the ASPNETCORE_ENVIRONMENT variable during the application startup. It does so very early in the application (even before the creation of the host). Open the program.cs and you will see the following code. The purpose of the CreateHostBuilder is to build the host.

How do I set DotNet environment variable in ASP NET Core?

When the ASPNETCORE_ENVIRONMENT environment variable is set globally, it takes effect for dotnet run in any command window opened after the value is set. To set the ASPNETCORE_ENVIRONMENT environment variable with web.config, see the Setting environment variables section of ASP.NET Core Module.


1 Answers

See Dependency injection in ASP.NET Core - Scope validation:

When the app runs in the Development environment and calls CreateDefaultBuilder to build the host, the default service provider performs checks to verify that:

  • Scoped services aren't resolved from the root service provider.
  • Scoped services aren't injected into singletons.

[...]

Scoped services are disposed by the container that created them. If a scoped service is created in the root container, the service's lifetime is effectively promoted to singleton because it's only disposed by the root container when the app shuts down. Validating service scopes catches these situations when BuildServiceProvider is called.

For more information, see Scope validation.

This feature is new in ASP.NET Core v3. Previous versions of ASP.NET Core lacked this feature.

To me, the downside of this feature is that it is actually disabled by default when you run in production. It should have been on by default in all environments, because it will cause multi-threading issues and memory leaks.

like image 184
Steven Avatar answered Oct 17 '22 04:10

Steven