Reading Messages for Validation
How do I perform XML validation against an entire message? There is a method to read the body of the message but it's only possible to read headers one at a time.
Although it sounds more complicated than it really is, a straightforward way to read an entire message is to write it instead. With the Message interface, you have more flexibility when writing a message to an XmlWriter than when reading from a generated XmlReader. Exchanging the roles of readers and writers is a common trick used in XML processing.
Here's a really short example of using an XmlWriter with some seekable storage. I've replaced the validation step with dumping out the contents of the message.
Message message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "action!", "body");
MemoryStream stream = new MemoryStream();
using (message)
{
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false)) {
writer.WriteStartDocument();
message.WriteMessage(writer);
writer.WriteEndDocument();
}
}
stream.Seek(0L, SeekOrigin.Begin);
Console.WriteLine(new StreamReader(stream).ReadToEnd());
Obviously I'm leaving out more details here than just the validation step. For example, you'd probably want to harvest the original message for things like the message version and message properties so that you could reconstruct the message on the other side, rather than just throwing everything away.
Next time: Importing and Exporting WSDL Annotations
Comments
Anonymous
January 24, 2008
When writing your own service authorization manager, you override the CheckAccess or CheckAccessCoreAnonymous
January 24, 2008
Funny. I just glanced through my feeds and low and behold I read this one to take a much needed break on a problem I've been trying to figure out. Well this blog lead me down the right path. Just wanted to say keep up the good work and thanks!