使用 Azure Function With Visual Studio 2022 加载所需库 hostpolicy.dll 时出错

An error occurred while loading required library hostpolicy.dll with Azure Function With Visual Studio 2022

我在本地计算机和 Visual Studio 2022 上使用 运行 我的独立 .NET 5 Azure 函数时遇到问题。我有下一条消息:

Failed to load the dll from [C:\Users\***\AppData\Local\AzureFunctionsTools\Releases.30.1\cli_x64\hostpolicy.dll], HRESULT: 0x800700C1

An error occurred while loading required library hostpolicy.dll from [C:\Users\***\AppData\Local\AzureFunctionsTools\Releases.30.1\cli_x64]

C:\Program Files (x86)\dotnet\dotnet.exe (process 15628) exited with code -2147450750.

Press any key to close this window . . .

项目代码如下:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <!-- Some code here -->
  </ItemGroup>
  <ItemGroup>
    <!-- Some code here -->
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

我试过这些东西:

  1. <RuntimeIdentifiers>win10-x64;win7-x86</RuntimeIdentifiers>添加到项目文件的PropertyGroup标签_(就像这个问题)→没用
  2. 设置路径环境变量 C:\Program Files\dotnet\ 以上 C:\Program Files (x86)\dotnet\ (如本文.NET 6 Azure Functions Isolated: An error occurred while loading required library hostpolicy.dll → 没用
  3. 删除并重新创建项目 → 没用
  4. 切换到 Visual Studio 2019 → 工作

为什么它不适用于 Visual Studio 2022?

问题归结为 PATH 环境变量,其中对 dotnet 的 32 位版本的引用高于 64 位版本。

你所要做的就是交换它们。 编辑环境变量

更新

重新启动系统以反映更改。