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; } }
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).ConfigureAwait(false); var cancelScheduledMessage = message as CancelScheduledMessage; if (cancelScheduledMessage != null) { try { long sequenceNumber; if (context.TryGetScheduledMessageId(out sequenceNumber)) { await _client.CancelScheduledSend(sequenceNumber).ConfigureAwait(false); } else { sequenceNumber = context.GetSequenceNumber(cancelScheduledMessage.TokenId); await _client.CancelScheduledSend(sequenceNumber).ConfigureAwait(false); } if (_log.IsDebugEnabled) { _log.DebugFormat("Canceled Scheduled: {0} {1}", sequenceNumber, _client.Path); } } catch (MessageNotFoundException exception) { if (_log.IsDebugEnabled) { _log.DebugFormat("The scheduled message was not found: {0}", exception.Detail.Message); } } } else { 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"); } 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; var sequenceNumber = await _client.ScheduleSend(brokeredMessage, enqueueTimeUtc).ConfigureAwait(false); context.SetScheduledMessageId(sequenceNumber); context.LogScheduled(enqueueTimeUtc); } else { await _observers.PreSend(context).ConfigureAwait(false); await _client.Send(brokeredMessage).ConfigureAwait(false); context.LogSent(); await _observers.PostSend(context).ConfigureAwait(false); } } } } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }
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).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"); } if (context.ScheduledEnqueueTimeUtc.HasValue) { brokeredMessage.ScheduledEnqueueTimeUtc = context.ScheduledEnqueueTimeUtc.Value; } if (context.PartitionKey != null) { brokeredMessage.PartitionKey = context.PartitionKey; } if (context.SessionId != null) { brokeredMessage.SessionId = context.SessionId; if (context.ReplyToSessionId == null) { brokeredMessage.ReplyToSessionId = context.SessionId; } } if (context.ReplyToSessionId != null) { brokeredMessage.ReplyToSessionId = context.ReplyToSessionId; } await _observers.PreSend(context).ConfigureAwait(false); await _sender.SendAsync(brokeredMessage).ConfigureAwait(false); _log.DebugFormat("SEND {0} ({1})", brokeredMessage.MessageId, _sender.Path); await _observers.PostSend(context).ConfigureAwait(false); } } } catch (Exception ex) { await _observers.SendFault(context, ex).ConfigureAwait(false); throw; } }