private async Task ProcessSessionMessageAsync(IReceiverClient client, IMessageSession session, Message message, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Type : {ClientType}", client.GetType());

            try
            {
                _logger.LogInformation($"Session       :{session.SessionId}");
                _logger.LogInformation($"SequenceNumber:{message.SystemProperties.SequenceNumber}");
                _logger.LogInformation($"Label         :{message.Label}");
                _logger.LogInformation($"DeliveryCount :{message.SystemProperties.DeliveryCount}");

                string body = Encoding.UTF8.GetString(message.Body);
                if (body == "1")
                {
                    await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); // simulate delay

                    _logger.LogError("Error: " + DateTime.UtcNow);
                    throw new Exception("!!!");
                }

                //if (message.Label == nameof(PersonMessage))
                //{
                //    var person = _serializer.Deserialize<PersonMessage>(message.Body);
                //    _logger.LogInformation("person = " + JsonConvert.SerializeObject(person));
                //}
                //else
                //{
                //    _logger.LogInformation("other = " + body);
                //}

                _logger.LogInformation("OK : " + body);

                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); // simulate delay

                await session.CompleteAsync(message.SystemProperties.LockToken);

                //await client.CompleteAsync(message.SystemProperties.LockToken);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error while Process Message with MessageId: {MessageId}", message.MessageId);
                if (!session.IsClosedOrClosing)
                {
                    var properties = new Dictionary <string, object>
                    {
                        { "uhuh", DateTime.UtcNow.ToString() }
                    };

                    await session.AbandonAsync(message.SystemProperties.LockToken, properties);

                    //await client.AbandonAsync(message.SystemProperties.LockToken);
                }
            }
        }
Beispiel #2
0
        private async Task ProcessMessageAsync(IReceiverClient client, IMessageSession session, Message message, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Type : {ClientType}", client.GetType());

            try
            {
                _logger.LogInformation("SystemProperties:{SystemProperties}", JsonConvert.SerializeObject(message.SystemProperties, Formatting.Indented));
                _logger.LogInformation("IMessageSession :{IMessageSession}", JsonConvert.SerializeObject(session, Formatting.Indented));

                string body = Encoding.UTF8.GetString(message.Body);
                if (body == "1")
                {
                    await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); // simulate delay

                    _logger.LogError("Error: " + DateTime.UtcNow);
                    throw new Exception("!!!");
                }

                if (message.Label == nameof(PersonMessage))
                {
                    var person = _serializer.Deserialize <PersonMessage>(message.Body);
                    _logger.LogInformation("person = " + JsonConvert.SerializeObject(person));
                }
                else
                {
                    _logger.LogInformation("body = {body}", body);
                }

                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); // simulate delay

                await session.CompleteAsync(message.SystemProperties.LockToken);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error while Process Message with MessageId: {MessageId}", message.MessageId);
                if (!session.IsClosedOrClosing)
                {
                    await session.AbandonAsync(message.SystemProperties.LockToken);
                }
            }
        }