Azure函数2.x ILogger是否不能与.net核心ILogger竞争?

时间:2019-11-29 01:14:16

标签: c# .net azure .net-core azure-functions

我一直试图将.net核心库项目引用到我的Azure函数项目中,以调用.net核心类库中定义的过程之一。

.net核心库项目使用ILogger。

但是,每当我尝试运行函数时,都会出现以下错误:

  

[29/11/2019 1:08:35 AM]'Function1'函数错误:   Microsoft.Azure.WebJobs.Host:错误索引方法'Function1'。   Microsoft.Azure.WebJobs.Host:无法绑定参数“ log”进行键入   ILogger`1。确保绑定支持参数类型。   如果您使用的是绑定扩展(例如Azure存储,ServiceBus,   计时器等),请确保您已经调用了注册方法   您的启动代码中的扩展名(例如builder.AddAzureStorage(),   builder.AddServiceBus(),

这是我的代码:

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

我还确认了我的.net核心项目和功能项目都引用了相同版本的ILogger:

  

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

如果我从功能项目中删除.net核心库项目引用,那么它将起作用。

我的函数CS Project文件:

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

我的.net核心库项目文件:

<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 个答案:

答案 0 :(得分:3)

更新

现在使用unpublish

enter image description here

但仍然遇到相同的错误:

enter image description here

这是我的.net core 3.0文件:

.csproj

解决方案:

<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> Microsoft.Extensions.HttpMicrosoft.Extensions.Logging.ConsoleMicrosoft.Extensions.Logging.Debug的版本从Microsoft.Extensions.Options降级到3.0.0。然后错误将得到解决。

enter image description here


原始答案:

我已重现您的错误:

enter image description here

原因:

装配冲突。关键是最后四个程序集引用。

解决方案:

将它们更改为2.2.0。事情对我来说很好。请尝试一下,如果遇到困难,请告诉我。

我的2.2.0文件:

.csproj