I'm trying to send and receive XmlDocument objects using the
System.Messaging.dll functions. The code I've written follows, as best I
can tell, the methodology used in the sample code for the
XmlMessageFormatter class. But it isn't working -- specifically, receiving
isn't working. What am I doing wrong here?
I begin by creating a MessageQueue object and setting up its
XmlMessageFormatter to handle XmlDocument objects:
System.Type[] types = { System.Type.GetType("XmlDocument") };
XmlMessageFormatter fmt = new XmlMessageFormatter(types);
queue = new MessageQueue(queueName);
queue.Formatter = fmt;
I can then use the queue object to send XmlDocument messages with no
problem:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<msg/>");
doc.DocumentElement.SetAttribute("time", String.Format("{0}",
DateTime.Now));
queue.Send(doc);
But receiving these messages doesn't work:
Message msg = queue.Receive(TimeSpan.Zero);
XmlDocument doc = (XmlDocument) msg.Body;
throws this exception:
An unhandled exception of type 'System.ArgumentNullException' occurred
in mscorlib.dll
Additional information: Key cannot be null.
I can readily work around this problem. I just need to set up the
XmlMessageFormatter object to handle String objects instead of XmlDocument
objects. Then on sending, I send the doc.OuterXml property, and I receive
with this code:
Message msg = queue.Receive(TimeSpan.Zero);
XmlDocument doc = new XmlDocument();
doc.LoadXml((string) msg.Body);
But I'd sure like to know why the code I've written doesn't work.
Bob Rossney
rb*@well.com