Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency issue when running c# sample project based on cloud pubsub

I am working on integrating Google Cloud PubSub into a sample c# project, I am a newbie with c# as this will probably be the only c# project I will work on in my company due to some requirements on integrating with a game written in c#. I used NuGet to install Google.Cloud.PubSub.V1.0.0-beta13 and the installation went successfully, however when I try to run the sample code created using the docs I get the following error:

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

I then tried downgrading Google.Apis.Auth to 1.21.0 but then the problem moves to "Could not load Google.Api.Gax, Version=1.0.1.0" and then (if I keep downgrading dependencies) on Google.Protobuf 3.2.0.0, then on Google.Apis.Core 1.24.1 and then back to "Could not load Google.Apis.Auth 1.21.0" so I guess the problem is somewhere else.

What causes this dependency issue? If I load the Google Pubsub sample project from Github I do not get any issues even if the packages.config is the same of the one in my project.

Here is my 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");
        }
    }
}

and my 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>

I use Rider 2017.1.1 to run my project and I run it on .NET framework 4.5.2.

Please note that I already know that a very similar question is already posted at this URL Unable to run Google Cloud PubSub in c#, DLL problems but due to my low "reputation" I can't comment it (you know, I usually try to read documentation and search for questions already answered and try to avoid creating duplicates, that is why I haven't build a high reputation on this site) and the guy that made the question solved the issue for himself without knowing how. In the answer is written:

...if you manage all the dependencies via NuGet, I'd expect it to be okay - it should add assembly binding redirects for you.

which it seems to me I am already doing.

like image 422
Jac Avatar asked Nov 08 '22 17:11

Jac


1 Answers

Try a fresh start: wipe out all the Google dependencies by running this command in Nuget Package Manager Console:

PM> get-package | where {$_.Id -like 'Google*'} | % { uninstall-package -Force $_.Id -ProjectName $_.ProjectName}

Then install PubSub again:

PM> install-package Google.Cloud.PubSub.V1 -pre
like image 168
Jeffrey Rennie Avatar answered Nov 14 '22 23:11

Jeffrey Rennie