Azure 函数 v2,核心 3.1 无法加载文件或程序集 System.Memory,版本=4.2.0.0

Azure function v2, Core 3.1 Could not load file or assembly System.Memory, Version=4.2.0.0

我 运行 在本地是 Core 3.1 上的 Azure 函数 v2。函数连接从 EventHub 读取事件并将数据写入 Redis 数据库。 连接函数出错

System.Private.CoreLib: Exception while executing function: 
 One or more errors occurred. (Could not load file or assembly 'System.Memory, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.). 
 Pipelines.Sockets.Unofficial: Could not load file or assembly 'System.Memory, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.

相同的 Redis 连接代码在 Azure 函数之外正常工作。

我已经在项目中安装了 System.Memory nuget package v 4.5.3 但它没有帮助。 核心 3.1

的 nuget 中没有列出 System.Memory 版本 4.2

函数使用启动

[assembly: FunctionsStartup(typeof(...))]

项目文件

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.1.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
    <PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
    <PackageReference Include="System.Memory" Version="4.5.3" />
    <PackageReference Include="Utf8Json" Version="1.3.7" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\Configuration\Configuration.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>

怎么了?

您能否尝试将 true 添加到您的 .csproj 文件中:

 <PropertyGroup>
    ...
    <UseNETCoreGenerator>true</UseNETCoreGenerator>
  </PropertyGroup>

通过在项目 .csproj 文件中将 Azure 函数版本更改为 v3 解决了该问题。

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>

如果问题仍然存在,请确认您使用的是正确版本的 Azure Functions Core Tools (v3),我碰巧从 nodejs 中获取了版本 2,也就是说,我在 nodejs 和版本中安装了版本 2 3 在文件夹中 From microsoft 在路径中它们都是,但我选择了第一个是 nodejs 的,它一直给我错误,直到我删除 nodejs 一个,你可以在 nodejs 中将它更新到版本 3,我想它效果一样。