Пример #1
0
        public override void Dispose()
        {
            var subscriber = new SubscriberServiceApiClientBuilder {
                EmulatorDetection = EmulatorDetection.EmulatorOrProduction
            }.Build();
            var subscriptions = subscriber.ListSubscriptions(new ProjectName(ProjectId))
                                .Where(sub => sub.SubscriptionName.SubscriptionId.StartsWith(SubscriptionPrefix))
                                .ToList();

            foreach (var sub in subscriptions)
            {
                subscriber.DeleteSubscription(sub.SubscriptionName);
            }

            var publisher = new PublisherServiceApiClientBuilder {
                EmulatorDetection = EmulatorDetection.EmulatorOrProduction
            }.Build();
            var topics = publisher.ListTopics(new ProjectName(ProjectId))
                         .Where(topic => topic.TopicName.TopicId.StartsWith(TopicPrefix))
                         .ToList();

            foreach (var topic in topics)
            {
                publisher.DeleteTopic(topic.TopicName);
            }
        }
Пример #2
0
        private PublisherServiceApiClient CreatePublisher()
        {
            var publisherBuilder = new PublisherServiceApiClientBuilder();

            publisherBuilder.ChannelCredentials = ChannelCredentials.Insecure;
            publisherBuilder.Endpoint           = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");
            var publisher = publisherBuilder.Build();

            return(publisher);
        }
        /// <summary>
        /// Creates a PublisherClient given a path to a downloaded json service
        /// credentials file.
        /// </summary>
        /// <param name="jsonPath">The path to the downloaded json file.</param>
        /// <returns>A new publisher client.</returns>
        public static PublisherServiceApiClient CreatePublisherWithServiceCredentials(
            string jsonPath)
        {
            PublisherServiceApiClientBuilder builder = new PublisherServiceApiClientBuilder
            {
                CredentialsPath = jsonPath
            };

            return(builder.Build());
        }
Пример #4
0
        public void ManualConnection()
        {
            // Sample: ManualConnection
            // For example, "localhost:8615"
            string emulatorHostAndPort = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");

            PublisherServiceApiClient client = new PublisherServiceApiClientBuilder
            {
                Endpoint           = emulatorHostAndPort,
                ChannelCredentials = ChannelCredentials.Insecure
            }.Build();
            // End sample
        }
Пример #5
0
        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));
        }
Пример #6
0
        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));
Пример #7
0
        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
        }
Пример #8
0
        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
        }
Пример #9
0
        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
        }