无法使用 Azure Functions 加载文件或程序集 System.Fabric

Could not load file or assembly System.Fabric with Azure Functions

可用于 Azure Functions 的包是否有任何限制。我已尽我所能进行研究,但似乎并非如此,但是当我尝试创建一个引用包 "Microsoft.ServiceFabric" 的 Azure 函数时,我收到以下错误:

System.Private.CoreLib: Exception while executing function: ScaleDownServiceFabrics. FunctionApp2: Could not load file or assembly 'System.Fabric, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'System.Fabric, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

我尝试了 Azure Func and.1 和 2,以及 .Net Framework 和 .Net Core,但没有成功。

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using System.Fabric;

namespace FunctionApp5
{
  public static class Function1
  {
    [FunctionName("Function1")]
    public static void Run([TimerTrigger("*/5 * * * * *")]TimerInfo myTimer, ILogger log)
    {
      log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
      FabricClient client = new FabricClient();
    }
  }
}

这是否可能,或者 Visual Studio 中 Azure Functions 的限制 - 如果是这样,哪些包是可接受的?

Diego 和 Suraj 指出了原因,64 位和 32 位之间存在冲突。

有两点需要修正

  1. 像您所做的那样将构建平台设置为 x64。
  2. 获取 x64 函数运行时。 Functions 在 Function runtime 上运行(包含在 Azure Function 核心工具中),但默认位是 VS 下载的 x86。

要以简单的方式获得 x64 位,让我们使用 Nodejs 从 NPM 安装 Azure Functions Core Tools。

安装完成后,在cmd中输入npm i -g azure-functions-core-tools --unsafe-perm true得到Function core tools

然后设置项目调试属性(右键单击您的项目>属性>调试blade)。

  1. 将启动类型设置为 Executable

  2. 将可执行文件路径设置为 %appdata%\npm\node_modules\azure-functions-core-tools\bin\func.exe

  3. 添加应用程序参数 start.

  • ServiceFabric 包是 x64 位的,如果您的函数目标是 32 位,它将失败。尝试 Jerry Liu 提出的解决方案
  • Service Fabric 包必须作为包添加,而不是直接在项目中引用 dll,因为它依赖于其他库。您应该添加 NuGet 包 Microsoft.ServiceFabric.
  • Microsoft.ServiceFabric 最新版本 6.3.x 目标 .Net Standard 2.0.Net Framework4.54.7.1,请确保您使用的是任何这些在你的项目中。
  • 确保在 built\deployed 时将 Microsoft.ServiceFabric 个 DLL 复制到 bin 文件夹。
  • 在集群外使用 FabricClient 时,必须指定设置和凭据,否则将无法连接到集群。请参阅 this example and this 文档。
  • FabricClient 使用 Service Fabric API 与集群交互,如果包有问题,另一种选择是使用 HttpClient 并向 API 发出请求并避免包冲突

我 运行 遇到与 @tank140 在原始 post 中评论的完全相同的问题:

Unable to load DLL 'FabricClient.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

我在 中针对此问题提出的更多详细信息。作为回答,已确认 SF Client API for .NET 需要在平台上安装 SF 运行time,这在 Azure Functions 中不受支持。

就我而言,我只是将 Azure 平台配置更新为 64 位。不过,我是用.net core 3.1 function app 进入win平台的。