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

How can I use the new DI to inject an ILogger into an Azure Function using IWebJobsStartup?(如何使用新的 DI 将 ILogger 注入使用 IWebJobsStartup 的 Azure 函数?)
本文介绍了如何使用新的 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并将其直接传递到您配置的方法中.

Registeringservices

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 => {
    //    //...
    /

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)