我有一个作为XDocument的订单,我只是想将它粘贴在消息体中,并将其发送到MSMQ队列。我已经有效地序列化了order对象,现在我只想发送它。这个是可能的吗?
我在这里使用的是WCF,但我很乐意使用简单的老式msmq解决方案。我在这里得到一个错误,表明XDocument不能被序列化...显然不能这样做,但是如何将XDocument放入消息体中呢?我需要使用自己的序列化程序吗?
public void SendOrder(XDocument order)
{
var address = new EndpointAddress(@"msmq.formatname:DIRECT=OS:myServer\private$\myQueue");
var binding = new MsmqIntegrationBinding();
binding.Security.Mode = MsmqIntegrationSecurityMode.None;
binding.ExactlyOnce = false;
binding.Durable = false;
var channelFactory = new ChannelFactory<IOrderSubmitter>(binding, address);
var channel = channelFactory.CreateChannel();
var message = new MsmqMessage<XDocument>(order);
message.Label = "Really Big Order with lots of profit";
message.BodyType = (int)System.Runtime.InteropServices.VarEnum.VT_ARRAY;
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
channel.SubmitOrder(message);
scope.Complete();
}
}
[ServiceContractAttribute(Namespace = "http://my.namespace.com", Name = "Hello")]
public interface IOrderSubmitter
{
[OperationContract(IsOneWay = true)]
void SubmitOrder(MsmqMessage<XDocument> message);
}
发布于 2010-07-22 17:27:53
我在Windows7机器上开发时也遇到了同样的问题。它将我的xml字符串放入另一个XML中。在server2003中,一切都运行良好。
我终于能够解决这个问题了。似乎有两种方法可以做到这一点。两者都涉及到将格式化程序设置为XmlMessageFormatter。您可以在MessageQueue上设置格式化程序,也可以在发送之前和查看/接收之后在邮件上设置它。
messageQueue.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(System.String) });
发布于 2010-07-16 13:27:29
XDocument是一种方便的XML数据包装器。无需序列化XDocument,只需使用XDocument.ToString()将其作为字符串发送即可
https://stackoverflow.com/questions/3265212
复制相似问题