views:

59

answers:

2

I have an order as an XDocument and I simply want to stick it in the body of a message and send it to an MSMQ queue. I've effectively serialized the order object already and now I just want to send it. Is this possible?

I'm using WCF here but I'd happy with a plain old msmq solution. I'm getting an error here indicating that an XDocument can't be serialized ... obviously can't do that, but how do I get my XDocument into the message body? Do I need to roll my own Serializer?

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);
}
+2  A: 

An XDocument is a convenient wrapper over XML data. There is no need to serialize the XDocument, just send the XML data as a string, using XDocument.ToString()

Panagiotis Kanavos
I tried that before I posted the question here, but it wraps the whole thing in a <string> tag. I need the the xml exactly as it is in the XDoc...
mattRo55
Not really fully answered but I'll accept this one because I don't like having an accept rate of <100%
mattRo55
A: 

I'm having the same problem developing on a Windows 7 box. It's putting my XML string inside another xml. Everything works just fine in server 2003.

I was finally able to fix this. There seem to be two ways to do this. Both involve setting the Formatter to XmlMessageFormatter. You can either set the Formatter on the MessageQueue or you can set it on the message before you send and after you peek/receive.

messageQueue.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(System.String) });
Adrian