我有一个 XDocument 订单,我只想将其粘贴到消息正文中并将其发送到 MSMQ 队列。我已经有效地序列化了订单对象,现在我只想发送它。这可能吗?
我在这里使用 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);
}
最佳答案
XDocument 是 XML 数据的便捷包装器。无需序列化 XDocument,只需将 XML 数据作为字符串发送即可,使用 XDocument.ToString()
关于c# - 如何通过 MSMQ(使用 WCF)发送 XDocument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3265212/