diff --git a/apis/Google.Cloud.PubSub.V1/Google.Cloud.PubSub.V1.Snippets/PublisherClientSnippets.cs b/apis/Google.Cloud.PubSub.V1/Google.Cloud.PubSub.V1.Snippets/PublisherClientSnippets.cs deleted file mode 100644 index 2de4a05bc003..000000000000 --- a/apis/Google.Cloud.PubSub.V1/Google.Cloud.PubSub.V1.Snippets/PublisherClientSnippets.cs +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 2023 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"). -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using Microsoft.Extensions.DependencyInjection; -using Xunit; - -namespace Google.Cloud.PubSub.V1.Snippets; - -public class PublisherClientSnippets -{ - [Fact] - public void AddPublisherClient() - { - string projectId = "projectId"; - string topicId = "topicId"; - var services = new ServiceCollection(); - - // Sample: AddPublisherClient - TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); - services.AddPublisherClient(topicName); - // End sample - } - - [Fact] - public void AddCustomizedPublisherClient() - { - string projectId = "projectId"; - string topicId = "topicId"; - var services = new ServiceCollection(); - - // Sample: AddCustomizedPublisherClient - TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); - services.AddPublisherClient(builder => - { - builder.TopicName = topicName; - builder.CredentialsPath = "path/to/credentials.json"; - // Other settings to customize. - }); - // End sample - } - - [Fact] - public void AddPublisherClientAndService() - { - string projectId = "projectId"; - string topicId = "topicId"; - var services = new ServiceCollection(); - - // Sample: AddPublisherClientAndService - TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); - services.AddPublisherClient(topicName); - services.AddSingleton(); - // End sample - } - - // Sample: UsePublisherClient - public class MyService - { - private readonly PublisherClient _publisherClient; - - public MyService(PublisherClient publisherClient) - { - _publisherClient = publisherClient; - } - - // Use the _publisherClient to publish messages. - } - // End sample -} diff --git a/apis/Google.Cloud.PubSub.V1/Google.Cloud.PubSub.V1.Snippets/SubscriberClientSnippets.cs b/apis/Google.Cloud.PubSub.V1/Google.Cloud.PubSub.V1.Snippets/SubscriberClientSnippets.cs index dd018ba2a9a7..282a0e4526e6 100644 --- a/apis/Google.Cloud.PubSub.V1/Google.Cloud.PubSub.V1.Snippets/SubscriberClientSnippets.cs +++ b/apis/Google.Cloud.PubSub.V1/Google.Cloud.PubSub.V1.Snippets/SubscriberClientSnippets.cs @@ -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 @@ -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(); - // 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(); - }) - .Build(); - - await host.RunAsync(); - // End sample - } - } - - // Sample: UseSubscriberClient - public class SubscriberService : BackgroundService - { - private readonly SubscriberClient _subscriberClient; - private readonly ILogger _logger; - - public SubscriberService(SubscriberClient subscriberClient, ILogger 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 } diff --git a/apis/Google.Cloud.PubSub.V1/docs/index.md b/apis/Google.Cloud.PubSub.V1/docs/index.md index 2a8f19fdc9cd..e736d2bb0fb0 100644 --- a/apis/Google.Cloud.PubSub.V1/docs/index.md +++ b/apis/Google.Cloud.PubSub.V1/docs/index.md @@ -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` 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` 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,