Skip to content

Commit

Permalink
chore: Remove Pub/Sub specific DI docs.
Browse files Browse the repository at this point in the history
These add nothing over our general DI documentations
  • Loading branch information
amanda-tarafa committed Oct 5, 2023
1 parent 269b187 commit c3ad3ab
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 241 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
// limitations under the License.

using Google.Api.Gax;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Google.Cloud.PubSub.V1.Snippets
Expand All @@ -40,91 +35,5 @@ public void Emulator()
// Use subscriber.StartAsync etc as normal
// End sample
}

[Fact]
public void AddSubscriberClient()
{
string projectId = "projectId";
string subscriptionId = "subscriptionId";
var services = new ServiceCollection();

// Sample: AddSubscriberClient
SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);
services.AddSubscriberClient(subscriptionName);
// End sample
}

[Fact]
public void AddCustomizedSubscriberClient()
{
string projectId = "projectId";
string subscriptionId = "subscriptionId";
var services = new ServiceCollection();

// Sample: AddCustomizedSubscriberClient
SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);
services.AddSubscriberClient(builder =>
{
builder.SubscriptionName = subscriptionName;
builder.CredentialsPath = "path/to/credentials.json";
// Other settings to customize the client.
});
// End sample
}

[Fact]
public void AddHostedService()
{
var services = new ServiceCollection();

// Sample: AddHostedService
services.AddHostedService<SubscriberService>();
// End sample
}

internal async Task UseSubscriberServiceInConsoleApp()
{
string projectId = "projectId";
string subscriptionId = "subscriptionId";

// Sample: UseSubscriberServiceInConsoleApp
// Add `using Microsoft.Extensions.Hosting;` in the using directives.
var host = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);
services.AddSubscriberClient(subscriptionName);
services.AddHostedService<SubscriberService>();
})
.Build();

await host.RunAsync();
// End sample
}
}

// Sample: UseSubscriberClient
public class SubscriberService : BackgroundService
{
private readonly SubscriberClient _subscriberClient;
private readonly ILogger<SubscriberService> _logger;

public SubscriberService(SubscriberClient subscriberClient, ILogger<SubscriberService> logger)
{
_subscriberClient = subscriberClient;
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken) =>
await _subscriberClient.StartAsync((msg, token) =>
{
_logger.LogInformation($"Received message {msg.MessageId}: {msg.Data.ToStringUtf8()}");
// Handle the message.
return Task.FromResult(SubscriberClient.Reply.Ack);
});

public override async Task StopAsync(CancellationToken stoppingToken) =>
await _subscriberClient.StopAsync(stoppingToken);
}
// End sample
}
70 changes: 0 additions & 70 deletions apis/Google.Cloud.PubSub.V1/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,76 +82,6 @@ restart listening for messages with `StartAsync(...)` again. Due to the expense
instance, it is recommended that a singleton client per topic is used for the lifetime of the
application.

## Dependency Injection

Both `PublisherClient` and `SubscriberClient` can be easily integrated with the [dependency injection](https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection)
container provided by the [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/) package.
The `Google.Cloud.PubSub.V1` package provides extension methods to register the clients with the dependency
injection container in the `Microsoft.Extensions.DependencyInjection` namespace.

### PublisherClient

To register a singleton `PublisherClient` instance with default settings in the `IServiceCollection`, use the
`AddPublisherClient` extension method as shown below:

{{sample:PublisherClient.AddPublisherClient}}

There is an overload of the `AddPublisherClient` method that takes `Action<PublisherClientBuilder>` as a parameter
and can be used to add the customized `PublisherClient` singleton instance as shown below:

{{sample:PublisherClient.AddCustomizedPublisherClient}}

The registered `PublisherClient` can then be used like any other service registered with the dependency injection container. For instance, in a `MyService` class that is itself registered with the dependency injection container,
the `PublisherClient` can be passed as a constructor parameter.
See [dependency injection](https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection) for more information.

Below code shows the registration of `MyService` class with the dependency injection container:

{{sample:PublisherClient.AddPublisherClientAndService}}

The `PublisherClient` can then be used in the `MyService` class as shown below:

{{sample:PublisherClient.UsePublisherClient}}

When the application exits, the `DisposeAsync` method of the `PublisherClient` will be invoked by the dependency injection container to gracefully shut down the client. See
[Disposing of the publisher and subscriber clients](#disposing-of-the-publisher-and-subscriber-clients) for more information about what happens when disposing the `PublisherClient`.

### SubscriberClient

To register a singleton `SubscriberClient` instance with default settings in the `IServiceCollection`, use the
`AddSubscriberClient` extension method as shown below:

{{sample:SubscriberClient.AddSubscriberClient}}

There is an overload of the `AddSubscriberClient` method that takes `Action<SubscriberClientBuilder>` as a parameter
and can be used to add the customized `SubscriberClient` singleton instance as shown below:

{{sample:SubscriberClient.AddCustomizedSubscriberClient}}

Registering the `SubscriberClient` doesn't automatically start the client. It needs to be started explicitly by calling the `StartAsync` method.
The `SubscriberClient` is a long-running client and so it may be useful to use
it in a background service. The background service can use the `SubscriberClient`
registered with the dependency injection container and handle the messages in the background.

The background services can be registered with the dependency injection container
using the [`AddHostedService`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollectionhostedserviceextensions.addhostedservice?view=dotnet-plat-ext-6.0) extension method as shown below:

{{sample:SubscriberClient.AddHostedService}}

Here `SubscriberService` is the class that implements `BackgroundService` and uses the `SubscriberClient`
registered with the dependency injection container to handle the messages. Once the background service is registered,
it will be automatically started when the application starts and stopped when the application exits.
A sample implementation of `SubscriberService` is shown below:

{{sample:SubscriberClient.UseSubscriberClient}}

During application shutdown, the `StopAsync` method of the `SubscriberService` is invoked by the dependency injection container, which in turn calls
the `StopAsync` method of the `SubscriberClient` to gracefully shut down the client.

Below is an example implementation of a console application that utilizes the dependency injection container and the `SubscriberService` to handle messages:

{{sample:SubscriberClient.UseSubscriberServiceInConsoleApp}}

## Disposing of the publisher and subscriber clients

Both `PublisherClient` and `SubscriberClient` implement the `IAsyncDisposable` interface,
Expand Down

0 comments on commit c3ad3ab

Please sign in to comment.