static BrokeredMessage CreateBrokeredMessage(AzureServiceBusSendContext <T> context) { var brokeredMessage = new BrokeredMessage(context.GetBodyStream()) { ContentType = context.ContentType.MediaType, ForcePersistence = context.Durable }; brokeredMessage.Properties.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"); } 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; } return(brokeredMessage); }
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)); }