Skip Navigation LinksHome / Articles / View Article

WCF NET.TCP Protocol in Silverlight 4

+ Add to SilverlightShow Favorites
20 comments   /   posted by Radenko Zec on Dec 11, 2009
(6 votes)
Categories: Learn , Tutorials

Introduction

As you may already know, Silverlight 4 beta has been released and it has many new features. One of the most important features is net.tcp binding support. Now in Silverlight 4 we can communicate with WCF web service using net.tcp protocol. You may have noticed the many blog posts or articles that came out with a title similar to this : Silverlight 4 – WPF killer. If I can choose the one most important feature in Silverlight 4 beta that can kill WPF apps I will choose the net.tcp support.

If you have already worked with Silverlight 3 you probably have some WCF web service. In Silverlight 3 you have been limited to use basic http binding for communication with WCF service. One of the major performance improvements in Silverlight 3 was the possibility to add binary encoding to basic http binding that serializes and deserializes your messages in binary format. Net.tcp binding uses binary message encoding by default.

Why to use net.tcp binding ?

A key benefit of net.tcp binding is the performance. If you want fast Silverlight applications that communicate with your WCF service in your secure intranet environment you will probably use this binding. 

This net.tcp binding for now is intended to be used in intranet environments. Because this protocol is build on top of Silverlight sockets implementation it has same network security restrictions. Silverlight restricts the ports of TCP socket connections to the range 4502 – 4534 . Because of that it requires environment where we can control the firewall configuration. In this way we can easily monitor Silverlight applications traffic and maintain the security of our corporate network.

Net.tcp protocol in Silverlight also does not support transport level security. For example, we can’t have SSL communication.

Net.tcp duplex support

Another key benefit of net.tcp binding is the support for duplex communication. Now we can use same HTTP polling duplex proxy from the previous version of Silverlight and just specify that the proxy will use net.tcp binding and everything will work. We can even make WCF service that uses HTTP polling duplex protocol for Internet clients and uses net.tcp protocol for Intranet clients. The programming model is the same like before and is simple to use.

Problems with installation

When I tried to debug an example of a WCF application using net.tcp binding  I ran into the following problem. I constantly received the error : The protocol 'net.tcp' is not supported .

Error1

It seems that Visual Studio Integrated Cassino server supports only HTTP activation. So after that I tried to deploy my application into IIS.

Only IIS7 has support for net.tcp binding so you must have IIS7 installed on your machine.

WCF services with net.tcp binding inside IIS7 are hosted on Windows Process Activation Services (WAS). To enable WAS hosting on IIS7 for net.tcp you must do a few things:

1) You must enable Windows Communication Foundation Non-HTTP Activation. One important feature of IIS is the ability to activate a web application when an HTTP request for that application is received. A similar feature exists in IIS7 for net.tcp , but it may not be enabled by default.

Feature

2) You must set the application poll to use Framework 4 enabled poll and must enable the net.tcp protocol.

Settings

3) You must also enable port or ports in range 4502 – 4534 for net.tcp binding.

SiteBinding

Or you can skip step two and three and do it faster and quicker using command prompt. IIS provides a new command-line utility to configure Web sites : Appcmd.exe. The command bellow will update the configuration file for WAS, applicationHost.config, with new binding for the default web site :

%windir%\system32\inetsrv\appcmd.exe set site     
"Default Web Site" -+bindings.[protocol=    
'net.tcp',bindingInformation='*']  

After that you need to check if net.tcp listner adapter service is running. You can do it by executing the following command sc query NetTcpActivator or look in services :

 

Net.Adapter

If you get the following error : Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].

SchemasError

the problem might be because you haven’t specify a base address for net.tcp binding in service web.config file. You need to specify a base address similar to this :

 

endpoint address="net.tcp://localhost:4502/wcservice/Service.svc"  

If we want to generate proxy on client side and we have only specified net.tcp binding in our service, we must set up the Metadata Exchange (mex) endpoint to use the net.tcp binding :

<endpoint address="mex"   
 binding="mexTcpBinding"   
 contract="IMetadataExchange" />  

Also if we want to allow  to share the same port on different services, we must enable the port sharing service for net.tcp. We can do it by executing the following command in command prompt : C:\sc.exe config NetTcpPortSharing start= demand

After that you must allow Silverlight applications to communicate over TCP. In order to do that the server must explicitly allow such a connection. This is done by exposing a TCP socket policy over TCP port 943.

This is how an example policy file for sockets looks like :

<?xml version="1.0" encoding ="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from>
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <socket-resource port="4502-4506" protocol="tcp" />
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>
 

To make our life easier, Tomasz Janczuk from Microsoft has created a free template for console applications that serves like TCP socket policy server.

SilverlightTCPSocketPolicyServer

You can later make windows service that will do the same thing on hosting server. This console application receives requests from Silverlight clients and returns socket policy file to the client. You must run this console app on server to enable Silverlight applications to communicate over TCP.

SocketConsole

After that we are up and running….

Summary

Net.tcp binding support in Silverlight 4 beta is one of the most important new features. It brings us excellent performance but with some security constraints such us port limitations. Because of that it is recommended to use it for Intranet applications in secure environments where you can control firewall settings. It can be great alternative in such environments for HTTP polling duplex or for Basic HTTP binding. This protocol supports a simple to use WCF duplex programming model. I hope that in the near future we will see a much better support for net.tcp binding in Silverlight without security constraints.

Share


Comments

Comments RSS RSS
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by Catherine Russell on Dec 11, 2009 16:34
    well done!
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by gavin whyte on Dec 12, 2009 11:28
    Excellent
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by RadenkoZec on Dec 12, 2009 12:29
    Thanks
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by River on Dec 16, 2009 09:42
    没看明白~!!!!
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by River on Dec 16, 2009 09:46

    can you  give  me  a Demo  for silverlight 4 use wcf net.tcp  chat ?

    my Email :81552364@163.com

    thanks !!!!!!

  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by RadenkoZec on Dec 16, 2009 09:52
    Hi River. You have sample chat application using net.tcp on Tomasz Janczuk blog (works at Microsoft)
    http://janczuk.org/code/samples/pubsubsamplenettcp.zip
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by River on Dec 16, 2009 10:40
    Thanks , Hi  RadenkoZec , about you give me source code address ,I  thanks again .But I need  Silverlight net.tcp  Duplex communication, WCF Service is independent's Service , it's not  in Silverlight Service .Can you give me independent's WCF Service  ,and Silverlight4 net.tcp Duplex call.
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by River on Dec 16, 2009 13:10
    Thanks;RadenkoZec.
    This problem I have been solved,
    I really appreciate your help.
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by Jan on Dec 17, 2009 18:25
    finally!
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by River on Dec 18, 2009 10:32

     Hi  RadenkoZec 

    Do you have the video chat Demo for silverlight 4.

    If has; can you give me it ?

    my email :81552364@163.com

    Thanks   !!!

     

  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by River on Dec 19, 2009 05:52

    Who can give me a video chat Demo for silverlight 4 .

    My Email : yu.zhenjiang@live.cn  .

    Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    ^.^                    ~.~             #.#           @.@  

     

  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by Bryan Livingston on Jan 03, 2010 04:12
    I don't understand the intranet and firewall requirements.

    If I build and deploy a WCF server that uses the required port range, and then deploy on a public web site a silverlight 4 app that talks to the WCF server, then are you saying that no one will be able to hit the service without messing with their firewall?

    Is it their windows firewall on their own computer or is it their home routers or does it have something to do with NAT (Network Address Translation)? Why wouldn't the silverlight app be able to initiate a duplex tcp connection with the server like anything else?
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by RadenkoZec on Jan 03, 2010 11:38
    When using TCP from Silverlight 4 we need to use port in specific range from 4502-4534 because Silverlight TCP is build on top of sockets and has same network security restrictions. See http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx
    So on client side when we connect to Silverlight 4 app that uses TCP we must connect through some of these ports 4502-4534. If client has firewall maybe these ports are disabled in firewall by default or when we connect firewall it will ask to add exception. We can't assume that client will click yes to add exception in firewall for our app.

    I hope this helps.
      

  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by Dex on Mar 30, 2010 18:03
    This is great! another cool additional features of Silverlight! Though in terms of performance and speed without the hassle of  ISS configuration and the like, I still consider using Silverlight implementation of the Berkely Socket Interface.
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by Rick on Apr 06, 2010 17:01
    You said you have to use IIS to provide a WCF service, but this is not necessary. WCF can run without IIS if you are using TCP as the protocol. On the other hand, if your using WCF RIA you will need IIS from what I can tell so far. I'm guessing there's a way to configure it to not require IIS but I have not had time to research further.
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by Eugene on Apr 16, 2010 00:33
    Would anybody please tell me into which assempbly NetTcpBinding is included in VS2010 RTM? I expected to find it in System.ServiceModel but looks like it's not there....
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by Mark on Apr 16, 2010 21:47
    I don't see it, either. Anybody?
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by ppopadiyn on Apr 20, 2010 15:45

    Hi Mark and Eugene,

    I don't know, I also couldn't find it. Probably it is not available for Silverlight.

  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by china liminghua on May 19, 2010 08:34
    windows xp vs2010 silverlight4 not well
  • RE: WCF NET.TCP Protocol in Silverlight 4  

    posted by ro on Jul 28, 2010 09:15
    you`re the man!! thanks for the light

Add Comment

 
 

   
  
  
   
Please add 4 and 4 and type the answer here:

Did you notice our new Silverlight-based Showcase section ? Check it out to get a bird's eye view of all showcases featured on SilverlightShow, with a quick thumbnail preview for easier browsing. Want to view the most recent showcases only? Use Group by Month option for a chronological listing.
This is the second redesigned, entirely Silverlight-based section in SilverlightShow, after the new Books section. We look forward to your feedback on both! (hide this)