コード例 #1
0
        internal async Task ProcessSessionMessageAsync(ProcessSessionMessageEventArgs args)
        {
            EnsureIsRunning();

            _concurrencyUpdateManager?.MessageProcessed();

            using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _cancellationTokenSource.Token))
            {
                var actions = new ServiceBusSessionMessageActions(args);
                if (!await _sessionMessageProcessor.Value.BeginProcessingMessageAsync(actions, args.Message, linkedCts.Token).ConfigureAwait(false))
                {
                    return;
                }

                ServiceBusTriggerInput input = ServiceBusTriggerInput.CreateSingle(args.Message, actions, _client.Value);

                TriggeredFunctionData data   = input.GetTriggerFunctionData();
                FunctionResult        result = await _triggerExecutor.TryExecuteAsync(data, linkedCts.Token).ConfigureAwait(false);

                if (actions.ShouldReleaseSession)
                {
                    args.ReleaseSession();
                }

                await _sessionMessageProcessor.Value.CompleteProcessingMessageAsync(actions, args.Message, result, linkedCts.Token).ConfigureAwait(false);
            }
        }
コード例 #2
0
        public void ProcessMessageAsync_LogsWarning_Disposed_Session()
        {
            try
            {
                _listener.Disposed = true;
                var message = ServiceBusModelFactory.ServiceBusReceivedMessage(
                    messageId: Guid.NewGuid().ToString(),
                    sessionId: Guid.NewGuid().ToString(),
                    sequenceNumber: 1,
                    deliveryCount: 55,
                    enqueuedTime: DateTimeOffset.Now,
                    lockedUntil: DateTimeOffset.Now);
                var receiver = new Mock <ServiceBusSessionReceiver>().Object;
                var args     = new ProcessSessionMessageEventArgs(message, receiver, CancellationToken.None);

                Assert.That(
                    async() => await _listener.ProcessSessionMessageAsync(args),
                    Throws.InstanceOf <InvalidOperationException>());

                Assert.NotNull(_loggerProvider.GetAllLogMessages()
                               .SingleOrDefault(
                                   x => x.FormattedMessage.StartsWith("Message received for a listener that is not in a running state. The message will not be delivered to the function, " +
                                                                      "and instead will be abandoned. (Listener started = True, Listener disposed = True") && x.Level == LogLevel.Warning));
            }
            finally
            {
                _listener.Disposed = false;
                _loggerProvider.ClearAllLogMessages();
            }
        }
コード例 #3
0
        protected internal override async Task OnProcessSessionMessageAsync(ProcessSessionMessageEventArgs args)
        {
            foreach (var plugin in _plugins)
            {
                await plugin.Invoke(args.Message);
            }

            await base.OnProcessSessionMessageAsync(args);
        }
コード例 #4
0
            static async Task MessageHandler(ProcessSessionMessageEventArgs args)
            {
                var body = args.Message.Body.ToString();

                Console.WriteLine($"Received Message: { body }");

                // we can evaluate application logic and use that to determine how to settle the message.
                await args.CompleteMessageAsync(args.Message);
            }
        /// <summary>
        /// Initialize a received item with all Sql Transactional Outbox data.  The resulting item will
        ///     allow for specific handling of the Message Acknowledgement/Rejection/Dead-lettering just as.
        ///     the ProcessMessageEventArgs allow...
        /// </summary>
        /// <param name="sessionMessageEventArgs"></param>
        /// <param name="outboxItemFactory"></param>
        public AzureServiceBusReceivedItem(
            ProcessSessionMessageEventArgs sessionMessageEventArgs,
            ISqlTransactionalOutboxItemFactory <TUniqueIdentifier, TPayloadBody> outboxItemFactory
            )
        {
            this.SessionMessageEventArgs = sessionMessageEventArgs.AssertNotNull(nameof(sessionMessageEventArgs));
            this.AzureServiceBusMessage  = sessionMessageEventArgs.Message;
            this.OutboxItemFactory       = outboxItemFactory.AssertNotNull(nameof(outboxItemFactory));

            this.InitAzureServiceBusOutboxReceivedItem(isFifoProcessingEnabled: true);
        }
コード例 #6
0
        private static async Task MessageHandler(ProcessSessionMessageEventArgs args)
        {
            string body = args.Message.Body.ToString();

            Console.WriteLine($"Message received from session processor: {body}");

            // we can evaluate application logic and use that to determine how to settle the message.
            await args.CompleteMessageAsync(args.Message);

            // we can also set arbitrary session state using this receiver
            // the state is specific to the session, and not any particular message
            await args.SetSessionStateAsync(new BinaryData("some state"));
        }
コード例 #7
0
        public async Task UserSettledPropertySetCorrectly()
        {
            var msg  = new ServiceBusReceivedMessage();
            var args = new ProcessSessionMessageEventArgs(
                msg,
                new Mock <ServiceBusSessionReceiver>().Object,
                CancellationToken.None);

            Assert.IsFalse(msg.IsSettled);

            msg.IsSettled = false;
            await args.AbandonMessageAsync(msg);

            Assert.IsTrue(msg.IsSettled);

            await args.CompleteMessageAsync(msg);

            Assert.IsTrue(msg.IsSettled);

            msg.IsSettled = false;
            await args.DeadLetterMessageAsync(msg);

            Assert.IsTrue(msg.IsSettled);

            msg.IsSettled = false;
            await args.DeadLetterMessageAsync(msg, "reason");

            Assert.IsTrue(msg.IsSettled);

            msg.IsSettled = false;
            await args.DeferMessageAsync(msg);

            Assert.IsTrue(msg.IsSettled);

            // getting or setting session state doesn't count as settling
            msg.IsSettled = false;
            await args.GetSessionStateAsync();

            Assert.IsFalse(msg.IsSettled);

            msg.IsSettled = false;
            await args.SetSessionStateAsync(new byte[] { });

            Assert.IsFalse(msg.IsSettled);
        }
コード例 #8
0
        internal async Task ProcessSessionMessageAsync(ProcessSessionMessageEventArgs args)
        {
            using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _cancellationTokenSource.Token))
            {
                var actions = new ServiceBusSessionMessageActions(args);
                if (!await _sessionMessageProcessor.BeginProcessingMessageAsync(actions, args.Message, linkedCts.Token).ConfigureAwait(false))
                {
                    return;
                }

                ServiceBusTriggerInput input = ServiceBusTriggerInput.CreateSingle(args.Message);
                input.MessageActions = actions;

                TriggeredFunctionData data   = input.GetTriggerFunctionData();
                FunctionResult        result = await _triggerExecutor.TryExecuteAsync(data, linkedCts.Token).ConfigureAwait(false);

                await _sessionMessageProcessor.CompleteProcessingMessageAsync(actions, args.Message, result, linkedCts.Token).ConfigureAwait(false);
            }
        }
コード例 #9
0
        internal async Task ProcessSessionMessageAsync(ProcessSessionMessageEventArgs args)
        {
            using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _cancellationTokenSource.Token))
            {
                ServiceBusSessionReceiver receiver = (ServiceBusSessionReceiver)typeof(ProcessSessionMessageEventArgs).GetField("_sessionReceiver", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(args);

                if (!await _sessionMessageProcessor.BeginProcessingMessageAsync(receiver, args.Message, linkedCts.Token).ConfigureAwait(false))
                {
                    return;
                }

                ServiceBusTriggerInput input = ServiceBusTriggerInput.CreateSingle(args.Message);
                input.SessionReceiver = receiver;

                TriggeredFunctionData data   = input.GetTriggerFunctionData();
                FunctionResult        result = await _triggerExecutor.TryExecuteAsync(data, linkedCts.Token).ConfigureAwait(false);

                await _sessionMessageProcessor.CompleteProcessingMessageAsync(receiver, args.Message, result, linkedCts.Token).ConfigureAwait(false);
            }
        }
コード例 #10
0
 internal ServiceBusSessionMessageActions(ProcessSessionMessageEventArgs eventArgs) : base(eventArgs)
 {
     _eventArgs = eventArgs;
 }
コード例 #11
0
        public void UserSettledPropertySetCorrectlyOnException()
        {
            var msg          = new ServiceBusReceivedMessage();
            var mockReceiver = new Mock <ServiceBusSessionReceiver>();

            mockReceiver
            .Setup(receiver => receiver.AbandonMessageAsync(
                       It.IsAny <ServiceBusReceivedMessage>(),
                       It.IsAny <IDictionary <string, object> >(),
                       It.IsAny <CancellationToken>()))
            .Throws(new Exception());

            mockReceiver
            .Setup(receiver => receiver.DeferMessageAsync(
                       It.IsAny <ServiceBusReceivedMessage>(),
                       It.IsAny <IDictionary <string, object> >(),
                       It.IsAny <CancellationToken>()))
            .Throws(new Exception());

            mockReceiver
            .Setup(receiver => receiver.CompleteMessageAsync(
                       It.IsAny <ServiceBusReceivedMessage>(),
                       It.IsAny <CancellationToken>()))
            .Throws(new Exception());

            mockReceiver
            .Setup(receiver => receiver.DeadLetterMessageAsync(
                       It.IsAny <ServiceBusReceivedMessage>(),
                       It.IsAny <IDictionary <string, object> >(),
                       It.IsAny <CancellationToken>()))
            .Throws(new Exception());

            mockReceiver
            .Setup(receiver => receiver.DeadLetterMessageAsync(
                       It.IsAny <ServiceBusReceivedMessage>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <CancellationToken>()))
            .Throws(new Exception());

            var args = new ProcessSessionMessageEventArgs(
                msg,
                mockReceiver.Object,
                CancellationToken.None);

            Assert.IsFalse(msg.IsSettled);

            msg.IsSettled = false;
            Assert.That(async() => await args.AbandonMessageAsync(msg),
                        Throws.InstanceOf <Exception>());
            Assert.IsFalse(msg.IsSettled);

            Assert.That(async() => await args.CompleteMessageAsync(msg),
                        Throws.InstanceOf <Exception>());
            Assert.IsFalse(msg.IsSettled);

            msg.IsSettled = false;
            Assert.That(async() => await args.DeadLetterMessageAsync(msg),
                        Throws.InstanceOf <Exception>());
            Assert.IsFalse(msg.IsSettled);

            msg.IsSettled = false;
            Assert.That(async() => await args.DeadLetterMessageAsync(msg, "reason"),
                        Throws.InstanceOf <Exception>());
            Assert.IsFalse(msg.IsSettled);

            msg.IsSettled = false;
            Assert.That(async() => await args.DeferMessageAsync(msg),
                        Throws.InstanceOf <Exception>());
            Assert.IsFalse(msg.IsSettled);
        }
コード例 #12
0
 internal ServiceBusMessageActions(ProcessSessionMessageEventArgs sessionEventArgs)
 {
     _sessionEventArgs = sessionEventArgs;
 }