我正在將Google Cloud PubSub集成到一個示例c#項目中,我是一個c#的新手,因爲這可能是我將在我公司工作的唯一c#項目由於與用c#編寫的遊戲集成的一些要求。 我用的NuGet安裝Google.Cloud.PubSub.V1.0.0-beta13和安裝了臺,但是當我嘗試運行使用我得到以下錯誤的文檔創建示例代碼:運行基於雲的pubsub的c#示例項目時的依賴問題
C:/Users/MyUser/RiderProjects/TestConsole/TestConsole/bin/Debug/TestConsole.exe
Unhandled Exception: System.IO.FileLoadException: 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)
at Google.Api.Gax.TaskExtensions.WaitWithUnwrappedExceptions(Task task) in C:\Users\jon\Test\Projects\gax-dotnet\releasebuild\src\Google.Api.Gax\TaskExtensions.cs:line 48
at Google.Api.Gax.Grpc.ChannelPool.GetChannel(ServiceEndpoint endpoint) in C:\Users\jon\Test\Projects\gax-dotnet\releasebuild\src\Google.Api.Gax.Grpc\ChannelPool.cs:line 92
at Google.Cloud.PubSub.V1.PublisherClient.Create(ServiceEndpoint endpoint, PublisherSettings settings) in C:\Users\jon\Test\Projects\google-cloud-dotnet\releasebuild\apis\Google.Cloud.PubSub.V1\Google.Cloud.PubSub.V1\PublisherClient.cs:line 558
at TestConsole.Program.CreateTopic(String projectId, String topicId) in C:\Users\MyUser\RiderProjects\TestConsole\TestConsole\Program.cs:line 11
at TestConsole.Program.Main(String[] args) in C:\Users\MyUser\RiderProjects\TestConsole\TestConsole\Program.cs:line 32
然後我嘗試將Google.Apis.Auth降級到1.21.0,但隨後問題轉移到「無法加載Google.Api.Gax,版本= 1.0.1.0」,然後(如果我保持降級依賴關係)在Google.Protobuf 3.2上.0.0,然後在Google.Apis.Core 1.24.1上,然後回到「無法加載Google.Apis.Auth 1.21.0」,所以我猜這個問題在別的地方。
是什麼導致了這種依賴性問題?如果我從Github加載Google Pubsub示例項目,即使packages.config與我的項目中的packages.config相同,我也不會收到任何問題。
這裏是我的Program.cs:
using Google.Cloud.PubSub.V1;
using Google.Protobuf;
namespace TestConsole
{
internal class Program
{
public static object CreateTopic(string projectId, string topicId)
{
var publisher = PublisherClient.Create();
var topicName = new TopicName(projectId, topicId);
var message = new PubsubMessage
{
// The data is any arbitrary ByteString. Here, we're using text.
Data = ByteString.CopyFromUtf8("Hello Cloud Pub/Sub!"),
// The attributes provide metadata in a string-to-string
// dictionary.
Attributes =
{
{ "description", "Simple text message" }
}
};
publisher.Publish(topicName, new[] { message });
return 0;
}
public static void Main(string[] args)
{
CreateTopic("MyProjectID", "MyProjectTopic");
}
}
}
和我packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Google.Api.CommonProtos" version="1.0.0" targetFramework="net452" />
<package id="Google.Api.Gax" version="1.0.1" targetFramework="net452" />
<package id="Google.Api.Gax.Grpc" version="1.0.1" targetFramework="net452" />
<package id="Google.Apis" version="1.24.1" targetFramework="net452" />
<package id="Google.Apis.Auth" version="1.24.1" targetFramework="net452" />
<package id="Google.Apis.Core" version="1.24.1" targetFramework="net452" />
<package id="Google.Cloud.Iam.V1" version="1.0.0-beta09" targetFramework="net452" />
<package id="Google.Cloud.PubSub.V1" version="1.0.0-beta09" targetFramework="net452" />
<package id="Google.Protobuf" version="3.2.0" targetFramework="net452" />
<package id="Grpc.Auth" version="1.4.0" targetFramework="net452" />
<package id="Grpc.Core" version="1.4.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net452" />
<package id="System.Interactive.Async" version="3.1.1" targetFramework="net452" />
<package id="System.Net.Http" version="4.3.1" targetFramework="net425" />
<package id="Zlib.Portable.Signed" version="1.11.0" targetFramework="net452" />
</packages>
我使用騎士2017年1月1日運行我的項目,我在.NET框架4.5運行它。 2。
請注意,我已經知道這個URL Unable to run Google Cloud PubSub in c#, DLL problems已經發布了一個非常類似的問題,但由於我的「信譽」低,我不能評論它(你知道,我通常嘗試閱讀文檔和搜索問題已經回答了,並且儘量避免造成重複,這就是爲什麼我沒有在這個網站上建立很高的聲譽),並且讓問題的人不知道該如何解決問題。 在回答寫的是:
...if you manage all the dependencies via NuGet, I'd expect it to be okay - it should add assembly binding redirects for you.
這在我看來,我已經做了。
嗨!你是否已經從鏈接問題的接受答案中嘗試了最後一個建議? 「或僅在應用程序和庫中添加對Google.Cloud.PubSub.V1的引用,此時NuGet將爲您執行所有依賴關係處理。」 –
我注意到的另一個細節是你在問題中提到了''beta13''',但'''packages.config''文件中引用了''beta09'''。在某個地方會有版本不匹配嗎? –
嗨@MarcelloRomani我嘗試過不同的版本,所以不匹配可能是我嘗試處理事物的不同方式中的一個剩餘部分。另外我已經在應用程序中添加了參考,但我不知道在庫中添加它的含義是什麼。 –