void IMessageTransport.SendMessage(Message message, Address targetAddress)
{
if (message == null)
{
throw new ArgumentNullException("message");
}
if (targetAddress == null)
{
throw new ArgumentNullException("targetAddress");
}
var queuePath = MsmqUtilities.GetFullPath(targetAddress);
try
{
using (var messageQueue = new MessageQueue(queuePath, QueueAccessMode.SendAndReceive))
{
var toSend = MsmqUtilities.Convert(message);
if (message.ReplyToAddress != null)
{
toSend.ResponseQueue = new MessageQueue(MsmqUtilities.GetReturnAddress(message.ReplyToAddress.ToString(), targetAddress.ToString()));
}
messageQueue.Send(toSend, MessageQueueTransactionType.Automatic);
message.Id = toSend.Id;
}
}
catch (MessageQueueException ex)
{
if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound)
{
throw new EventSourcingException("消息队列未找到: [{0}]", targetAddress);
}
else
{
throw new EventSourcingException("发送消息到队列时遇到异常,队列地址:{0},异常详情:{1}", targetAddress, ex);
}
}
catch (Exception ex)
{
throw new EventSourcingException("发送消息到队列时遇到异常,队列地址:{0},异常详情:{1}", targetAddress, ex);
}
}