示例#1
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);
                }
            }

            _logger.LogInformation(
                "Published message with subject '{MessageSubject}' and content '{MessageBody}'.",
                request.Subject,
                request.Message);

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

            try
            {
                var response = await Client.PublishAsync(request, cancellationToken).ConfigureAwait(false);

                _eventLog.LogInformation($"Published message: '{request.Subject}' with content {request.Message}");

                MessageResponseLogger?.Invoke(new MessageResponse
                {
                    HttpStatusCode = response?.HttpStatusCode,
                    MessageId      = response?.MessageId
                }, message);
            }
            catch (Exception ex)
            {
                if (!ClientExceptionHandler(ex, message))
                {
                    throw new PublishException(
                              $"Failed to publish message to SNS. TopicArn: {request.TopicArn} Subject: {request.Subject} Message: {request.Message}",
                              ex);
                }
            }
        }
示例#3
0
        public void Publish(Message message)
        {
            var request = BuildPublishRequest(message);

            try
            {
                var response = Client.Publish(request);
                _eventLog.LogInformation($"Published message: '{request.Subject}' with content {request.Message}");

                MessageResponseLogger?.Invoke(new MessageResponse
                {
                    HttpStatusCode = response?.HttpStatusCode,
                    MessageId      = response?.MessageId
                }, message);
            }
            catch (Exception ex)
            {
                if (!ClientExceptionHandler(ex, message))
                {
                    throw new PublishException(
                              $"Failed to publish message to SNS. TopicArn: {request.TopicArn} Subject: {request.Subject} Message: {request.Message}",
                              ex);
                }
            }
        }
示例#4
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);
        }
    }
示例#5
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);
        }
    }