<legend id='hqdN4'><style id='hqdN4'><dir id='hqdN4'><q id='hqdN4'></q></dir></style></legend>

        <small id='hqdN4'></small><noframes id='hqdN4'>

      1. <tfoot id='hqdN4'></tfoot>
          <bdo id='hqdN4'></bdo><ul id='hqdN4'></ul>
        <i id='hqdN4'><tr id='hqdN4'><dt id='hqdN4'><q id='hqdN4'><span id='hqdN4'><b id='hqdN4'><form id='hqdN4'><ins id='hqdN4'></ins><ul id='hqdN4'></ul><sub id='hqdN4'></sub></form><legend id='hqdN4'></legend><bdo id='hqdN4'><pre id='hqdN4'><center id='hqdN4'></center></pre></bdo></b><th id='hqdN4'></th></span></q></dt></tr></i><div id='hqdN4'><tfoot id='hqdN4'></tfoot><dl id='hqdN4'><fieldset id='hqdN4'></fieldset></dl></div>

        ASP.Net Core 2 ServiceProviderOptions.ValidateScopes 属性

        ASP.Net Core 2 ServiceProviderOptions.ValidateScopes Property(ASP.Net Core 2 ServiceProviderOptions.ValidateScopes 属性)

        1. <small id='JHJxl'></small><noframes id='JHJxl'>

          <legend id='JHJxl'><style id='JHJxl'><dir id='JHJxl'><q id='JHJxl'></q></dir></style></legend>
            • <i id='JHJxl'><tr id='JHJxl'><dt id='JHJxl'><q id='JHJxl'><span id='JHJxl'><b id='JHJxl'><form id='JHJxl'><ins id='JHJxl'></ins><ul id='JHJxl'></ul><sub id='JHJxl'></sub></form><legend id='JHJxl'></legend><bdo id='JHJxl'><pre id='JHJxl'><center id='JHJxl'></center></pre></bdo></b><th id='JHJxl'></th></span></q></dt></tr></i><div id='JHJxl'><tfoot id='JHJxl'></tfoot><dl id='JHJxl'><fieldset id='JHJxl'></fieldset></dl></div>
                <tbody id='JHJxl'></tbody>

              <tfoot id='JHJxl'></tfoot>

                • <bdo id='JHJxl'></bdo><ul id='JHJxl'></ul>
                • 本文介绍了ASP.Net Core 2 ServiceProviderOptions.ValidateScopes 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Migrate html helpers to ASP.NET Core(将 html 帮助程序迁移到 ASP.NET Core)
                  Conditional validation in MVC.NET Core (RequiredIf)(MVC.NET Core 中的条件验证(RequiredIf))
                  Is it possible to serve static files from outside the wwwroot folder?(是否可以从 wwwroot 文件夹外部提供静态文件?)
                  Working with multiple resultset in .net core(在 .net 核心中使用多个结果集)
                  Where all types for http headers gone in ASP.NET 5?(ASP.NET 5 中所有类型的 http 标头都去了哪里?)
                  Failed to Authenticate HTTPS connection when attempting GET from WebAPI(尝试从 WebAPI 获取时无法验证 HTTPS 连接)
                  1. <legend id='BlVu1'><style id='BlVu1'><dir id='BlVu1'><q id='BlVu1'></q></dir></style></legend>

                  2. <i id='BlVu1'><tr id='BlVu1'><dt id='BlVu1'><q id='BlVu1'><span id='BlVu1'><b id='BlVu1'><form id='BlVu1'><ins id='BlVu1'></ins><ul id='BlVu1'></ul><sub id='BlVu1'></sub></form><legend id='BlVu1'></legend><bdo id='BlVu1'><pre id='BlVu1'><center id='BlVu1'></center></pre></bdo></b><th id='BlVu1'></th></span></q></dt></tr></i><div id='BlVu1'><tfoot id='BlVu1'></tfoot><dl id='BlVu1'><fieldset id='BlVu1'></fieldset></dl></div>
                    <tfoot id='BlVu1'></tfoot>

                      <small id='BlVu1'></small><noframes id='BlVu1'>

                          <tbody id='BlVu1'></tbody>
                          • <bdo id='BlVu1'></bdo><ul id='BlVu1'></ul>