Пример #1
0
        public void StartConsuming(IMessageTriggerHandler messageHandler)
        {
            ValidateProjectIdIsSet();
            ValidateSubscriptionIdIsSet();

            messageHandler.ThrowExceptionIfNull("messageHandler");

            _subscriberService.StartConsuming(_projectId, _subscriptionId, messageHandler);
        }
        private async Task StartConsumingAsync(string projectId, string subscriptionId, IMessageTriggerHandler messageHandler)
        {
            var subscriptionName = new SubscriptionName(projectId, subscriptionId);

            _subscriberClient = await SubscriberClient.CreateAsync(subscriptionName);

            //Blame google
            #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            _subscriberClient.StartAsync((msg, cancellationToken) =>
            {
                var message = msg.Data.ToStringUtf8();

                Task.Run(() => messageHandler.HandleMessage(message), cancellationToken);

                return(Task.FromResult(SubscriberClient.Reply.Ack));
            }).ConfigureAwait(false);
            #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        }
        public void StartConsuming(string projectId, string subscriptionId, IMessageTriggerHandler messageHandler)
        {
            var createSubscriptionTask = StartConsumingAsync(projectId, subscriptionId, messageHandler);

            createSubscriptionTask.WaitAndUnwrapException();
        }