public async Task <string> PublishOneAsync <T>(T message, string withSubscriptionId = null, CancellationToken cancellationToken = default) { //PublisherServiceApiClient publisherService = await PublisherServiceApiClient.CreateAsync PublisherServiceApiClient publisherService = new PublisherServiceApiClientBuilder { Endpoint = endpoint, ChannelCredentials = ChannelCredentials.Insecure }.Build(); TopicName topicName = new TopicName(projectId, topicId); try { publisherService.CreateTopic(topicName, CallSettings.FromCancellationToken(cancellationToken)); } catch { //already exists } if (!string.IsNullOrWhiteSpace(withSubscriptionId)) { //SubscriberServiceApiClient subscriberService = await SubscriberServiceApiClient.CreateAsync(); SubscriberServiceApiClient subscriberService = new SubscriberServiceApiClientBuilder { Endpoint = endpoint, ChannelCredentials = ChannelCredentials.Insecure }.Build(); SubscriptionName subscriptionName = new SubscriptionName(projectId, withSubscriptionId); try { subscriberService.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60); } catch { //already exists } } return(await PublishAsync(topicName, endpoint, message)); }
static async Task Main(string[] args) { Environment.SetEnvironmentVariable("PUBSUB_EMULATOR_HOST", "localhost:8681"); var emulatorHostAndPort = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST"); var pubsubProjectId = Environment.GetEnvironmentVariable("PUBSUB_PROJECT_ID"); //Environment.SetEnvironmentVariable("GCP_SERVICE_ACCOUNT_JSON", ""); //Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", ""); //PublisherServiceApiClient publisherService = await PublisherServiceApiClient.CreateAsync PublisherServiceApiClient publisherService = new PublisherServiceApiClientBuilder { Endpoint = emulatorHostAndPort, ChannelCredentials = ChannelCredentials.Insecure }.Build(); TopicName topicName = new TopicName(projectId, topicId); try { publisherService.CreateTopic(topicName, CallSettings.FromCancellationToken(default));
public void Overrides() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); DateTime deadline = DateTime.MaxValue; CancellationToken cancellationToken = new CancellationTokenSource().Token; // Sample: Overrides // Create a default PublisherServiceApiSettings, with customizations for CreateTopic RPCs: // * A custom "ClientVersion" header. // * A custom 5-second timeout Timing. // * No cancellation token. PublisherServiceApiSettings publisherSettings = new PublisherServiceApiSettings(); publisherSettings.CreateTopicSettings = publisherSettings.CreateTopicSettings .WithCancellationToken(CancellationToken.None) .WithTimeout(TimeSpan.FromSeconds(5)) .WithHeader("ClientVersion", "1.0.0"); // Override the above Timing and CancellationToken in the client-wide CallSettings; // the Headers are not overridden. publisherSettings.CallSettings = CallSettings .FromExpiration(Expiration.FromDeadline(deadline)) .WithCancellationToken(CancellationToken.None); // Create the client with the configured publisherSettings PublisherServiceApiClient client = new PublisherServiceApiClientBuilder { Settings = publisherSettings }.Build(); // Create a topic name from the projectId and topicId. TopicName topicName = new TopicName(projectId, topicId); // Call CreateTopic(). Override only the CancellationToken, using a per-RPC-method CallSettings. // The CallSettings used during this RPC invocation is: // * A custom "ClientVersion" header. // * A Timing deadline of 'deadline' (*not* the overridden 5-second timeout). // * The CancellationToken 'cancellationToken' (*not* CancellationToken.None). Topic topic = client.CreateTopic(topicName, CallSettings.FromCancellationToken(cancellationToken)); // End sample }
public void Emulator() { // Sample: Emulator // [START pubsub_use_emulator] // For example, "localhost:8615" string emulatorHostAndPort = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST"); PublisherServiceApiClient client = new PublisherServiceApiClientBuilder { Endpoint = emulatorHostAndPort, ChannelCredentials = ChannelCredentials.Insecure }.Build(); client.CreateTopic(new TopicName("project", "topic")); foreach (var topic in client.ListTopics(new ProjectName("project"))) { Console.WriteLine(topic.Name); } // [END pubsub_use_emulator] // End sample }
public void ClientPerMethod() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Sample: ClientPerMethod // Create a default PublisherServiceApiSettings, with a custom header for calls // to the CreateTopic RPC method. PublisherServiceApiSettings publisherSettings = new PublisherServiceApiSettings(); publisherSettings.CreateTopicSettings = publisherSettings.CreateTopicSettings.WithHeader("ClientVersion", "1.0.0"); PublisherServiceApiClient client = new PublisherServiceApiClientBuilder { Settings = publisherSettings }.Build(); // Create a topic name from the projectId and topicId. TopicName topicName = new TopicName(projectId, topicId); // The custom 'ClientVersion' header will be included in the RPC call, due to // the client being configured with 'publishersettings' above. Topic topic = client.CreateTopic(topicName); // End sample }