0
votes

I have been trying to refer a .net core library project into my Azure function project to call one of the process defined in a .net core class library.

The .net core library project uses ILogger.

However, whenever I try to run a function I get the following error:

[29/11/2019 1:08:35 AM] The 'Function1' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'log' to type ILogger`1. 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(),

This is my code:

[FunctionName("Function1")]
        public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            // string templateFile = Path.Combine(context.FunctionAppDirectory, "Data", "HelloHttpOutputTemplate.txt");
            Startup.Main(null).Wait();
        }

I also confirmed that my .net core project as well as function project are referring a same version of ILogger:

'microsoft.extensions.logging.abstractions\3.0.0\lib\netstandard2.0'

If I remove a .net core library project reference from within function project then it works.

My function CS Project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <Content Include="..\Accounts.Console\appsettings.json" Link="appsettings.json" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.28" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Accounts.Console\Accounts.Console.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

My .net core Library project file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <StartupObject></StartupObject>
    <ApplicationIcon />
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="librdkafka.redist" Version="1.2.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="3.0.0" />
  </ItemGroup>


  </Project>
1
If you're developing locally, can you show your .csproj file? It makes no sense to show your function code for this error.Bowman Zhu
@BowmanZhu Thanks I have just updated my question.codebased
Hi, I have update my answer.Bowman Zhu
@BowmanZhu Thank you the compilation, and registration of a azure function by runtime problem was solved by what you'd suggested. However, I will not use the same because it caused me other issues in the app where I was using logger and method was not found in lower version. The permanent solution I found is to update to function v3, which is in preview at the moment.codebased

1 Answers

3
votes

Update:

Have use .net core 3.0 now.

enter image description here

But still meet the same error:

enter image description here

This is my .csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="librdkafka.redist" Version="1.2.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.28" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Solution:

Downgrading the version of Microsoft.Extensions.Http, Microsoft.Extensions.Logging.Console, Microsoft.Extensions.Logging.Debug and Microsoft.Extensions.Options from 3.0.0 to 2.2.0. Then error will be solved.

enter image description here


Original Answer:

I have reproduce your error:

enter image description here

Cause:

Assembly conflict. The key is your last four assembly references.

Solution:

Change them to 2.2.0. Things works fine on my side. Please have a try on your side and let me know if you have difficulties.

My .csproj file:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="librdkafka.redist" Version="1.2.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="2.2.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>
</Project>