无法在 c# 中 运行 Google Cloud PubSub,DLL 问题

Unable to run Google Cloud PubSub in c#, DLL problems

我正在努力将 Google Cloud PubSub 集成到我的 c# 项目中,我使用 NuGet 安装 1.0.0-beta11,完全没有错误。当我 运行 我的项目并且当它到达使用 pubsub 的代码时,我收到以下错误:

An unhandled exception of type 'System.IO.FileLoadException' occurred in 
 Google.Api.Gax.dll

Additional information: Could not load file or assembly 'Google.Apis.Auth, Version=1.21.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

NuGet 确实安装了所有依赖项,包括 Google.Apis.Auth.dll,但它是 1.27.1。我已经尝试了所有我能想到的方法,包括获取该 dll 的 1.21.0 并使用它,但没有成功。我已经能够 运行 .net sample 没有错误。

有什么想法吗?如果您需要更多信息,请告诉我

好的,我找到了对 Google.Apis.Auth 1.21.0 的引用 - 它在 Grpc.Auth NuGet 包中。

如果您只是添加对 Grpc.Auth DLL 的引用,您将遇到这种失败 - 但如果您通过 NuGet 管理 所有 依赖项,我希望它没问题 - 它应该为您添加程序集绑定重定向。

在不知道您的确切设置的情况下,很难说更多 - 可能是您的 library 需要程序集绑定重定向,但 application 控制着它们...特别是,如果您的应用程序仅引用库 DLL,就会导致问题。

作为一种变通方法,您可以手动添加程序集绑定重定向(确切的方法取决于应用程序类型),或者只在应用程序中添加对 Google.Cloud.PubSub.V1 的引用以及库,此时 NuGet 将为您完成所有依赖项处理。

您可以将以下内容附加到您的 app.config 文件中。

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Google.Apis.Auth" publicKeyToken="4b01fa6e34db77ab" culture="neutral" />
      <bindingRedirect oldVersion="1.21.0.0-1.35.1.0" newVersion="1.35.1.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

老问题,但也许将来会对某人有所帮助。 也被它困住了,这确实是 bindingRedirect 问题。 在我的例子中,app.config 处于错误的级别,就在项目级别。

祝你好运