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

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

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

        如何使用新的 DI 将 ILogger 注入使用 IWebJobsStartup 的 Azure 函数?

        时间:2023-10-05

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

            <bdo id='cY2Kk'></bdo><ul id='cY2Kk'></ul>
            <legend id='cY2Kk'><style id='cY2Kk'><dir id='cY2Kk'><q id='cY2Kk'></q></dir></style></legend>

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

              • <tfoot id='cY2Kk'></tfoot>
                  本文介绍了如何使用新的 DI 将 ILogger 注入使用 IWebJobsStartup 的 Azure 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Azure 函数 v2.这是我使用构造函数注入的函数:

                  I am using Azure Function v2. Here is my function that uses the constructor injection:

                  public sealed class FindAccountFunction
                  {
                      private readonly IAccountWorkflow m_accountWorkflow;
                  
                      private readonly IMapper m_mapper;
                  
                      private readonly ILogger m_logger;
                  
                      public FindAccountFunction(ILogger logger, IMapper mapper, IAccountWorkflow accountWorkflow)
                      {
                          m_logger = logger;
                          m_mapper = mapper;
                          m_accountWorkflow = accountWorkflow;
                      }
                  
                      [FunctionName("FindAccount")]
                      public async Task<IActionResult> Run(
                              [HttpTrigger(AuthorizationLevel.Function, Verbs.Get, Route = "v1/accounts/")] HttpRequest httpRequest, ILogger logger)
                      {
                          // Do stuff.
                      }
                  }
                  

                  我在从 IWebJobsStartup 派生的 Startup 类中声明我想要注入到 Azure 函数中的所有依赖项:

                  I am declaring all the dependencies that I want to inject into my Azure Function in the Startup class that derives from IWebJobsStartup:

                      public sealed class Startup : IWebJobsStartup
                      {
                          public void Configure(IWebJobsBuilder webJobsBuilder)
                          {
                              //  Registers the application settings' class.
                              webJobsBuilder.Services.AddSingleton<IApplicationSettings, ApplicationSettings>();
                  
                              //  ** Registers the ILogger instance **
                              //  ** ?? **
                  
                              //  Registers the IMapper instance for the contracts.
                              var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new MyProfile()));
                  
                       webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());
                  
                              // Registers custom services.
                              webJobsBuilder.Services.AddTransient<IStorageService, StorageService>();
                  
                              webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();
                          }
                     }
                  

                  Azure 函数调用其他也依赖于 ILogger 的注入服务,例如 IAccountWorkflow:

                  The Azure Function calls other injected services that do depends on the ILogger as well, such as the IAccountWorkflow:

                  public sealed class AccountWorkflow : IAccountWorkflow
                  {  
                      public AccountWorkflow(ILogger logger, IStorageService storageService)
                      {
                          if(logger is null)
                              throw new ArgumentNullException();
                      }
                  }
                  

                  问题在于 DI 无法找到任何 ILogger 实现并且无法解析服务,因为注入了一个空的 ILogger.

                  The problem is that the DI is unable to find any ILogger implementation and fails to resolve services since a null ILogger is injected.

                  问题

                  如何在 IWebJobsStartup 中设置 ILogger 的注入?

                  How can I setup the injection of the ILogger in IWebJobsStartup?

                  推荐答案

                  更新

                  参考 使用依赖注入在 .NET Azure 函数中

                  要注册服务,您可以创建配置方法并将组件添加到 IFunctionsHostBuilder实例.Azure Functions 主机创建一个 IFunctionsHostBuilder并将其直接传递到您配置的方法中.

                  Registering services

                  To register services, you can create a configure method and add components to an IFunctionsHostBuilder instance. The Azure Functions host creates an IFunctionsHostBuilder and passes it directly into your configured method.

                  要注册你的配置方法,你必须添加一个程序集属性指定您的配置方法的类型,使用FunctionsStartup 属性.

                  To register your configure method, you must add an assembly attribute that specifies the type for your configure method using the FunctionsStartup attribute.

                  所以在这种情况下

                  [assembly: FunctionsStartup(typeof(MyNamespace.Startup))]    
                  namespace MyNamespace {
                      public class Startup : FunctionsStartup {
                          public override void Configure(IFunctionsHostBuilder builder) {
                              //  ** Registers the ILogger instance **
                              builder.Services.AddLogging();
                  
                              //  Registers the application settings' class.
                              //...
                  
                              //...omitted for brevity    
                          }
                      }
                  }
                  

                  原创

                  我相信既然您可以访问服务集合,您应该可以向它添加日志记录

                  ORIGINAL

                  I believe since you have access to the service collection, you should be able to add logging to it

                  public void Configure(IWebJobsBuilder webJobsBuilder) {       
                  
                      //  ** Registers the ILogger instance **
                      webJobsBuilder.Services.AddLogging();
                  
                      //OR
                      //webJobsBuilder.Services.AddLogging(builder => {
                      //    //...
                      /
                1. <i id='aU0d2'><tr id='aU0d2'><dt id='aU0d2'><q id='aU0d2'><span id='aU0d2'><b id='aU0d2'><form id='aU0d2'><ins id='aU0d2'></ins><ul id='aU0d2'></ul><sub id='aU0d2'></sub></form><legend id='aU0d2'></legend><bdo id='aU0d2'><pre id='aU0d2'><center id='aU0d2'></center></pre></bdo></b><th id='aU0d2'></th></span></q></dt></tr></i><div id='aU0d2'><tfoot id='aU0d2'></tfoot><dl id='aU0d2'><fieldset id='aU0d2'></fieldset></dl></div>
                    <legend id='aU0d2'><style id='aU0d2'><dir id='aU0d2'><q id='aU0d2'></q></dir></style></legend>
                      <bdo id='aU0d2'></bdo><ul id='aU0d2'></ul>
                            <tbody id='aU0d2'></tbody>

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

                        1. <tfoot id='aU0d2'></tfoot>