azure - 通过 Startup.cs 添加依赖项注入(inject)到 Azure Functions 会导致主机错误

标签 azure azure-functions

我有以下 Startup.cs 文件

using System;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Namespace.Functions.Services;

[assembly: FunctionsStartup(typeof(Namespace.Functions.Startup))]

namespace Namespace.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();
            builder.Services.AddSingleton<INamespaceApiService, QuizService>();
        }
    }
}

添加此内容后,当我运行函数应用程序时,我会在输出中得到以下内容

[10/15/2020 5:31:33 PM] Loaded extension 'Startup' (1.0.0.0)
[10/15/2020 5:31:33 PM] A host error has occurred during startup operation 'f456f2c1-5fd7-4253-859c-c8dc5bc3b2ca'.
[10/15/2020 5:31:33 PM] System.Private.CoreLib: Could not load type 'Microsoft.Azure.WebJobs.Hosting.IWebJobsStartup2' from assembly 'Microsoft.Azure.WebJobs.Host, Version=3.0.17.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

这是我的 .csproj 文件

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.1.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.9" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" Version="3.0.14" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.1.0" />
    <PackageReference Include="SendGrid" Version="9.16.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.7" />
    <PackageReference Include="Twilio" Version="5.44.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.22" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Services\" />
  </ItemGroup>
</Project>

如果我删除 Startup.cs 文件,一切都会正常运行 - 我需要做什么才能解决此错误?

最佳答案

This issue似乎表明可以通过将 Microsoft.Azure.Functions.Extensions 包降级到版本 1.0.0 来解决该错误。

我可以确认我有一个使用依赖项注入(inject)和 .NET Core 的 Azure 函数以及以下版本的所需包:

<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.7" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.9" />

Another issue似乎suggest that you将需要系统上安装的 Azure Function Core Tools 版本。

npm i -g azure-functions-core-tools@3 --unsafe-perm true

What does unsafe-perm in npm actually do .

TL;博士

您有两个选择:

  • Microsoft.Azure.Functions.Extensions 包降级到版本 1.0.0
  • 或者
  • 升级当前安装的 Azure Function Core Tools 版本

关于azure - 通过 Startup.cs 添加依赖项注入(inject)到 Azure Functions 会导致主机错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64376979/

相关文章:

c# - 在我的 Azure Function 中引用 C# 类库

azure 而不是 LA(MP)

azure - 将 webjobs 功能公开到仪表板,无需 Azure 存储

c# - 跟踪 Azure Functions 中的传出请求

azure - 如何从 Azure Functions 创建到 SignalR 服务的输出绑定(bind)?

sql - 由于外键冲突,无法恢复 bacpac

node.js - Azure托管 Node 应用程序找不到模块 'async/each'

azure - 如何添加对 Blob 触发的 Azure Function V 1.x 文件夹中所有 DLL 的引用?

node.js - 如何从 Nodejs 中的同步 Azure 函数返回结果

c# - Azure 函数计时器触发器