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