public void Send(OutgoingTransportMessage transportMessage, IEnumerable <string> addresses) { var transaction = SqlServerTransactionManager.TryGetTransactionForCurrentTask(); var commitAndDisposeTransaction = false; if (transaction == null) { transaction = SqlServerTransactionManager.BeginTransaction(ConnectionString); commitAndDisposeTransaction = true; } try { transportMessage.Headers[StandardHeaders.TimeSent] = DateTime.UtcNow.ToString("o"); if (!transportMessage.Headers.ContainsKey(StandardHeaders.ReplyToAddress)) { transportMessage.Headers[StandardHeaders.ReplyToAddress] = this.ServiceBrokerSendingService; } transportMessage.Headers[StandardHeaders.OriginatingAddress] = this.ServiceBrokerSendingService; transportMessage.Headers[StandardHeaders.ContentType] = messageEncoder.ContentType; transportMessage.Headers[StandardHeaders.EnclosedMessageTypes] = string.Join(",", messageMapper.GetEnclosedMessageTypes(transportMessage.Message.GetType()).Distinct()); using (var stream = new MemoryStream()) { var encodedMessage = messageEncoder.EncodeAsString(transportMessage.Message); FastXmlTransportMessageSerializer.Serialize(transportMessage.Headers, encodedMessage, stream); var messageBuffer = stream.ToArray(); foreach (var destination in addresses) { var conversationHandle = ServiceBrokerWrapper.SendOne( transaction: transaction , initiatorServiceName: ServiceBrokerSendingService , targetServiceName: destination , messageContractName: ServiceBrokerContract , messageType: ServiceBrokerMessageType , body: messageBuffer); #if DEBUG logger.Debug(string.Format("Sending message {0} with Handle {1} to Service Named {2}.", transportMessage.Message.GetType().AssemblyQualifiedName, conversationHandle, destination)); logger.Debug(string.Format("ToString() of the message yields: {0}\n" + "Message headers:\n{1}", transportMessage.Message.ToString(), string.Join(", ", transportMessage.Headers.Select(h => h.Key + ":" + h.Value).ToArray()))); #endif } } if (commitAndDisposeTransaction) { SqlServerTransactionManager.CommitTransactionAndDisposeConnection(transaction); } } catch (Exception) { if (commitAndDisposeTransaction) { SqlServerTransactionManager.TryForceDisposeTransactionAndConnection(transaction); } throw; } }