设置一个引用其他 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>
我很确定这是引用问题,但无法亲自处理。 有什么帮助吗?
最佳答案
已重现您的错误:
似乎错误来自引用项目。
解决方案:
将 Microsoft.Extensions.Logging.Abstractions
版本降级至 2.2.0。
原因:
绑定(bind)引擎不会将您的参数类型识别为相同,因为它们来自不同的程序集。
关于c# - 由于与 ILogger 的引用冲突,具有依赖项注入(inject)的 Azure 函数未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58680145/