问题描述
我正在使用 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
?
推荐答案
更新
要注册服务,您可以创建配置方法并将组件添加到 IFunctionsHostBuilder
实例.Azure Functions 主机创建一个 IFunctionsHostBuilder
并将其直接传递到您配置的方法中.
要注册你的配置方法,你必须添加一个程序集属性指定您的配置方法的类型,使用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 => {
// //...
//});
// Registers the application settings' class.
//...
//...removed for brevity
}
并且在函数的构造函数中有一个ILoggerFactory
.
and having anILoggerFactory
in the Function's constructor.
//...
//Ctor
public FindAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow) {
m_logger = loggerFactory.CreateLogger<FindAccountFunction>();
m_mapper = mapper;
m_accountWorkflow = accountWorkflow;
}
//...
这篇关于如何使用新的 DI 将 ILogger 注入使用 IWebJobsStartup 的 Azure 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!