MVFC.Aspire.Helpers.GcpPubSub
13.1.0
dotnet add package MVFC.Aspire.Helpers.GcpPubSub --version 13.1.0
NuGet\Install-Package MVFC.Aspire.Helpers.GcpPubSub -Version 13.1.0
<PackageReference Include="MVFC.Aspire.Helpers.GcpPubSub" Version="13.1.0" />
<PackageVersion Include="MVFC.Aspire.Helpers.GcpPubSub" Version="13.1.0" />
<PackageReference Include="MVFC.Aspire.Helpers.GcpPubSub" />
paket add MVFC.Aspire.Helpers.GcpPubSub --version 13.1.0
#r "nuget: MVFC.Aspire.Helpers.GcpPubSub, 13.1.0"
#:package MVFC.Aspire.Helpers.GcpPubSub@13.1.0
#addin nuget:?package=MVFC.Aspire.Helpers.GcpPubSub&version=13.1.0
#tool nuget:?package=MVFC.Aspire.Helpers.GcpPubSub&version=13.1.0
MVFC.Aspire.Helpers.GcpPubSub
π§π· Leia em PortuguΓͺs
Helpers for integrating with Google Pub/Sub in .NET Aspire projects, including support for the emulator and administration interface (UI).
Motivation
Use .WithConfigurationTimeout(120) to override the default 60-second configuration deadline per emulator. Positive whole seconds up to 4,294,967 are supported. This deadline is shared across endpoint resolution and all subscription configuration; it is not restarted for each subscription. .WithWaitTimeout(...) controls the image separately. The UI is an optional administrative dependency; consumers should wait for the emulator itself.
Subscription configuration has a shared deadline of 60 seconds by default, including emulator and consumer endpoint resolution, client creation and SDK retries. Reads retry transient SDK errors and NotFound while the emulator image creates the declared subscriptions, with exponential backoff, jitter and at most 30 attempts. Subscription updates retain the SDK retry policy. AppHost cancellation interrupts calls and retry waits; expiration of the shared deadline raises TimeoutException. Configuration start, completion and final failures are logged against the emulator resource in the Aspire dashboard. WithWaitTimeout continues to control only the image's initialization timeout.
Project configurations may be added before or after references. Provisioning reads the current configuration when the emulator is ready. Each push subscription must select its consumer through MessageConfig.PushEndpointReference. There is no implicit destination derived from WithReference(pubSub).
Before resolving endpoints or issuing subscription RPCs, the helper validates all project/topic identifiers, push requirements and subscription policy ranges. AckDeadlineSeconds accepts 10β600 seconds; null keeps the library default of 300, while explicit zero requests the service default of 10. When a DLQ is configured, MaxDeliveryAttempts accepts 5β100; null and zero use 5. Topic-only declarations do not apply subscription policies. Declarations for the same project and subscription must have equivalent topic, policies and push destination; conflicting declarations raise InvalidOperationException, while equivalent declarations produce one subscription update. Different consumers need different subscription names. This validation prevents static configuration errors from causing partial updates; runtime RPC failures can still leave some subscriptions configured.
Push subscriptions combine the PushEndpoint route with the allocated port of PushEndpointReference, reached from the container through the Docker host. The port is resolved per subscription under the shared deadline. An explicit destination is required whenever a push route is configured; a missing destination raises InvalidOperationException. Reusing a topic across consumers requires a separate subscription for each destination. In the playground, GET /api/pub-sub-enter publishes a message and POST /api/pub-sub-exit receives the push. The publication response in Scalar does not include the response from this asynchronous callback.
Working with Google Pub/Sub locally usually means:
- Spinning up an emulator container by hand.
- Remembering ports, project IDs and environment variables.
- Manually creating topics/subscriptions and DLQs.
With .NET Aspire you can define containers, but you still need to:
- Configure the emulator image and its ports.
- Keep emulator environment variables in sync across projects.
- Define topics/subscriptions/DLQs in a consistent way.
MVFC.Aspire.Helpers.GcpPubSub provides:
AddGcpPubSub(...)to start the emulator.WithPubSubConfigs(...)to describe topics/subscriptions in code.AddGcpPubSubUI(...)to add a simple web UI.WithReference(...)to wire projects to the emulator and/or UI.
Overview
This project facilitates the configuration and integration of Google Pub/Sub in distributed .NET Aspire applications, providing extension methods to:
- Add the Google Pub/Sub emulator.
- Configure topics and subscriptions automatically.
- Support push and pull subscriptions.
- Provide an administration interface (UI) for management.
Pub/Sub emulator advantages
- Simulates message flow between services locally.
- Supports testing push and pull subscriptions without depending on Google Cloud infrastructure.
- Facilitates development and debugging of asynchronous integrations.
Push delivery to multiple consumers
var first = builder.AddDotnetProject("first", "../FirstApi/FirstApi.csproj");
var second = builder.AddDotnetProject("second", "../SecondApi/SecondApi.csproj");
var pubSub = builder.AddGcpPubSub("pubsub")
.WithPubSubConfigs(new PubSubConfig("project", messageConfigs:
[
new MessageConfig("events", "first-subscription", "/events")
{
PushEndpointReference = first.GetEndpoint("http"),
},
new MessageConfig("events", "second-subscription", "/notifications")
{
PushEndpointReference = second.GetEndpoint("http"),
},
]));
first.WithReference(pubSub);
second.WithReference(pubSub);
Explicit destinations are independent of reference order. Configurations without a push route remain pull subscriptions, even when an endpoint reference is supplied.
Compatible Images
- Emulator:
thekevjames/gcloud-pubsub-emulatormessagebird/gcloud-pubsub-emulator
- UI:
echocode/gcp-pubsub-emulator-ui
Project Structure
MVFC.Aspire.Helpers.GcpPubSub: Helpers and extensions library for Pub/Sub.
Features
- Adds the Google Pub/Sub emulator.
- Creates topics and subscriptions according to configuration.
- Supports push and pull subscriptions.
- Provides a Pub/Sub administration interface (UI).
- Extension methods to facilitate AppHost configuration.
- Dead Letter (DLQ) support.
Installation
dotnet add package MVFC.Aspire.Helpers.GcpPubSub
dotnet add package Aspire.Hosting.Dotnet --version 17.0.0
Commands and examples using AddDotnetProject target a .NET 10/11 AppHost on Aspire 17. For .NET 9 AppHosts, keep Aspire 13.6.5, omit Aspire.Hosting.Dotnet 17 and use AddProject<Projects.MyApi>("api"). Adjust the project path to your AppHost.
Quick Aspire usage (AppHost)
using Aspire.Hosting;
using MVFC.Aspire.Helpers.GcpPubSub;
using MVFC.Aspire.Helpers.GcpPubSub.Models;
var builder = DistributedApplication.CreateBuilder(args);
var api = builder.AddDotnetProject("api", "../MVFC.Aspire.Helpers.Playground.Api/MVFC.Aspire.Helpers.Playground.Api.csproj");
var messageConfig = new MessageConfig(
TopicName: "test-topic",
SubscriptionName: "test-subscription",
PushEndpoint: "/api/pub-sub-exit")
{
PushEndpointReference = api.GetEndpoint("http"),
DeadLetterTopic = "test-dead-letter-topic",
MaxDeliveryAttempts = 5,
AckDeadlineSeconds = 300,
};
var pubSubConfig = new PubSubConfig(
projectId: "test-project",
messageConfig: messageConfig);
var gcpPubSub = builder.AddGcpPubSub("gcp-pubsub")
.WithPubSubConfigs([pubSubConfig])
.WithWaitTimeout(seconds: 5)
.WithConfigurationTimeout(seconds: 120);
var ui = builder.AddGcpPubSubUI("pubsub-ui")
.WithReference(gcpPubSub)
.WaitFor(gcpPubSub);
api.WithReference(gcpPubSub)
.WaitFor(gcpPubSub);
await builder.Build().RunAsync();
Topics and subscriptions configuration
PubSubConfig
| Parameter | Type | Default | Description |
|---|---|---|---|
projectId |
string | β | GCP project ID. |
messageConfig |
MessageConfig |
β | Message configuration. |
secondsDelay |
int | 5 |
Legacy value stored in UpDelay; currently has no effect on initialization. |
secondsDelay and UpDelay do not control retries or timeouts. Use WithWaitTimeout(seconds) for the emulator image timeout and WithConfigurationTimeout(seconds) for the shared subscription configuration deadline (60 seconds by default).
MessageConfig
| Parameter | Type | Default | Description |
|---|---|---|---|
TopicName |
string | β | Topic name. |
SubscriptionName |
string? | null |
Subscription name. |
PushEndpoint |
string? | null |
HTTP route for push delivery. |
PushEndpointReference |
EndpointReference? | null |
Consumer endpoint; required when a push route is configured. |
DeadLetterTopic |
string? | null |
Dead letter topic name (DLQ). |
MaxDeliveryAttempts |
int? | null |
Max attempts before sending to DLQ. |
AckDeadlineSeconds |
int? | null |
Ack deadline (seconds). |
Note: If DeadLetterTopic is provided, the subscription {DeadLetterTopic}-subscription will be created automatically.
Ports
- Emulator port:
8681 - UI port:
8680
Topics and subscriptions diagram
graph TD
A[Pub/Sub Emulator]
B[test-topic]
C[test-subscription push]
D[test-subscription pull]
E[dead-letter-topic]
F[dead-letter-subscription]
A --> B
B --> C
B --> D
C --> E
E --> F
Provisioning diagram
sequenceDiagram
participant Aspire as .NET Aspire
participant Container as Pub/Sub Emulator
participant Configurator as Pub/Sub Configurator
Aspire->>Container: Start with PUBSUBn_PROJECT declarations
Note over Container: The image provisions declared topics and subscriptions
Container-->>Aspire: HTTP health check passes
Aspire->>Configurator: OnResourceReady
Configurator->>Configurator: Validate all declarations and coalesce equivalent subscriptions
Note over Configurator: Shared 60-second deadline for endpoint resolution and RPCs
Configurator->>Aspire: Resolve emulator endpoint
loop Each configured subscription
opt Push route configured
Configurator->>Aspire: Resolve MessageConfig.PushEndpointReference port
end
Configurator->>Container: GetSubscription (bounded SDK retries)
Configurator->>Container: UpdateSubscription (push, ack deadline, DLQ)
end
Configurator-->>Aspire: Configuration completed
Public methods
AddGcpPubSubβ adds the emulator.AddGcpPubSubUIβ adds the Pub/Sub UI.WithPubSubConfigsβ configures projects, topics and subscriptions.WithWaitTimeoutβ sets the emulator image initialization timeout in seconds; it does not change the subscription configuration deadline.WithConfigurationTimeoutβ sets the shared endpoint resolution and subscription configuration deadline per emulator, in seconds (default: 60); it does not change image initialization.WithReferenceβ wires projects to emulator/UI and sets env vars.
Default image
The default image is pinned to a tested SHA256 digest. Select another image/tag with WithDockerImage(image, tag) or Aspire image methods; the override clears the previous digest. See runtime contracts for image maintenance, readiness and emulator limitations.
Requirements
- .NET 9, 10 or 11
- Aspire.Hosting 13.6.5 on .NET 9; Aspire.Hosting 17.0.0 on .NET 10/11. AppHost examples use
Aspire.Hosting.Dotnet17 andAddDotnetProject; .NET 9 AppHosts retain theProjectResourcemodel. - Docker running locally
- Google.Cloud.PubSub.V1 >= 3.40.0
License
Apache-2.0
| Product | Versions 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. net11.0 is compatible. |
-
net10.0
- Aspire.Hosting (>= 17.0.0)
- Google.Cloud.PubSub.V1 (>= 3.40.0)
- MessagePack (>= 3.1.12)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 10.0.15)
-
net11.0
- Aspire.Hosting (>= 17.0.0)
- Google.Cloud.PubSub.V1 (>= 3.40.0)
- MessagePack (>= 3.1.12)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 11.0.0)
-
net9.0
- Aspire.Hosting (= 13.6.5)
- Google.Cloud.PubSub.V1 (>= 3.40.0)
- MessagePack (>= 3.1.12)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 9.0.3)
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 |
|---|---|---|
| 13.1.0 | 84 | 11/29/2026 |
| 13.0.0 | 71 | 11/28/2026 |
| 12.3.0 | 71 | 11/27/2026 |
| 12.2.1 | 64 | 11/27/2026 |
| 12.2.0 | 97 | 11/26/2026 |
| 12.1.0 | 94 | 11/26/2026 |
| 12.0.0 | 98 | 11/25/2026 |
| 11.0.2 | 108 | 11/21/2026 |
| 11.0.1 | 102 | 11/20/2026 |
| 11.0.0 | 107 | 11/13/2026 |
| 10.1.7 | 275 | 11/8/2026 |
| 10.1.6 | 95 | 11/8/2026 |
| 10.1.4 | 99 | 11/7/2026 |
| 10.1.3 | 118 | 11/1/2026 |
| 10.1.2 | 108 | 11/1/2026 |
| 10.1.1 | 110 | 10/31/2026 |
| 10.1.0 | 506 | 8/31/2026 |
| 10.0.0 | 138 | 8/30/2026 |
| 9.0.5 | 125 | 8/28/2026 |
| 9.0.4 | 167 | 6/26/2026 |