MVFC.Messaging.GCP 3.0.2

dotnet add package MVFC.Messaging.GCP --version 3.0.2
                    
NuGet\Install-Package MVFC.Messaging.GCP -Version 3.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MVFC.Messaging.GCP" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Messaging.GCP" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Messaging.GCP" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MVFC.Messaging.GCP --version 3.0.2
                    
#r "nuget: MVFC.Messaging.GCP, 3.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MVFC.Messaging.GCP@3.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MVFC.Messaging.GCP&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Messaging.GCP&version=3.0.2
                    
Install as a Cake Tool

MVFC.Messaging.GCP

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

A .NET messaging provider for Google Cloud Pub/Sub, built on top of MVFC.Messaging.Core. Provides PubSubPublisher<T> and PubSubConsumer<T> for publishing and consuming JSON-serialized messages using the official Google Cloud client library with built-in emulator support.

Package

Package Downloads
MVFC.Messaging.GCP Downloads

Installation

dotnet add package MVFC.Messaging.GCP

This package depends on MVFC.Messaging.Core (installed automatically) and Google.Cloud.PubSub.V1.

Configuration

Authentication

Google Cloud client libraries use Application Default Credentials (ADC). Common setups:

Method Description
gcloud auth application-default login For local development — stores credentials in ~/.config/gcloud/
Service Account JSON Set GOOGLE_APPLICATION_CREDENTIALS env var to the JSON key file path
Workload Identity On GKE, Cloud Run, or Compute Engine — automatic credential discovery
Emulator Set PUBSUB_EMULATOR_HOST=localhost:8085 for local testing

Emulator Detection

Both PubSubPublisher<T> and PubSubConsumer<T> are configured with EmulatorDetection.EmulatorOrProduction. This means:

  • If PUBSUB_EMULATOR_HOST is set → connects to the emulator.
  • Otherwise → connects to production Google Cloud Pub/Sub.

No code changes needed between local and production environments.

Constructor Parameters

  • Publisher: projectId (your GCP project) and topicId (the Pub/Sub topic name).
  • Consumer: projectId and subscriptionId (the Pub/Sub subscription name).

appsettings.json Example

{
  "GCP": {
    "ProjectId": "my-gcp-project",
    "TopicId": "orders",
    "SubscriptionId": "orders-subscription"
  }
}
var projectId = builder.Configuration["GCP:ProjectId"]!;
var topicId = builder.Configuration["GCP:TopicId"]!;
var subscriptionId = builder.Configuration["GCP:SubscriptionId"]!;

Usage

Publishing a Single Message

using MVFC.Messaging.GCP.PubSub;

var projectId = "my-gcp-project";
var topicId = "orders";

await using var publisher = new PubSubPublisher<OrderCreated>(projectId, topicId);

var order = new OrderCreated(1, "Keyboard", 149.90m);
await publisher.PublishAsync(order);

The message is serialized to JSON and published as the Pub/Sub message data.

Publishing a Batch

Batch publishing sends all messages concurrently using Task.WhenAll:

var orders = new[]
{
    new OrderCreated(1, "Keyboard", 149.90m),
    new OrderCreated(2, "Mouse", 59.90m),
    new OrderCreated(3, "Monitor", 899.00m)
};

await publisher.PublishBatchAsync(orders);

Consuming Messages

The consumer wraps Google Cloud's SubscriberClient, which handles pull-based message delivery with automatic flow control. Each message receives an Ack (success) or Nack (failure) reply:

using MVFC.Messaging.GCP.PubSub;

var projectId = "my-gcp-project";
var subscriptionId = "orders-subscription";

await using var consumer = new PubSubConsumer<OrderCreated>(projectId, subscriptionId);

await consumer.StartAsync(async (message, ct) =>
{
    Console.WriteLine($"Processing order #{message.OrderId}: {message.Product}");
    // Your business logic here
}, cancellationToken);

// ... later, when shutting down:
await consumer.StopAsync();

Consumer behavior:

  • Uses Google Cloud's SubscriberClient.StartAsync for managed message delivery.
  • Successful handler execution results in an Ack — the message is removed from the subscription.
  • If the handler throws an exception, a Nack is sent — the message will be redelivered.
  • A 1-second initialization delay ensures the subscriber is fully ready before returning.
  • StopAsync gracefully stops the subscriber and waits for all pending handlers to complete.

Complete Publish + Consume Example

using MVFC.Messaging.GCP.PubSub;

// Using emulator: set PUBSUB_EMULATOR_HOST=localhost:8085
var projectId = "my-gcp-project";
var topicId = "orders";
var subscriptionId = "orders-subscription";

await using var publisher = new PubSubPublisher<OrderCreated>(projectId, topicId);
await using var consumer = new PubSubConsumer<OrderCreated>(projectId, subscriptionId);

// Start consuming
var received = new TaskCompletionSource<OrderCreated>();
await consumer.StartAsync(async (msg, ct) =>
{
    Console.WriteLine($"Received: Order #{msg.OrderId} — {msg.Product}");
    received.SetResult(msg);
}, CancellationToken.None);

// Publish
await publisher.PublishAsync(new OrderCreated(42, "Keyboard", 149.90m));

// Wait for the message to be consumed
var result = await received.Task.WaitAsync(TimeSpan.FromSeconds(30));

// Cleanup
await consumer.StopAsync();

API Reference

PubSubPublisher<T>

Constructor Parameters
PubSubPublisher<T>(string projectId, string topicId) GCP project ID and the Pub/Sub topic name
Method Description
PublishAsync(T message, CancellationToken ct) Serializes the message to JSON and publishes to the topic
PublishBatchAsync(IEnumerable<T> messages, CancellationToken ct) Publishes all messages concurrently
DisposeAsync() Shuts down the underlying PublisherClient

PubSubConsumer<T>

Constructor Parameters
PubSubConsumer<T>(string projectId, string subscriptionId) GCP project ID and the Pub/Sub subscription name
Method Description
StartAsync(Func<T, CancellationToken, Task> handler, CancellationToken ct) Starts the subscriber with Ack/Nack handling
StopAsync(CancellationToken ct) Stops the subscriber and waits for pending handlers
DisposeAsync() Stops the subscriber and ignores any shutdown exceptions

Requirements

  • .NET 9.0+
  • Google.Cloud.PubSub.V1 (installed automatically)
  • Google Cloud credentials (ADC) or Pub/Sub emulator

License

Apache-2.0

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 114 4/5/2026
3.0.1 108 4/3/2026
2.0.4 103 3/21/2026
2.0.3 102 3/21/2026
2.0.2 107 3/21/2026
2.0.1 101 3/19/2026
1.0.2 248 12/19/2025
1.0.1 263 12/19/2025
1.0.0 259 12/19/2025