public string Serialise(Message message) { var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Converters = new[] { _enumConverter } }; return JsonConvert.SerializeObject(message, settings); }
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) }); }
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)); }
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); }
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); } }
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); } }
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())); }
public async Task PublishAsync(Message message, CancellationToken cancellationToken) => await PublishAsync(message, null, cancellationToken).ConfigureAwait(false);
public string GetMessageInContext(Message message) => _serializationRegister.Serialize(message, serializeForSnsPublishing: false);
public string Serialise(Message message) { var settings = GetJsonSettings(); return JsonConvert.SerializeObject(message, settings); }
public Task PublishAsync(Message message, CancellationToken cancellationToken) => PublishAsync(message, null, cancellationToken);
private bool ClientExceptionHandler(Exception ex, Message message) => _handleException?.Invoke(ex, message) ?? false;