<bdo id='vTTo0'></bdo><ul id='vTTo0'></ul>

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

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

      <tfoot id='vTTo0'></tfoot>

      1. <legend id='vTTo0'><style id='vTTo0'><dir id='vTTo0'><q id='vTTo0'></q></dir></style></legend>

        在配置依赖注入之后,但在第一个 http 请求之前初始化一个单例

        Initialize a singleton after configuring dependency injection, but before the first http request(在配置依赖注入之后,但在第一个 http 请求之前初始化一个单例)
          <tfoot id='lcHup'></tfoot>

        • <legend id='lcHup'><style id='lcHup'><dir id='lcHup'><q id='lcHup'></q></dir></style></legend>

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

            <bdo id='lcHup'></bdo><ul id='lcHup'></ul>

              <tbody id='lcHup'></tbody>

              <i id='lcHup'><tr id='lcHup'><dt id='lcHup'><q id='lcHup'><span id='lcHup'><b id='lcHup'><form id='lcHup'><ins id='lcHup'></ins><ul id='lcHup'></ul><sub id='lcHup'></sub></form><legend id='lcHup'></legend><bdo id='lcHup'><pre id='lcHup'><center id='lcHup'></center></pre></bdo></b><th id='lcHup'></th></span></q></dt></tr></i><div id='lcHup'><tfoot id='lcHup'></tfoot><dl id='lcHup'><fieldset id='lcHup'></fieldset></dl></div>
                • 本文介绍了在配置依赖注入之后,但在第一个 http 请求之前初始化一个单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用的是 Azure Functions 版本 2.x.它具有对依赖注入的内置支持.

                  I am using Azure Functions version 2.x. It has built-in support for dependency injection.

                  所以我可以使用以下方法在单例范围内为 DI 注册我的服务 IMyService:

                  So I can register my service IMyService for DI at singleton scope using:

                  public class Startup : FunctionsStartup
                  {
                      public override void Configure(IFunctionsHostBuilder builder)
                      {
                          builder.Services.AddSingleton<IOther, DefaultOther>();
                          builder.Services.AddSingleton<IMyService, DefaultMyService>();  // IMyService depends on IOther.
                      }
                  }
                  

                  DefaultMyService 的实例在函数第一次执行时创建.这意味着第一个请求较慢,因为它会在 DefaultMyService 内部进行大量初始化(它填充缓存等).

                  An instance of DefaultMyService is created the first time the function gets executed. This means the first request is slower because it does heavy initialization that happens inside DefaultMyService (it populates cache, etc.).

                  问题:有没有办法让 DefaultMyService 在第一次请求之前创建?

                  Question: Is there a way to have DefaultMyService created earlier than the first request?

                  向 asp.net 询问了 类似问题核心,那里的答案提出了一些解决方案,但它们都不适用于函数应用程序的上下文:

                  A similar question was asked for asp.net core, and the answers there suggests a few solutions, but none of them works in the context of a function app:

                  选项1:创建我的服务的实例(初始化发生在这里),然后注册实例(而不是注册类型)

                  Option 1: Create an instance of my service (initialization happens here), and then register the instance (instead of registering the type)

                  var foo = new Foo();
                  services.AddSingleton<IFoo>(foo);
                  

                  这不起作用,因为在我的情况下 IMyService 依赖于其他服务,这些服务在我在 Configure 中注册 IMyService 时未实例化 方法.它失败并出现 此处描述的错误.

                  This doesn't work because in my case IMyService depends on other services, which are not instantiated at the time when I am registering IMyService in the Configure method. It fails with an error that's described here.

                  选项 2:其他建议是使用重载的配置方法:

                  Option 2: Other suggestion is to use overloaded Configure method:

                  public void Configure(IApplicationBuilder app, IFoo foo) 
                  {
                      ...
                  }
                  

                  这也不起作用,因为在函数应用的情况下,唯一执行的配置方法是 Configure(IFunctionsHostBuilder builder),并且不会调用其他重载.

                  This also doesn't work because in case of function app, the only configure method that gets executed is Configure(IFunctionsHostBuilder builder), and other overloads are not called.

                  推荐答案

                  因为它会在 DefaultMyService 内部进行大量初始化

                  because it does heavy initialization that happens inside DefaultMyService

                  这就是问题的核心所在.正如 Mark Seemann here 解释的那样,注入构造函数不应该再检查 null并存储传入的依赖项.任何时候你在构造函数中进行任何 I/O 或调用类的依赖项内部,你都会遇到麻烦.

                  This is where the core of the problem lies. As Mark Seemann explained here, Injection constructors should do nothing more checking for null and storing incoming dependencies. Any time you do any I/O or invoke the class's dependencies inside the constructor, you'll get in trouble.

                  您的问题似乎类似于 this q/a,我的建议是一样的:从构造函数中提取初始化逻辑,然后执行以下操作:

                  Your question seems similar to this q/a, and my advise would be the same: extract the initialization logic out of the constructor and either do the following:

                  • 在连接对象图之前进行初始化,并为 DI 配置提供初始化对象,或者
                  • 在注册阶段之后、第一次请求之前直接解析和调用对象图,以使数据可以初始化.

                  这篇关于在配置依赖注入之后,但在第一个 http 请求之前初始化一个单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                  set equality in linq(在 linq 中设置相等)
                  HashSet conversion to List(HashSet 转换为 List)
                  How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                  Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                  How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)

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

                    <bdo id='7Jnrw'></bdo><ul id='7Jnrw'></ul>
                      <tbody id='7Jnrw'></tbody>

                      • <small id='7Jnrw'></small><noframes id='7Jnrw'>

                          <legend id='7Jnrw'><style id='7Jnrw'><dir id='7Jnrw'><q id='7Jnrw'></q></dir></style></legend>