c# - 由于与 ILogger 的引用冲突,具有依赖项注入(inject)的 Azure 函数未启动

标签 c# azure dependency-injection .net-core azure-functions

设置一个引用其他 netstandard 项目的 Azure 函数,而这些项目又引用 Microsoft.Extensions.Logging。

当函数在本地运行而没有任何对其他项目的引用时,一切都会开始正常。但是,一旦我添加对其他项目之一的引用,我就会在启动时收到以下异常:

TestTrigger: Microsoft.Azure.WebJobs.Host: Error indexing method 'TestTrigger'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'log' to type ILogger. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

这是我的计时器功能的初始代码

public class TestTrigger
{
    private ITester _tester;
    public TestTrigger(ITester tester)
    {
        _tester = tester;
    }

    [FunctionName("TestTrigger")]
    public void Run([TimerTrigger("* * * * * *")] TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.UtcNow}");
        _tester?.TestMethod().Wait();
    }
}

我在以下 Startup 类中注入(inject)了依赖项

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
            var cfgBuilder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile($"appsettings.json", true, true)
                .AddJsonFile($"appsettings.dev.json", true, true)
                .AddEnvironmentVariables();
            var configuration = cfgBuilder.Build();
            builder.Services
                .AddSingleton<ITester, Tester>()
                .AddLogging()
                .AddOptions();
    }
}

实现 ITester 的自定义类是引用 Microsoft.Extensions.Logging.Abstractions 3.0.0.0 nuget 包的 netstandard 2.0 项目的一部分。函数项目本身是一个 netcoreapp2.1 项目,它也引用相同的 nuget 包,以及 Microsoft.NET.Sdk.Functions 1.0.29、Microsoft.Azure.Functions.Extensions 1.0.0 和 Microsoft.NETCore。应用程序2.1.0包。

csproj 文件供引用:

功能项目

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
        <AzureFunctionsVersion></AzureFunctionsVersion>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
    </ItemGroup>
    <ItemGroup>
        <None Update="host.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
    </ItemGroup>
    <ItemGroup>
      <ProjectReference Include="..\TestLibrary\TestLibrary.csproj" />
    </ItemGroup>
</Project>

引用的项目:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
      <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" />
    </ItemGroup>

</Project>

我很确定这是引用问题,但无法亲自处理。 有什么帮助吗?

最佳答案

已重现您的错误:

enter image description here

似乎错误来自引用项目。

解决方案:

Microsoft.Extensions.Logging.Abstractions 版本降级至 2.2.0。

原因:

绑定(bind)引擎不会将您的参数类型识别为相同,因为它们来自不同的程序集。

关于c# - 由于与 ILogger 的引用冲突,具有依赖项注入(inject)的 Azure 函数未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58680145/

相关文章:

azure - 可以使用Azure SDK for python来查询azure服务运行状况吗?

node.js - 尝试从 DocumentDb 删除文档时出现错误请求错误

Azure服务结构可靠的集合清理

c# - 如何为 ASP.NET MVC 5 创建依赖注入(inject)?

C# 在 switch 语句中没有隐式失败

c# - MVC 传递列表以查看问题

带有 TypeScript 和注入(inject)的 AngularJS 过滤器

grails - 无法将 grailsApplication 注入(inject)服务

c# - virtual 关键字对 Entity Framework 5 中的实体属性有什么影响?

c# - 使用 C# 将 AppDomain 映射到一个友好的名称