by david
2. August 2010 09:20
Here is some example code for the simple MessageDecoder class that I wrote for my last blog, it takes an example ebMS 3.0 soap document from a file and loads it - giving access to the UserMessage and SignalMessage elements inside the envelope header. I have tried it on all the examples that I extracted from the eMS 3.0 core spec and it seems to work OK. It's just a start...
using System;
using System.IO;
using ebMS3.Soap;
namespace SoapLoaderTest
{
class Program
{
static void Main(string[] args)
{
string filename = "example.xml";
FileStream file = new FileStream(filename, FileMode.Open,
FileAccess.Read);
ebMS3.Soap.MessageDecoder decoder = new MessageDecoder(file);
SignalMessage[] smsgs = decoder.GetHeaderSignalMessages();
UserMessage[] umsgs = decoder.GetHeaderUserMessages();
file.Close();
}
}
}
by david
1. August 2010 17:25
The class below is my first attempt at being able to read the ebMS 3.0 headers in C#. It deserialises the xml into the objects I have created from the xsd files (see previous post). So you'll need the auto-generated C# classes to use this code. I'll post an example useage in another blog. So far I have tested it on all the examples extracted from the ebMS core spec and it seems to load the data OK.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Channels;
namespace ebMS3.Soap
{
public class MessageDecoder
{
private string _xmlnamespace = null;
public Message Message { get; private set; }
public MessageDecoder(Stream stream)
{
_xmlnamespace = GetXmlNamespace(typeof(Messaging));
Message = GetMessage(stream);
}
public MessageDecoder(byte[] rawbytes)
{
_xmlnamespace = GetXmlNamespace(typeof(Messaging));
MemoryStream ms = new MemoryStream(rawbytes);
Message = GetMessage(ms);
}
private string GetXmlNamespace(Type t)
{
XmlTypeAttribute[] attrs = (XmlTypeAttribute[])
t.GetCustomAttributes(typeof(XmlTypeAttribute), true);
if (attrs != null && attrs.Length > 0)
return attrs[0].Namespace;
else
return "";
}
public UserMessage[] GetHeaderUserMessages()
{
return GetHeaderMessages<UserMessage>();
}
public SignalMessage[] GetHeaderSignalMessages()
{
return GetHeaderMessages<SignalMessage>();
}
private T[] GetHeaderMessages<T>() where T : class
{
if (Message == null || Message.Headers == null) return default(T[]);
List<T> retval = new List<T>();
XmlSerializer xs = new XmlSerializer(typeof(T), _xmlnamespace);
XmlNode[] hdrs =
Message.Headers.GetHeader<XmlNode[]>("Messaging", _xmlnamespace);
foreach (XmlNode xn in hdrs)
{
if (xn.Name.EndsWith(typeof(T).Name))
{
T um = xs.Deserialize(new XmlNodeReader(xn)) as T;
if (um != null) retval.Add(um);
}
}
return retval.ToArray();
}
private Message GetMessage(Stream stream)
{
MessageVersion soap11 = MessageVersion.Soap11WSAddressingAugust2004;
XmlReader xreader = XmlReader.Create(stream);
return Message.CreateMessage(xreader, 5120, soap11); // 5Kb in headers
}
}
}
by david
31. July 2010 18:40
Instructions for creating auto-generated C# classes for ebMS 3.0 headers
(we should end up with this file: ebms-header-3_0-200704_soap11_soap12.cs).
1) Download these xsd files to a folder.
ebms-header-3_0-200704.xsd:
http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/core/ebms-header-3_0-200704.xsd
soap11.xsd:
http://schemas.xmlsoap.org/soap/envelope/
soap12.xsd:
http://www.w3.org/2003/05/soap-envelope/
2) Open a Visual Studio Command Prompt
Then change to the folder where the downloaded files are located
3) Run this command:
xsd /c /n:ebMS3.Soap ebms-header-3_0-200704.xsd soap11.xsd soap12.xsd
by david
31. July 2010 18:07