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

4

我在本地运行一个基于 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.

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

我已经将System.Memory nuget包v4.5.3安装到项目中,但这并没有帮助解决问题。对于Core 3.1,nuget上没有列出System.Memory版本4.2。

该函数使用Startup。

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

有什么问题吗?

3个回答

4
问题已通过在项目.csproj文件中将Azure功能版本更改为v3来修复。
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>

1
如果问题仍然存在,请确认您正在使用正确的Azure Functions Core Tools版本(v3)。我曾经遇到过这样的情况,我从nodejs中获取了版本2,也就是说,我在nodejs中安装了版本2,并且在路径中从Microsoft文件夹中安装了版本3,但我选择了第一个是nodejs版本,直到我删除了nodejs版本才解决了错误。您可以在nodejs中将其更新为版本3,我想它会起作用。

0

你可以尝试在你的.csproj文件中添加true:

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

很遗憾,这并没有帮助。我刚刚将此行添加到所有相关的项目文件中,而不仅仅是函数项目。错误仍然存在。 - Michael Chudinov
请查看此链接 https://github.com/Azure/azure-functions-vs-build-sdk/issues/160 - Sajeetharan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接