Monday, October 22, 2012

Silverlight with Self hosted WCF with NetTcpBinding

Advantages of using NetTcpBinding

  • Performance: Silverlight applications using NetTcpBinding to with WCF service in your intranet  can obtain a performance gain compared to HTTP polling duplex in duplex communication scenarios.
  • Duplex Communication: Supports Duplex communication between client and service.
  • Helps optimize the Silverlight application (XAP) size. Applications that utilize only

  1. Create WCF enabling NetTcpBinding

Sample WCF Config,
        <behavior name="NetTcpWCFService.ServiceBehavior">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="false" />
      <service behaviorConfiguration="NetTcpWCFService "
          name=" NetTcpTest.NetTcpTest ">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding"
            contract="NetTcpTest.INetTcpTestService " />
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
            <add baseAddress="net.tcp://localhost:4502/NetTcpTestService" />

        <binding name="NetTcpBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
                  receiveTimeout="00:10:00" sendTimeout="00:01:00"
                  transactionFlow="false" transferMode="Buffered"
                  listenBacklog="10" maxBufferPoolSize="524288"
                  maxBufferSize="2147483647" maxConnections="10" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
          <security mode="None"/>

 2.    Create Windows Service To Host the WCF

  • ·         Create New Windows Service Project & reference the created WCF dll
  • ·         On Service Start add the following code

  internal static ServiceHost myServiceHost = null;

        protected override void OnStart(string[] args)
            if (myServiceHost != null)
            myServiceHost = new ServiceHost(typeof(NetTcpTestService));

This will up the service on windows service start.

  • ·         On Service Stop add the following code to close the WCF service.

  protected override void OnStop()
            if (myServiceHost != null)
                myServiceHost = null;
3. Create an Outbound rule in machine firewall to open the port which WCF is running. In this case as we have added the base address as "net.tcp://localhost:4502/NetTcpTestService" , open the port ‘4502’

4.       4. Update the clientaccesspolicy file under as follows to C:\inetpub\wwwroot to allow Silverlight    applcations to access the ports enabled.

        <domain uri="*"/>
        <socket-resource port="4502-4534" protocol="tcp" />

5      5. Install the Windows service. Then start the windows service under services.
6      6. Then go to the Silverlight project where you want to access the service.
7      7. Right click on references select add service reference. Add the base address in the WCF config to as Address.

You are ready to use the Self hosted WCF with Silverlight.


  1. Hi Sammani,
    Is this example meant for netTcpBinding with duplex communication.

  2. Hi Maity,
    Yes this is meant for duplex communication