public string Serialise(Message message)
        {
            var settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                Converters = new[] { _enumConverter }
            };

            return JsonConvert.SerializeObject(message, settings);
        }
Exemple #2
0
    private PublishRequest BuildPublishRequest(Message message, PublishMetadata metadata)
    {
        var messageToSend = _serializationRegister.Serialize(message, serializeForSnsPublishing: true);
        var messageType   = _messageSubjectProvider.GetSubjectForType(message.GetType());

        return(new PublishRequest
        {
            TopicArn = Arn,
            Subject = messageType,
            Message = messageToSend,
            MessageAttributes = BuildMessageAttributes(metadata)
        });
    }
Exemple #3
0
    private protected override Task <SqsMessagePublisher> CreateSystemUnderTestAsync()
    {
        var sqs = new SqsMessagePublisher(new Uri(Url), Sqs, _serializationRegister, Substitute.For <ILoggerFactory>())
        {
            MessageResponseLogger = (r, m) =>
            {
                _response = r;
                _message  = m;
            }
        };

        return(Task.FromResult(sqs));
    }
Exemple #4
0
    private SendMessageRequest BuildSendMessageRequest(Message message, PublishMetadata metadata)
    {
        var request = new SendMessageRequest
        {
            MessageBody = GetMessageInContext(message),
            QueueUrl    = QueueUrl.AbsoluteUri,
        };

        if (metadata?.Delay != null)
        {
            request.DelaySeconds = (int)metadata.Delay.Value.TotalSeconds;
        }

        return(request);
    }
        public string Serialise(Message message, bool serializeForSnsPublishing)
        {
            var settings = GetJsonSettings();

            var msg = JsonConvert.SerializeObject(message, settings);

            // AWS SNS service will add Subject and Message properties automatically, 
            // so just return plain message
            if (serializeForSnsPublishing)
                return msg;

            // for direct publishing to SQS, add Subject and Message properties manually
            var context = new { Subject = message.GetType().Name, Message = msg };
            return JsonConvert.SerializeObject(context);
        }
Exemple #6
0
    public async Task PublishAsync(Message message, PublishMetadata metadata, CancellationToken cancellationToken)
    {
        if (QueueUrl is null)
        {
            throw new PublishException("Queue URL was null, perhaps you need to call `StartAsync` on the `IMessagePublisher` before publishing.");
        }

        var request = BuildSendMessageRequest(message, metadata);
        SendMessageResponse response;

        try
        {
            response = await _client.SendMessageAsync(request, cancellationToken).ConfigureAwait(false);
        }
        catch (AmazonServiceException ex)
        {
            throw new PublishException(
                      $"Failed to publish message to SQS. {nameof(request.QueueUrl)}: {request.QueueUrl},{nameof(request.MessageBody)}: {request.MessageBody}",
                      ex);
        }

        using (_logger.BeginScope(new Dictionary <string, object>
        {
            ["AwsRequestId"] = response?.MessageId
        }))
        {
            _logger.LogInformation(
                "Published message {MessageId} of type {MessageType} to {DestinationType} '{MessageDestination}'.",
                message.Id,
                message.GetType().FullName,
                "Queue",
                request.QueueUrl);
        }

        if (MessageResponseLogger != null)
        {
            var responseData = new MessageResponse
            {
                HttpStatusCode   = response?.HttpStatusCode,
                MessageId        = response?.MessageId,
                ResponseMetadata = response?.ResponseMetadata
            };
            MessageResponseLogger.Invoke(responseData, message);
        }
    }
Exemple #7
0
    public async Task PublishAsync(Message message, PublishMetadata metadata, CancellationToken cancellationToken)
    {
        var             request  = BuildPublishRequest(message, metadata);
        PublishResponse response = null;

        try
        {
            response = await Client.PublishAsync(request, cancellationToken).ConfigureAwait(false);
        }
        catch (AmazonServiceException ex)
        {
            if (!ClientExceptionHandler(ex, message))
            {
                throw new PublishException(
                          $"Failed to publish message to SNS. Topic ARN: '{request.TopicArn}', Subject: '{request.Subject}', Message: '{request.Message}'.",
                          ex);
            }
        }

        using (_logger.BeginScope(new Dictionary <string, object>
        {
            ["AwsRequestId"] = response?.MessageId
        }))
        {
            _logger.LogInformation(
                "Published message {MessageId} of type {MessageType} to {DestinationType} '{MessageDestination}'.",
                message.Id,
                message.GetType().FullName,
                "Topic",
                request.TopicArn);
        }

        if (MessageResponseLogger != null)
        {
            var responseData = new MessageResponse
            {
                HttpStatusCode   = response?.HttpStatusCode,
                MessageId        = response?.MessageId,
                ResponseMetadata = response?.ResponseMetadata
            };
            MessageResponseLogger.Invoke(responseData, message);
        }
    }
Exemple #8
0
    protected virtual void Given()
    {
        _typedMessage = new OrderAccepted();

        _sqsMessage = new SQSMessage
        {
            Body          = JsonConvert.SerializeObject(_typedMessage),
            ReceiptHandle = "i_am_receipt_handle"
        };

        IEnumerable <SQSMessage> GetMessages()
        {
            yield return(_sqsMessage);
        }

        _queue = new FakeSqsQueue(ct => Task.FromResult(GetMessages()))
        {
            Uri = new Uri(ExpectedQueueUrl)
        };
        _serializationRegister.DeserializeMessage(Arg.Any <string>())
        .Returns(new MessageWithAttributes(_typedMessage, new MessageAttributes()));
    }
Exemple #9
0
 public async Task PublishAsync(Message message, CancellationToken cancellationToken)
 => await PublishAsync(message, null, cancellationToken).ConfigureAwait(false);
Exemple #10
0
 public string GetMessageInContext(Message message) => _serializationRegister.Serialize(message, serializeForSnsPublishing: false);
        public string Serialise(Message message)
        {
            var settings = GetJsonSettings();

            return JsonConvert.SerializeObject(message, settings);
        }
Exemple #12
0
 public Task PublishAsync(Message message, CancellationToken cancellationToken)
 => PublishAsync(message, null, cancellationToken);
Exemple #13
0
 private bool ClientExceptionHandler(Exception ex, Message message) => _handleException?.Invoke(ex, message) ?? false;