WebContentFormat.Raw in your WCF config file

If you want to specify WebContentFormat.Raw in your WCF service - so that it can process any content-type, but want that to happen from the config file and not programatically, here is one approach.

 

1) Add an overridden WebContentTypeMapper in your code, like this:

 

using System;

using System.ServiceModel.Channels;

 

namespace ebMS3.ServiceModel

{

    public class RawContentTypeMapper : WebContentTypeMapper

    {

        public override WebContentFormat

                GetMessageFormatForContentType(string contentType)

        {

            return WebContentFormat.Raw;

        }

    }

}

 

2) Reference this new WebContentTypeMapper in your config file in a customBinding:

 

<bindings>

  <customBinding>

    <binding name="RawReceiveCapable">

      <!-- Provide the fully qualified name of the WebContentTypeMapper  -->

      <webMessageEncoding webContentTypeMapperType=

          "ebMS3.ServiceModel.RawContentTypeMapper, ebMS3.ServiceModel,

                  Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

      <httpTransport manualAddressing="true" maxReceivedMessageSize="524288000"

            transferMode="Streamed" />

      <!-- maxReceivedMessageSize is 500 Mb -->

    </binding>

  </customBinding>

</bindings>

 

3) Finally, use the new customBinding on your endpoint, also in your config file:

 

<endpoint contract="MyContract" behaviorConfiguration="MyEndpointBehavior"

binding="customBinding" bindingConfiguration="RawReceiveCapable" />

That's it, another job done...

How to use the ebMS 3.0 MessageDecoder

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();
        }
    }
}

Deserialising ebMS 3.0 Headers

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 { getprivate 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 == nullreturn 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
        }
    }
}