Up Configuration and deployment code samples
DevForce Resource Center » Samples » Configuration and deployment code samples » Code sample: Add GZip support (Silverlight)

Code sample: Add GZip support (Silverlight)

Last modified on September 18, 2012 15:26

If you're using a ServiceReferences.ClientConfig to customize communications in your Silverlight client, messages to and from the EntityServer won't be compressed as in the default configuration.  Here we show how to work around that limitation and add GZip support for compressed messages.


Unknown macro: IBNote


Problem

When using a ServiceReferences.ClientConfig in your Silverlight application to customize communications (for example, to override default timeout values), you cannot add the GZip binding extension within the <system.serviceModel> section.

This is because the .ClientConfig in Silverlight does not allow the same configuration settings as a standard .config file.

Solution

To work around this limitation, you can implement a simple class which extends the ServiceProxyEvents  to programmatically add GZip support.

C#
using System.ServiceModel.Channels;
using IdeaBlade.Core.Wcf.Extensions;

public class ProxyEvents  : IdeaBlade.EntityModel.ServiceProxyEvents {

 public override void OnEndpointCreated(System.ServiceModel.Description.ServiceEndpoint endpoint) {
   base.OnEndpointCreated(endpoint);

   // Sample adding Gzip compression programmatically.  
   // Use when using a ServiceReferences.ClientConfig where a gzip binding cannot be added declaratively (this is a
   // Silverlight limitation since it does not support extensions via config).

   if (endpoint.Binding is CustomBinding) {

      var binding = endpoint.Binding as CustomBinding;
      var elements = binding.CreateBindingElements();

     // Swap out binary/text message encoding for gzip/binary
     var encoding = elements.Find<MessageEncodingBindingElement>();
     if (encoding != null) {
        elements.Remove(encoding);
      }

      encoding = new GZipMessageEncodingBindingElement();
      elements.Insert(0, encoding);
      endpoint.Binding = new CustomBinding(elements);

    }
  }
}
VB
Imports System.ServiceModel.Channels
Imports IdeaBlade.Core.Wcf.Extensions

Public Class ProxyEvents
Inherits IdeaBlade.EntityModel.ServiceProxyEvents

Public Overrides Sub OnEndpointCreated(ByVal endpoint As System.ServiceModel.Description.ServiceEndpoint)
  MyBase.OnEndpointCreated(endpoint)

  ' Sample adding Gzip compression programmatically.  
  ' Use when using a ServiceReferences.ClientConfig where a gzip binding cannot be added declaratively (this is a
  ' Silverlight limitation since it does not support extensions via config).

  If TypeOf endpoint.Binding Is CustomBinding Then

 Dim binding = TryCast(endpoint.Binding, CustomBinding)
 Dim elements = binding.CreateBindingElements()

' Swap out binary/text message encoding for gzip/binary
Dim encoding = elements.Find(Of MessageEncodingBindingElement)()
If encoding IsNot Nothing Then
elements.Remove(encoding)
End If

 encoding = New GZipMessageEncodingBindingElement()
 elements.Insert(0, encoding)
 endpoint.Binding = New CustomBinding(elements)

  End If
End Sub
End Class

Here's a sample ServiceReferences.ClientConfig for reference. Note the binding is using the BinaryMessageEncoding element, which will be replaced at run time by the ServiceProxyEvents implementation above.

XML
<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding" receiveTimeout="00:02:00" sendTimeout="00:02:00">
                  <binaryMessageEncoding />
                  <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
            </customBinding>
        </bindings>
     
      <client>
        <endpoint name="EntityService"
            address="http://localhost:9009/EntityService.svc/sl"
            binding="customBinding" bindingConfiguration="CustomBinding"
            contract="IdeaBlade.EntityModel.IEntityServiceContractAsync"
            />

        <endpoint name="EntityServer"
            address="http://localhost:9009/EntityServer.svc/sl"
            binding="customBinding" bindingConfiguration="CustomBinding"
            contract="IdeaBlade.EntityModel.IEntityServerContractAsync"
            />
      </client>
    </system.serviceModel>
</configuration>


Created by DevForce on March 03, 2011 18:04

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2020 IdeaBlade