async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancelSend) { var context = new InMemorySendContext <T>(message, cancelSend); try { await pipe.Send(context).ConfigureAwait(false); var messageId = context.MessageId ?? NewId.NextGuid(); await _sendObservable.PreSend(context).ConfigureAwait(false); var transportMessage = new InMemoryTransportMessage(messageId, context.Body, context.ContentType.MediaType, TypeMetadataCache <T> .ShortName); await _exchange.Send(transportMessage).ConfigureAwait(false); context.LogSent(); await _sendObservable.PostSend(context).ConfigureAwait(false); } catch (Exception ex) { context.LogFaulted(ex); await _sendObservable.SendFault(context, ex).ConfigureAwait(false); throw; } }
async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { if (IsStopped) { throw new TransportUnavailableException($"The send transport is stopped: {_entityName}"); } IPipe <ClientContext> modelPipe = Pipe.New <ClientContext>(p => { p.UseFilter(_filter); p.UseExecuteAsync(async clientContext => { var sendContext = new TransportAmazonSqsSendContext <T>(message, cancellationToken); try { await pipe.Send(sendContext).ConfigureAwait(false); var transportMessage = clientContext.CreateSendRequest(_entityName, sendContext.Body); transportMessage.MessageAttributes.Set(sendContext.Headers); if (!string.IsNullOrEmpty(sendContext.DeduplicationId)) { transportMessage.MessageDeduplicationId = sendContext.DeduplicationId; } if (!string.IsNullOrEmpty(sendContext.GroupId)) { transportMessage.MessageGroupId = sendContext.GroupId; } transportMessage.MessageAttributes.Set("Content-Type", sendContext.ContentType.MediaType); transportMessage.MessageAttributes.Set(nameof(sendContext.MessageId), sendContext.MessageId); transportMessage.MessageAttributes.Set(nameof(sendContext.CorrelationId), sendContext.CorrelationId); transportMessage.MessageAttributes.Set(nameof(sendContext.TimeToLive), sendContext.TimeToLive); await _observers.PreSend(sendContext).ConfigureAwait(false); await clientContext.SendMessage(transportMessage, sendContext.CancellationToken).ConfigureAwait(false); sendContext.LogSent(); await _observers.PostSend(sendContext).ConfigureAwait(false); } catch (Exception ex) { sendContext.LogFaulted(ex); await _observers.SendFault(sendContext, ex).ConfigureAwait(false); throw; } }); }); await _clientSource.Send(modelPipe, cancellationToken).ConfigureAwait(false); }
async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancelSend) { var context = new ServiceBusSendContextImpl <T>(message, cancelSend); try { await pipe.Send(context); using (Stream messageBodyStream = context.GetBodyStream()) { using (var brokeredMessage = new BrokeredMessage(messageBodyStream)) { brokeredMessage.ContentType = context.ContentType.MediaType; brokeredMessage.ForcePersistence = context.Durable; if (context.TimeToLive.HasValue) { brokeredMessage.TimeToLive = context.TimeToLive.Value; } if (context.MessageId.HasValue) { brokeredMessage.MessageId = context.MessageId.Value.ToString("N"); } if (context.CorrelationId.HasValue) { brokeredMessage.CorrelationId = context.CorrelationId.Value.ToString("N"); } if (context.ScheduledEnqueueTimeUtc.HasValue) { brokeredMessage.ScheduledEnqueueTimeUtc = context.ScheduledEnqueueTimeUtc.Value; } if (context.PartitionKey != null) { brokeredMessage.PartitionKey = context.PartitionKey; } await _observers.PreSend(context); await _sender.SendAsync(brokeredMessage); _log.DebugFormat("SEND {0} ({1})", brokeredMessage.MessageId, _sender.Path); await _observers.PostSend(context); } } } catch (Exception ex) { _observers.SendFault(context, ex).Wait(cancelSend); throw; } }
public async Task Send(ModelContext modelContext) { await _filter.Send(modelContext, Pipe.Empty<ModelContext>()).ConfigureAwait(false); var properties = modelContext.Model.CreateBasicProperties(); var context = new BasicPublishRabbitMqSendContext<T>(properties, _exchange, _message, _cancellationToken); try { await _pipe.Send(context).ConfigureAwait(false); byte[] body = context.Body; if (context.TryGetPayload(out PublishContext publishContext)) context.Mandatory = context.Mandatory || publishContext.Mandatory; if (properties.Headers == null) properties.Headers = new Dictionary<string, object>(); properties.ContentType = context.ContentType.MediaType; properties.Headers["Content-Type"] = context.ContentType.MediaType; SetHeaders(properties.Headers, context.Headers); properties.Persistent = context.Durable; if (context.MessageId.HasValue) properties.MessageId = context.MessageId.ToString(); if (context.CorrelationId.HasValue) properties.CorrelationId = context.CorrelationId.ToString(); if (context.TimeToLive.HasValue) properties.Expiration = context.TimeToLive.Value.TotalMilliseconds.ToString("F0", CultureInfo.InvariantCulture); await _observers.PreSend(context).ConfigureAwait(false); var publishTask = modelContext.BasicPublishAsync(context.Exchange, context.RoutingKey ?? "", context.Mandatory, context.BasicProperties, body, context.AwaitAck); await publishTask.WithCancellation(context.CancellationToken).ConfigureAwait(false); context.LogSent(); await _observers.PostSend(context).ConfigureAwait(false); } catch (Exception ex) { context.LogFaulted(ex); await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }
Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { IPipe <EventDataSendEndpointContext> clientPipe = Pipe.ExecuteAsync <EventDataSendEndpointContext>(async clientContext => { var context = new EventDataSendContext <T>(message, cancellationToken); try { await pipe.Send(context).ConfigureAwait(false); await _observers.PreSend(context).ConfigureAwait(false); using (var messageBodyStream = context.GetBodyStream()) { var eventData = new EventData(messageBodyStream); KeyValuePair <string, object>[] headers = context.Headers.GetAll() .Where(x => x.Value != null && (x.Value is string || x.Value.GetType().IsValueType)) .ToArray(); foreach (KeyValuePair <string, object> header in headers) { if (eventData.Properties.ContainsKey(header.Key)) { continue; } eventData.Properties.Add(header.Key, header.Value); } if (context.PartitionKey != null) { eventData.PartitionKey = context.PartitionKey; } await clientContext.Send(eventData).ConfigureAwait(false); context.LogSent(); await _observers.PostSend(context).ConfigureAwait(false); } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }); return(_source.Send(clientPipe, cancellationToken)); }
Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { IPipe <EventDataSendEndpointContext> clientPipe = Pipe.ExecuteAsync <EventDataSendEndpointContext>(async clientContext => { var context = new EventDataSendContext <T>(message, cancellationToken); try { await pipe.Send(context).ConfigureAwait(false); await _observers.PreSend(context).ConfigureAwait(false); using (var eventData = new EventData(context.Body)) { eventData.Properties.SetTextHeaders(context.Headers, (_, text) => text); // if (context.PartitionKey != null) // eventData.PartitionKey = context.PartitionKey; await clientContext.Send(eventData).ConfigureAwait(false); context.LogSent(); await _observers.PostSend(context).ConfigureAwait(false); } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }); return(_source.Send(clientPipe, cancellationToken)); }
Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { IPipe <SendEndpointContext> clientPipe = Pipe.ExecuteAsync <SendEndpointContext>(async clientContext => { var context = new AzureServiceBusSendContext <T>(message, cancellationToken); try { await pipe.Send(context).ConfigureAwait(false); if (message is CancelScheduledMessage cancelScheduledMessage && (context.TryGetScheduledMessageId(out var sequenceNumber) || context.TryGetSequencyNumber(cancelScheduledMessage.TokenId, out sequenceNumber))) { try { await clientContext.CancelScheduledSend(sequenceNumber).ConfigureAwait(false); if (_log.IsDebugEnabled) { _log.DebugFormat("Canceled Scheduled: {0} {1}", sequenceNumber, clientContext.EntityPath); } } catch (MessageNotFoundException exception) { if (_log.IsDebugEnabled) { _log.DebugFormat("The scheduled message was not found: {0}", exception.Message); } } return; } await _observers.PreSend(context).ConfigureAwait(false); var brokeredMessage = new Message(context.Body) { ContentType = context.ContentType.MediaType }; brokeredMessage.UserProperties.SetTextHeaders(context.Headers, (_, text) => text); if (context.TimeToLive.HasValue) { brokeredMessage.TimeToLive = context.TimeToLive.Value; } if (context.MessageId.HasValue) { brokeredMessage.MessageId = context.MessageId.Value.ToString("N"); } if (context.CorrelationId.HasValue) { brokeredMessage.CorrelationId = context.CorrelationId.Value.ToString("N"); } CopyIncomingIdentifiersIfPresent(context); if (context.PartitionKey != null) { brokeredMessage.PartitionKey = context.PartitionKey; } var sessionId = string.IsNullOrWhiteSpace(context.SessionId) ? context.ConversationId?.ToString("N") : context.SessionId; if (!string.IsNullOrWhiteSpace(sessionId)) { brokeredMessage.SessionId = sessionId; if (context.ReplyToSessionId == null) { brokeredMessage.ReplyToSessionId = sessionId; } } if (context.ReplyToSessionId != null) { brokeredMessage.ReplyToSessionId = context.ReplyToSessionId; } if (context.ScheduledEnqueueTimeUtc.HasValue) { var enqueueTimeUtc = context.ScheduledEnqueueTimeUtc.Value; sequenceNumber = await clientContext.ScheduleSend(brokeredMessage, enqueueTimeUtc).ConfigureAwait(false); context.SetScheduledMessageId(sequenceNumber); context.LogScheduled(enqueueTimeUtc); } else { await clientContext.Send(brokeredMessage).ConfigureAwait(false); context.LogSent(); await _observers.PostSend(context).ConfigureAwait(false); } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }); return(_source.Send(clientPipe, cancellationToken)); }
Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { IPipe <SendEndpointContext> clientPipe = Pipe.ExecuteAsync <SendEndpointContext>(async clientContext => { var context = new AzureServiceBusSendContext <T>(message, cancellationToken); try { await pipe.Send(context).ConfigureAwait(false); if (message is CancelScheduledMessage cancelScheduledMessage && (context.TryGetScheduledMessageId(out var sequenceNumber) || context.TryGetSequencyNumber(cancelScheduledMessage.TokenId, out sequenceNumber))) { try { await clientContext.CancelScheduledSend(sequenceNumber).ConfigureAwait(false); if (_log.IsDebugEnabled) { _log.DebugFormat("Canceled Scheduled: {0} {1}", sequenceNumber, clientContext.EntityPath); } } catch (MessageNotFoundException exception) { if (_log.IsDebugEnabled) { _log.DebugFormat("The scheduled message was not found: {0}", exception.Message); } } return; } await _observers.PreSend(context).ConfigureAwait(false); var brokeredMessage = CreateBrokeredMessage(context); if (context.ScheduledEnqueueTimeUtc.HasValue && context.ScheduledEnqueueTimeUtc.Value < DateTime.UtcNow) { var enqueueTimeUtc = context.ScheduledEnqueueTimeUtc.Value; try { sequenceNumber = await clientContext.ScheduleSend(brokeredMessage, enqueueTimeUtc).ConfigureAwait(false); } catch (ArgumentOutOfRangeException exception) { brokeredMessage = CreateBrokeredMessage(context); await clientContext.Send(brokeredMessage).ConfigureAwait(false); sequenceNumber = 0; } context.SetScheduledMessageId(sequenceNumber); context.LogScheduled(enqueueTimeUtc); } else { await clientContext.Send(brokeredMessage).ConfigureAwait(false); context.LogSent(); await _observers.PostSend(context).ConfigureAwait(false); } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }); return(_source.Send(clientPipe, cancellationToken)); }
public async Task Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) where T : class { IPipe <ClientContext> clientPipe = Pipe.New <ClientContext>(p => { p.UseExecuteAsync(async clientContext => { var context = new HttpSendContextImpl <T>(message, cancellationToken); try { await pipe.Send(context).ConfigureAwait(false); using (var request = new HttpRequestMessage(_sendSettings.Method, context.DestinationAddress)) using (var payload = new ByteArrayContent(context.Body)) { request.Headers.UserAgent.Add(new ProductInfoHeaderValue("MassTransit", HostMetadataCache.Host.MassTransitVersion)); if (context.ResponseAddress != null) { request.Headers.Referrer = context.ResponseAddress; } payload.Headers.ContentType = new MediaTypeHeaderValue(context.ContentType.MediaType); foreach (KeyValuePair <string, object> header in context.Headers.GetAll().Where(h => h.Value != null && (h.Value is string || h.Value.GetType().IsValueType))) { request.Headers.Add(header.Key, header.Value.ToString()); } if (context.MessageId.HasValue) { request.Headers.Add(Clients.HttpHeaders.MessageId, context.MessageId.Value.ToString()); } if (context.CorrelationId.HasValue) { request.Headers.Add(Clients.HttpHeaders.CorrelationId, context.CorrelationId.Value.ToString()); } if (context.InitiatorId.HasValue) { request.Headers.Add(Clients.HttpHeaders.InitiatorId, context.InitiatorId.Value.ToString()); } if (context.ConversationId.HasValue) { request.Headers.Add(Clients.HttpHeaders.ConversationId, context.ConversationId.Value.ToString()); } if (context.RequestId.HasValue) { request.Headers.Add(Clients.HttpHeaders.RequestId, context.RequestId.Value.ToString()); } //TODO: TTL? request.Content = payload; await _observers.PreSend(context).ConfigureAwait(false); using (var response = await clientContext.SendAsync(request, cancellationToken).ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); if (responseStream.Length > 0) { var receiveContext = new HttpClientReceiveContext(response, responseStream, false, _topology); await clientContext.ReceiveResponse(receiveContext).ConfigureAwait(false); } } await _observers.PostSend(context).ConfigureAwait(false); } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); if (_log.IsErrorEnabled) { _log.Error("Send Fault: " + context.DestinationAddress, ex); } throw; } }); }); await _clientContextSupervisor.Send(clientPipe, cancellationToken).ConfigureAwait(false); }
async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { if (IsStopped) { throw new TransportUnavailableException($"The send transport is stopped: {_entityName}/{_destinationType}"); } IPipe <SessionContext> sessionPipe = Pipe.New <SessionContext>(p => { p.UseFilter(_filter); p.UseExecuteAsync(async sessionContext => { var destination = await sessionContext.GetDestination(_entityName, _destinationType).ConfigureAwait(false); var producer = await sessionContext.CreateMessageProducer(destination).ConfigureAwait(false); var sendContext = new TransportActiveMqSendContext <T>(message, cancellationToken); try { await pipe.Send(sendContext).ConfigureAwait(false); byte[] body = sendContext.Body; var transportMessage = sessionContext.Session.CreateBytesMessage(); KeyValuePair <string, object>[] headers = sendContext.Headers.GetAll() .Where(x => x.Value != null && (x.Value is string || x.Value.GetType().GetTypeInfo().IsValueType)) .ToArray(); foreach (KeyValuePair <string, object> header in headers) { if (transportMessage.Properties.Contains(header.Key)) { continue; } transportMessage.Properties[header.Key] = header.Value; } transportMessage.Properties["Content-Type"] = sendContext.ContentType.MediaType; transportMessage.NMSDeliveryMode = sendContext.Durable ? MsgDeliveryMode.Persistent : MsgDeliveryMode.NonPersistent; if (sendContext.MessageId.HasValue) { transportMessage.NMSMessageId = sendContext.MessageId.ToString(); } if (sendContext.CorrelationId.HasValue) { transportMessage.NMSCorrelationID = sendContext.CorrelationId.ToString(); } if (sendContext.TimeToLive.HasValue) { transportMessage.NMSTimeToLive = sendContext.TimeToLive.Value; } if (sendContext.Priority.HasValue) { transportMessage.NMSPriority = sendContext.Priority.Value; } transportMessage.Content = body; await _observers.PreSend(sendContext).ConfigureAwait(false); var publishTask = Task.Run(() => producer.Send(transportMessage), sendContext.CancellationToken); await publishTask.UntilCompletedOrCanceled(sendContext.CancellationToken).ConfigureAwait(false); sendContext.LogSent(); await _observers.PostSend(sendContext).ConfigureAwait(false); } catch (Exception ex) { sendContext.LogFaulted(ex); await _observers.SendFault(sendContext, ex).ConfigureAwait(false); throw; } }); }); await _sessionAgent.Send(sessionPipe, cancellationToken).ConfigureAwait(false); }
public async Task Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancelSend) where T : class { IPipe <ClientContext> clientPipe = Pipe.New <ClientContext>(p => { //TODO: p.UseFilter(_filter); p.UseExecuteAsync(async clientContext => { //sendSettings var method = HttpMethod.Post; var timeOut = TimeSpan.FromSeconds(5); var context = new HttpSendContextImpl <T>(message, cancelSend); try { await pipe.Send(context).ConfigureAwait(false); using (var msg = new HttpRequestMessage(_sendSettings.Method, context.DestinationAddress)) using (var payload = new ByteArrayContent(context.Body)) { //TODO: Get access to a HostInfo instance msg.Headers.UserAgent.Add(new ProductInfoHeaderValue("MassTransit", Version)); if (context.ResponseAddress != null) { msg.Headers.Referrer = context.ResponseAddress; } payload.Headers.ContentType = new MediaTypeHeaderValue(context.ContentType.MediaType); foreach ( KeyValuePair <string, object> header in context.Headers.GetAll().Where(h => h.Value != null && (h.Value is string || h.Value.GetType().IsValueType))) { msg.Headers.Add(header.Key, header.Value.ToString()); } if (context.MessageId.HasValue) { msg.Headers.Add(HttpHeaders.MessageId, context.MessageId.Value.ToString()); } if (context.CorrelationId.HasValue) { msg.Headers.Add(HttpHeaders.CorrelationId, context.CorrelationId.Value.ToString()); } if (context.InitiatorId.HasValue) { msg.Headers.Add(HttpHeaders.InitiatorId, context.InitiatorId.Value.ToString()); } if (context.ConversationId.HasValue) { msg.Headers.Add(HttpHeaders.ConversationId, context.ConversationId.Value.ToString()); } if (context.RequestId.HasValue) { msg.Headers.Add(HttpHeaders.RequestId, context.RequestId.Value.ToString()); } //TODO: TTL? msg.Content = payload; await _observers.PreSend(context).ConfigureAwait(false); using (var response = await clientContext.SendAsync(msg, cancelSend).ConfigureAwait(false)) { var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); if (responseStream.Length > 0) { ISendEndpointProvider sendEndpointProvider = new HttpClientSendEndpointProvider(); IPublishEndpointProvider publishEndpointProvider = new HttpClientPublishEndpointProvider(); var receiveContext = new HttpClientReceiveContext(response, responseStream, false, _receiveObserver, sendEndpointProvider, publishEndpointProvider); await clientContext.ReceiveResponse(receiveContext).ConfigureAwait(false); } } await _observers.PostSend(context).ConfigureAwait(false); } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); if (_log.IsErrorEnabled) { _log.Error("Send Fault: " + context.DestinationAddress, ex); } throw; } }); }); await _clientCache.DoWith(clientPipe, cancelSend).ConfigureAwait(false); }
Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { IPipe <SendEndpointContext> clientPipe = Pipe.ExecuteAsync <SendEndpointContext>(async clientContext => { var context = new AzureServiceBusSendContext <T>(message, cancellationToken); try { await pipe.Send(context).ConfigureAwait(false); if (message is CancelScheduledMessage cancelScheduledMessage && (context.TryGetScheduledMessageId(out var sequenceNumber) || context.TryGetSequencyNumber(cancelScheduledMessage.TokenId, out sequenceNumber))) { try { await clientContext.CancelScheduledSend(sequenceNumber).ConfigureAwait(false); if (_log.IsDebugEnabled) { _log.DebugFormat("Canceled Scheduled: {0} {1}", sequenceNumber, clientContext.EntityPath); } } catch (MessageNotFoundException exception) { if (_log.IsDebugEnabled) { _log.DebugFormat("The scheduled message was not found: {0}", exception.Detail.Message); } } return; } await _observers.PreSend(context).ConfigureAwait(false); using (var messageBodyStream = context.GetBodyStream()) using (var brokeredMessage = new BrokeredMessage(messageBodyStream)) { brokeredMessage.ContentType = context.ContentType.MediaType; brokeredMessage.ForcePersistence = context.Durable; KeyValuePair <string, object>[] headers = context.Headers.GetAll() .Where(x => x.Value != null && (x.Value is string || x.Value.GetType().IsValueType)) .ToArray(); foreach (KeyValuePair <string, object> header in headers) { if (brokeredMessage.Properties.ContainsKey(header.Key)) { continue; } brokeredMessage.Properties.Add(header.Key, header.Value); } if (context.TimeToLive.HasValue) { brokeredMessage.TimeToLive = context.TimeToLive.Value; } if (context.MessageId.HasValue) { brokeredMessage.MessageId = context.MessageId.Value.ToString("N"); } if (context.CorrelationId.HasValue) { brokeredMessage.CorrelationId = context.CorrelationId.Value.ToString("N"); } CopyIncomingIdentifiersIfPresent(context); if (context.PartitionKey != null) { brokeredMessage.PartitionKey = context.PartitionKey; } var sessionId = string.IsNullOrWhiteSpace(context.SessionId) ? context.ConversationId?.ToString("N") : context.SessionId; if (!string.IsNullOrWhiteSpace(sessionId)) { brokeredMessage.SessionId = sessionId; if (context.ReplyToSessionId == null) { brokeredMessage.ReplyToSessionId = sessionId; } } if (context.ReplyToSessionId != null) { brokeredMessage.ReplyToSessionId = context.ReplyToSessionId; } if (context.ScheduledEnqueueTimeUtc.HasValue) { var enqueueTimeUtc = context.ScheduledEnqueueTimeUtc.Value; sequenceNumber = await clientContext.ScheduleSend(brokeredMessage, enqueueTimeUtc).ConfigureAwait(false); context.SetScheduledMessageId(sequenceNumber); context.LogScheduled(enqueueTimeUtc); } else { await clientContext.Send(brokeredMessage).ConfigureAwait(false); context.LogSent(); await _observers.PostSend(context).ConfigureAwait(false); } } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }); return(_source.Send(clientPipe, cancellationToken)); }
public Task PreSend(SendContext <TMessage> context) { return(_observer.PreSend(context)); }
public void PreSend(Message message) { _innerObservable.PreSend(Address, RoutingType, message); }
async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { if (IsStopped) { throw new TransportUnavailableException($"The RabbitMQ send transport is stopped: {_exchange}"); } IPipe <ModelContext> modelPipe = Pipe.New <ModelContext>(p => { p.UseFilter(_filter); p.UseExecuteAsync(async modelContext => { var properties = modelContext.Model.CreateBasicProperties(); var context = new BasicPublishRabbitMqSendContext <T>(properties, _exchange, message, cancellationToken); try { await pipe.Send(context).ConfigureAwait(false); byte[] body = context.Body; if (context.TryGetPayload(out PublishContext publishContext)) { context.Mandatory = context.Mandatory || publishContext.Mandatory; } if (properties.Headers == null) { properties.Headers = new Dictionary <string, object>(); } properties.ContentType = context.ContentType.MediaType; properties.Headers["Content-Type"] = context.ContentType.MediaType; properties.Headers.SetTextHeaders(context.Headers, (_, text) => text); properties.Persistent = context.Durable; if (context.MessageId.HasValue) { properties.MessageId = context.MessageId.ToString(); } if (context.CorrelationId.HasValue) { properties.CorrelationId = context.CorrelationId.ToString(); } if (context.TimeToLive.HasValue) { properties.Expiration = context.TimeToLive.Value.TotalMilliseconds.ToString("F0", CultureInfo.InvariantCulture); } await _observers.PreSend(context).ConfigureAwait(false); var publishTask = modelContext.BasicPublishAsync(context.Exchange, context.RoutingKey ?? "", context.Mandatory, context.BasicProperties, body, context.AwaitAck); await publishTask.WithCancellation(context.CancellationToken).ConfigureAwait(false); context.LogSent(); await _observers.PostSend(context).ConfigureAwait(false); } catch (Exception ex) { context.LogFaulted(ex); await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }); }); await _modelContextSupervisor.Send(modelPipe, cancellationToken).ConfigureAwait(false); }
async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) { if (IsStopped) { throw new TransportUnavailableException($"The send transport is stopped: {_entityName}"); } IPipe <ModelContext> modelPipe = Pipe.New <ModelContext>(p => { p.UseFilter(_filter); p.UseExecuteAsync(async modelContext => { var topicArn = await modelContext.GetTopic(_entityName).ConfigureAwait(false); var sendContext = new TransportAmazonSqsSendContext <T>(message, cancellationToken); try { await pipe.Send(sendContext).ConfigureAwait(false); var transportMessage = modelContext.CreateTransportMessage(topicArn, sendContext.Body); KeyValuePair <string, object>[] headers = sendContext.Headers.GetAll() .Where(x => x.Value != null && (x.Value is string || x.Value.GetType().GetTypeInfo().IsValueType)) .ToArray(); foreach (KeyValuePair <string, object> header in headers) { if (transportMessage.MessageAttributes.ContainsKey(header.Key)) { continue; } transportMessage.MessageAttributes[header.Key].StringValue = header.Value.ToString(); } transportMessage.MessageAttributes.Add("Content-Type", new MessageAttributeValue { DataType = "String", StringValue = sendContext.ContentType.MediaType }); if (sendContext.MessageId.HasValue) { transportMessage.MessageAttributes.Add("MessageId", new MessageAttributeValue { DataType = "String", StringValue = sendContext.MessageId.ToString() }); } if (sendContext.CorrelationId.HasValue) { transportMessage.MessageAttributes.Add("CorrelationId", new MessageAttributeValue { DataType = "String", StringValue = sendContext.CorrelationId.ToString() }); } if (sendContext.TimeToLive.HasValue) { transportMessage.MessageAttributes.Add("TimeToLive", new MessageAttributeValue { DataType = "Number", StringValue = sendContext.TimeToLive.Value.Milliseconds.ToString() }); } await _observers.PreSend(sendContext).ConfigureAwait(false); var publishTask = Task.Run(() => modelContext.Publish(transportMessage, sendContext.CancellationToken), sendContext.CancellationToken); await publishTask.UntilCompletedOrCanceled(sendContext.CancellationToken).ConfigureAwait(false); sendContext.LogSent(); await _observers.PostSend(sendContext).ConfigureAwait(false); } catch (Exception ex) { sendContext.LogFaulted(ex); await _observers.SendFault(sendContext, ex).ConfigureAwait(false); throw; } }); }); await _modelAgent.Send(modelPipe, cancellationToken).ConfigureAwait(false); }