2
votes

Setting up an Azure Function that references other netstandard projects which on their turn reference Microsoft.Extensions.Logging.

When the Function runs locally without any references to the other projects, everything starts fine. But as soon as I add a reference to one of the other projects, I get the following exception at startup:

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.).

This is the initial code I have for my Timer function

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();
    }
}

I have my dependencies injected in the following Startup class

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();
    }
}

The custom class that implements ITester is part of a netstandard 2.0 project that references the Microsoft.Extensions.Logging.Abstractions 3.0.0.0 nuget package. The functions project itself is a netcoreapp2.1 project that also references that same nuget package, as well as the Microsoft.NET.Sdk.Functions 1.0.29, the Microsoft.Azure.Functions.Extensions 1.0.0 and the Microsoft.NETCore.App 2.1.0 package.

For reference, the csproj files:

The function project

<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>

The referenced 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>

I am quite sure it's reference issue, but cannot put my hands on it. Any help?

1
Can you show the package reference of your project? The entier .csproj file.Bowman Zhu
Hello @BowmanZhu: just added both csproj files in the description. ThxSam Vanhoutte
Hi, I try to reproduce on myside and solved this error, you can have a try whether it can also work on your side.Bowman Zhu
Thanks, @BowmanZhu - good catch. You just saved my day.Sam Vanhoutte
Happy to help you.Bowman Zhu

1 Answers

2
votes

Have reproduced your error:

enter image description here

Seems the error comes from the reference project.

Solution:

Downgrading the version of Microsoft.Extensions.Logging.Abstractions to 2.2.0.

Cause:

The binding engine doesn't recognize your parameter's type as being the same since they're from different assemblies.