示例#1
0
        public async Task SubscribeUserCreationAsync(Func <User, Task <SubscriberClient.Reply> > feedHandler, CancellationToken stoppingToken)
        {
            _logger.LogInformation($"Initializing subscription for {_pubSubCredentials} using { _userCreationSubscription }");
            SubscriberServiceApiClient subscriber = PubSubHelper.CreateSubscriber(_pubSubCredentials);

            await SubscribeAsync(subscriber, _userCreationSubscription, feedHandler, stoppingToken);
        }
示例#2
0
        public async Task <bool> CreateUserAsync(User user)
        {
            var publisher = PubSubHelper.CreatePublisher(_pubSubCredentials);

            await PublishAsync(publisher, _userCreationTopic, user);

            return(true);
        }
示例#3
0
        private async Task SubscribeAsync <T>(SubscriberServiceApiClient subscriber, SubscriptionName subscription, Func <T, Task <SubscriberClient.Reply> > feedHandler, CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                /// The returnImmediately parameter allows the pull async function
                /// to get a response even when the channel is empty.
                /// You can set this to false so that the current context
                /// waits for a message for a bounded amount of time
                PullResponse response = await subscriber.PullAsync(
                    subscription,
                    returnImmediately : true,
                    maxMessages : 20
                    );

                foreach (var receivedMessage in response.ReceivedMessages)
                {
                    var mesId   = receivedMessage.Message.MessageId;
                    var message = receivedMessage.Message;
                    _logger.LogDebug($"Message received: {message.Data.ToStringUtf8()}");
                    try
                    {
                        /// The extraction process assumes that the best practice of
                        /// wrapping PubSub data in base64 encoding, thus it goes through
                        /// the process of decoding before finally parsing it as a C# object
                        /// This can be disabled by setting the base64decode parameter to false
                        /// e.g. PubSubHelper.Extract<T>(message, false)
                        var extracted          = PubSubHelper.Extract <T>(message);
                        var subscriptionResult = await feedHandler(extracted);

                        if (subscriptionResult == SubscriberClient.Reply.Ack)
                        {
                            await subscriber.AcknowledgeAsync(subscription, new[] { receivedMessage.AckId });
                        }
                    }
                    catch (Exception exc)
                    {
                        _logger.LogError($"Error in subscription {subscription.SubscriptionId}\n {exc.Message}\n {exc.StackTrace}");
                    }
                }

                // Add 10s delay per pull
                await Task.Delay(1000);
            }
        }
示例#4
0
        private Task <PublishResponse> PublishAsync <T>(PublisherServiceApiClient publisher, TopicName topic, T obj)
        {
            var message = PubSubHelper.CreateMessage(obj);

            return(publisher.PublishAsync(topic, new[] { message }));
        }