问题描述
ServiceProviderOptions.ValidateScopes 究竟是什么?我觉得我无法完全理解它在引擎盖下的作用.我在教程中遇到过这个问题,但没有解释.
What is ServiceProviderOptions.ValidateScopes exactly? I feel I couldn't understand completely what it does under the hood. I've come across this with a tutorial, but no explanation.
推荐答案
我假设你说的是这段代码:
I assume you are speaking about this piece of code:
services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateScopes = true
});
// or
services.BuildServiceProvider(true); // or false
?
ASP.NET Core Provider 有一个机制,可以验证作用域服务是否由单例容器解析.ASP.NET Core 有两种容器.在应用程序的生命周期内有效的主单例容器和每个请求的作用域容器.
The ASP.NET Core Provider has a mechanic which validates if a scoped service is resolved by a singleton container. ASP.NET Core has two kinds of containers. The main, singleton container which is valid for the life-time of the application and scoped containers for every request.
此选项将阻止从单例容器解析作用域服务,也就是说,如果您不小心尝试在 Configure
方法中解析作用域服务,您将收到异常.而如果你禁用它,你不应该这样做.
This option will prevent resolving of scoped services from the singleton container, that is if you accidentally try to resolve a scoped service within Configure
method, you will get an exception. Whereas if you disable it you shouldn't.
public void Configure(IApplicationBuilder app)
{
// will throw exception, since by default DbContext is registered as scope
app.ApplicationServices.GetRequiredService<MyDbContext>();
}
异常类似于
InvalidOperationException:无法从根提供程序解析IExampleService",因为它需要范围服务MyDbContext".
InvalidOperationException: Cannot resolve 'IExampleService' from root provider because it requires scoped service 'MyDbContext'.
这种行为是为了防止内存泄漏并从单例容器中解析范围服务(应该是短暂的),本质上使该服务也成为准单例(因为它们不会直到容器被释放并且单例容器仅在应用程序关闭时才被释放).
This behavior is there to prevent memory leaks and resolving scoped services (which are supposed to be short-lived) from singleton container, essentially making this services quasi-singletons too (because they won't get disposed until the container gets disposed and the singleton container only gets disposed when the application is shut down).
在 Configure
方法中解析作用域服务的正确方法是这样
The correct way to resolve scoped services within i.e. Configure
method is this
// get scoped factory
var scopedFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
// create a scope
using (var scope = scopedFactory.CreateScope())
{
// then resolve the services and execute it
var context = scope.ServiceProvider.GetRequiredService<MyDbContext>();
}
// here, the child (scoped) container will be disposed and all scoped and transient services from it
2020 年 12 月更新:.NET 5.0 的默认值现在是 false
.
Update 12/2020: The default value is now false
for .NET 5.0.
~~默认值是 true
~~ 除非你确切地知道你在做什么,否则你应该保持这样.
~~The default value is true
~~ and you should leave it like that unless you know exactly what you are doing otherwise you risk nasty memory leaks (or object already disposed exceptions) by unreleased services.
这篇关于ASP.Net Core 2 ServiceProviderOptions.ValidateScopes 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!