asp.net-core - 尝试在 Azure 函数中使用依赖注入(inject)时出现奇怪的异常

标签 asp.net-core dependency-injection .net-core azure-functions

给定一个文件->新建->Azure Functions v2 应用程序,我试图获取对 ILoggerFactory 的引用或ILogger<T> .

我正在 StartUp.cs 中执行此操作在函数应用程序启动时运行的类。

给出以下代码,抛出一个奇怪的异常:

var serviceProvider = builder.Services.BuildServiceProvider();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();         

但以下情况除外:

enter image description here

 A host error has occurred
[27/02/2019 8:21:22 AM] Microsoft.Extensions.DependencyInjection: Unable to resolve service for type 'Microsoft.Azure.WebJobs.Script.IFileLoggingStatusManager' while attempting to activate 'Microsoft.Azure.WebJobs.Script.Diagnostics.HostFileLoggerProvider'.
Value cannot be null.
Parameter name: provider

这是怎么回事?

完整的测试存储库/代码可以是 found here on GitHub .

最佳答案

在您创建依赖项注入(inject)容器并尝试解析记录器时,似乎尚未设置 HostFileLoggerProvider 所需的某些基础设施(例如 IFileLoggingStatusManager)在启动类中。我认为您应该延迟日志记录,直到应用程序完全启动之后。

如果您查看 WebJobs 的启动代码,您会看到添加了记录器 first ,之后the external startup gets executed最后是 required logging services被添加。对于您的情况,这顺序是错误的。

关于asp.net-core - 尝试在 Azure 函数中使用依赖注入(inject)时出现奇怪的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54900982/

相关文章:

c# - 在每个 POST/GET 请求之前调用方法/执行操作 .NET Core

c# - 红隼: get access to IP+port pair(s) it's listening on

constraints - 标签助手 : Is it possible to add type constraints to a ModelExpression?

c# - 使用参数从 .Net Core 运行 bash 命令

c# - 为什么在Nuget上找到的包不是最新版本?

c# - Unity的属性注入(inject)导致堆栈溢出

asp.net - 如何在 ASP.NET Web 服务中使用依赖注入(inject)和存储库模式?

jakarta-ee - Java EE 中的 InitialContext 相对于mappedName 有什么用处?

amazon-web-services - .NET Core API Gateway (AWS) 如何设置依赖注入(inject) (DI) 以使用 MemCached .NET Core 包、基于 DI 的记录器等

.net - Azure Signalr 服务拒绝将连接添加到组