2
votes

I'm fighting with Google Docs for setting up Cloud PubSub with .NET using a PubSub emulator.

https://cloud.google.com/dotnet/docs/getting-started/using-pub-sub

https://cloud.google.com/pubsub/docs/publisher

https://cloud.google.com/pubsub/docs/emulator

Coming from a Rails background, I'm tasked to implement Cloud PubSub for a .NET product, running our google cloud on .NET Core, to enable it to publish.

Google::Cloud::Pubsub.new(project: project_id, emulator_host: emulator_host)

From the documentation using .NET, I keep coming back to the following:

PublisherServiceApiClient publisherClient = PublisherServiceApiClient.Create();
PublisherClient publisher = PublisherClient.Create(...)

However, the library used from the docs Google.Cloud.PubSub.V1 -Pre does not contain the definition.

'PublisherClient' does not contain a definition for 'Create'.

Instead, I get CreateAsync that takes in TopicName, PublisherClient.ClientCreationSettings and PublisherClient.Settings.

https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/api/Google.Cloud.PubSub.V1.PublisherClient.html

I noticed that PublisherServiceApiClient can take in a Channel, but I'm confused on how to get this going.

To conclude with an actual question, how does one currently implement Cloud PubSub with .NET for in cloud and then locally with emulator? Adding to that, am I using the wrong library or the wrong docs?

Any suggestions, pointers or piece of advice would be truly appreciated.

2
It’s well documented stuff, may be too much.. After searching I find this tutorial which guides you how to setup your dev environment.. cloud.google.com/appengine/docs/flexible/dotnet/… As to why for you Create() not showing, is may be you missing a setup step..boateng
Unfortunately that guide is for a ASP.NET app, which our application is not :/ I thought there could be some take aways from the guide, but seems like they're also using PublisherClient.Create()...fbelanger
I think they using ASP .Net Core for HelloWorld tutorial.. There is also bigger all encompassing tutorial for Bookshelf app (probably not what you want either) cloud.google.com/dotnet/docs/getting-started/tutorial-appboateng
Yeah I was initially following along using the Bookshelf app tutorial, but same issue.fbelanger
For PublisherClient, you should indeed use CreateAsync. We don't currently have any direct emulator support, although there's work afoot that might make that simpler. You could specify a PublisherClient.ClientCreationSettings with a suitable ServiceEndpoint and ChannelCredentials.Insecure to talk to the emulator though. I won't add an answer to this question at the moment as it's not a clear question to answer - some parts appear to be about docs, some about creation, and some about the emulator. If you could clarify this to a specific question, it would be easier to add an answer.Jon Skeet

2 Answers

2
votes

I managed a solution that I am happy with.

Instead of using the PublisherClient, I went with using the PublisherServiceApiClient alone.

emulatorAddr = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");
if (emulatorAddr != null)
{
    channel = new Channel(emulatorAddr, ChannelCredentials.Insecure);
    pub = PublisherServiceApiClient.Create(channel);
}
else
{
    pub = PublisherServiceApiClient.Create();
}

Which meant that publishing was slightly more involved then sending string to the PublisherClient, but overall not so bad.

PubsubMessage msg = new PubsubMessage
{
    Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(payload))
};

pub.PublishAsync(topic, new[]{ msg });

If the project is running in a Google Compute Engine, it will have default credentials. Otherwise, wether you're running an emulator locally or in docker you can define PUBSUB_EMULATOR_HOST.

What really helped was this https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/index.html

0
votes

To make the PublisherClient connect to a local emulator, you need to pass custom ServiceEndpoint and ChannelCredentials to CreateAsync:

var serviceEndpoint = new ServiceEndpoint(theEmulatorHost, theEmulatorPort);

var publisherClient = await PublisherClient.CreateAsync(
    topicName,
    new PublisherClient.ClientCreationSettings(credentials: ChannelCredentials.Insecure, serviceEndpoint: serviceEndpoint));

To switch to the real PubSub, just leave away the ClientCreationSettings.