Posted by: beatkiener | May 22, 2009

BasicHttpBinaryBinding for Silverlight

Silverlight 3 beta introduces a binary message encoder on the channel stack.

Binary encoding is implemented as a custom binding, there is no out-of-the-box binary binding.

<bindings>
  <customBinding>
	<binding name="binaryHttpBinding">
	   <binaryMessageEncoding />
         <httpTransport />
      </binding>
  </customBinding>
</bindings>
<endpoint address=""
          binding="customBinding"
          bindingConfiguration="binaryHttpBinding"
          contract="Service" />

If you would set the binding programmatically you have to configure your own CustomBinding instance.

This was the reason to implement the BasicHttpBinaryBinding class as you can find below.

/// <summary>
// Represents a basic http binding with binary encoding.
/// </summary>
public class BasicHttpBinaryBinding : BasicHttpBinding
{
    /// <summary>
    ///  Initializes a new instance of the BasicHttpBinaryBinding class.
    /// </summary>
    public BasicHttpBinaryBinding()
        : this(BasicHttpSecurityMode.None)
    {
    }

    /// <summary>
    /// Initializes a new instance of the BasicHttpBinaryBinding class.
    /// </summary>
    /// <param name="securityMode">
    /// The value of System.ServiceModel.BasicHttpSecurityMode that specifies 
    /// the type of security that is used with the SOAP message and for the client.
    /// </param>
    public BasicHttpBinaryBinding(BasicHttpSecurityMode securityMode)
        : this(securityMode, true)
    {
    }

    /// <summary>
    /// Initializes a new instance of the BasicHttpBinaryBinding class.
    /// </summary>
    /// <param name="securityMode">
    /// The value of System.ServiceModel.BasicHttpSecurityMode that specifies 
    /// the type of security that is used with the SOAP message and for the client.
    /// </param>
    /// <param name="binaryEncoding">
    /// Indicates whether the binary encoding is enabled or not
    /// </param>
    public BasicHttpBinaryBinding(BasicHttpSecurityMode securityMode, bool binaryEncoding)
        : base(securityMode)
    {
        this.BinaryEncoding = true;
        this.BinaryEncoding = binaryEncoding;
    }

    /// <summary>
    /// Gets or sets a value that indicates whether the binary encoding is enabled or not. Default is true.
    /// </summary>
    public bool BinaryEncoding { get; set; }

    /// <summary>
    /// Returns an ordered collection of binding elements contained in the current binding.
    /// </summary>
    public override BindingElementCollection CreateBindingElements()
    {
        BindingElementCollection elements = base.CreateBindingElements();

        if (this.BinaryEncoding)
        {
            // search the existing message encoding element (Text or MTOM) and replace it
            // note: the search must be done with the base type of text and mtom binding element, because this code is compiled against silverlight also 
            // and there is no mtom encoding available
            for (int i = elements.Count - 1; i >= 0; i--)
            {
                BindingElement element = elements[i];
                if (element.GetType().IsSubclassOf(typeof(MessageEncodingBindingElement)))
                {
                    elements.RemoveAt(i);
                    elements.Insert(i, new BinaryMessageEncodingBindingElement());
                    break;
                }
            }
        }

        return elements;
    }

}

The class is Silverlight compilable, therefore you can share it along the Silverlight and wcf-service project.

How to use: instead of the BasicHttpBinding you can use BasicHttpBinaryBinding as following to enable binary encoding.

BasicHttpBinding binding = new BasicHttpBinding();

BasicHttpBinding binding = new BasicHttpBinaryBinding();

Download the code here


Responses

  1. Kieners — Please help. (1) You mention MTOM but I have been reading elsewhere that SL3 does not support MTOM at all. Which is correct? Is your solution a workaround that allows SL3 to use MTOM? Is it is fact true that MTOM is supported intrinsically in SL3? Something else? (2) I have to move large chunks of data, large DOC or RTF or similar files, and I am wondering– will the BasicBinaryBinding solution you note help to move that data faster? Do you have any recommendations for my case? Pelase advise. Thank you. — Mark Kmaoski

    • Hi mkamoski,

      Sorry for my late anwser.

      Well, SL3 doesn’t support MTOM. The difference is that binary encoding is proprietary and MTOM is standardized and interoperable.
      With binary encoding you get some serious performance gains over text encoding. In our projects, when binary encoding is enabled the reduction of the message size is about 30-40 percent. But keep in mind that binary encoding is a proprietary format and a WCF only feature.

      Some explanations about the code:
      My class BasicHttpBinaryBinding is compiled against Silverlight-CLR and the normal-CLR, because you will use it in both environments to declare your endpoints for binary message encoding. On server side (in the normal-CLR) the BasicHttpBinaryBinding must ensure that no other encoding such as Text or MTOM is in use or you get an exception.

      Hope this helps.

      Best regards,
      Beat Kiener


Leave a response

Your response:

Categories