(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

WCF NET.TCP Protocol in Silverlight

(10 votes)
Radenko Zec
>
Radenko Zec
Joined Nov 10, 2009
Articles:   3
Comments:   6
More Articles
30 comments   /   posted on Dec 11, 2009
Tags:   wcf , tcp , net.tcp , binding , radenko-zec
Categories:   Data Access , Line-of-Business , General
This article is also available in print (Word, PDF) and e-reader formats (MOBI, EPUB).
Download all formats for $0.99.  Add to Cart

This article is compatible with the latest version of Silverlight.


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 Cassini 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.


Subscribe

Comments

  • -_-

    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
  • RadenkoZec

    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:46

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

    my Email :81552364@163.com

    thanks !!!!!!

  • RadenkoZec

    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?
  • RadenkoZec

    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?
  • ppopadiyn

    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
  • -_-

    Cross platform (Windows & Mac) WCF-Silverlight app


    posted by Roberto Dalmonte on Nov 22, 2010 11:38

    Hi Radenko,
    my goal is to develop a cross-platform INTRANET application with the following components:
    1) WCF Host running on WPF Windows application on a windows machine
    2) Silverlight client hosted indifferently on a windows or mac machine.

    At the moment I have a working application with the following component:
    1) WCF Host running on WPF Windows application on a windows machine
    2) WPF client running on windows machine

    The WCF host is configured this way:


    String baseAddress = string.Format("net.tcp://localhost:{0}/ArchiveEndpoint", port);
                  NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
                  XmlDictionaryReaderQuotas quota = new XmlDictionaryReaderQuotas();
                  quota.MaxStringContentLength = quota.MaxArrayLength = quota.MaxBytesPerRead = binding.MaxBufferSize = Int32.MaxValue;
                  binding.MaxReceivedMessageSize = Int32.MaxValue;
                  binding.ReaderQuotas = quota;
                  binding.TransferMode = TransferMode.Streamed;
                  serviceHost = new ServiceHost(typeof(DataService));
                  serviceHost.AddServiceEndpoint(typeof(IDataContract), binding, baseAddress);
                  serviceHost.Opened += new EventHandler(OnServiceOpened);
                  serviceHost.Open();


    My question:
    is it possible to configure the silverlight client to run against my existing WCF server? This way I might have both WPF clients running on windows machine, Silverlight clients running on either windows or mac machines.
    Thanks
    Roberto Dalmonte

  • RadenkoZec

    RE: WCF NET.TCP Protocol in Silverlight 4


    posted by RadenkoZec on Nov 22, 2010 12:10

    Hi Roberto. 

    You can configure WCF to work with two or more different bindings. You can specify different bindings for same WCF service in config file so you can have one binding for WPF and another for Silverlight and use same WCF service with no changes in code of WCF service.

    Thanks

  • -_-

    RE: WCF NET.TCP Protocol in Silverlight 4


    posted by Roberto Dalmonte on Dec 16, 2010 14:34

     I have a working silverlight application that talks to a self-hosting TCP service wpf application.

    This is the picture: intranet with n silverlight clients and 1 self-hosting TCP service wpf application that works as a server. Everything is wonderful if the clients are running on a windows machine, but I can't make the service work on a  MacBook Pro connected in the very same intranet. Everything looks fine:

    I can ping the Macbook from the server machine, and I can ping back the server machine from the macbook. I can see the shared folders of the server machine from the macbook, nonetheless I can't see anything coming back from the wcf via net.tcp service.

    My question is:

    Is it possible at all a similar scenario (an intranet with Mac's and Pc's living together and using the same WCF service via net.tcp)?.

    Do you have a hint of what could make it work?

    Best Regards

    Roberto Dalmonte

  • -_-

    RE: WCF NET.TCP Protocol in Silverlight 4


    posted by vivek on Dec 17, 2010 09:47
    quite good guys.
  • -_-

    RE: WCF NET.TCP Protocol in Silverlight 4


    posted by selim on Dec 19, 2010 12:25

    Please give me the code   mbstu_sajib@yahoo.com

  • -_-

    RE: WCF NET.TCP Protocol in Silverlight 4


    posted by WE-NEED-CODE on Jan 03, 2011 21:57
    YES PLEASE, SHARE THE CODE.
  • -_-

    RE: WCF NET.TCP Protocol in Silverlight 4


    posted by Roberto Dalmonte on Jan 05, 2011 21:21

    You can find a working sample and explanation here:

    http://blogs.msdn.com/b/carlosfigueira/archive/2010/07/25/enabling-cross-domain-calls-for-sl-apps-on-self-hosted-tcp-services.aspx

  • -_-

    RE: WCF NET.TCP Protocol in Silverlight 4


    posted by Ben on Feb 02, 2011 20:35

    Hi

    can i just have net.tcp as the protocol enabled on the service in IIS and not http. when i remove http it throws an error saying that the service could not be activated.

  • -_-

    RE: WCF NET.TCP Protocol in Silverlight


    posted by alphalima on Mar 06, 2011 17:25
    Setting the server/client config to either localhost or the servername keeps prompting me for username and password.  Do I need to do something else in IIS?  I only have windows authentication enabled.  I tried setting anonymous also, but the window security is still prompting.  What is weird is, when running in VS, browser it also prompts me for user name and password when I run it.  any ideas?  this is only happening with net.tcp apps.
  • -_-

    RE: WCF NET.TCP Protocol in Silverlight


    posted by alphalima on Mar 06, 2011 17:56
    disregard, I finally figured it out that I had to add the IP Address of the server into client local intranet zone.  Somehow the server uses IPv6 and wasn't working with localhost.  By adding the IP address, it stopped prompting for username password.  I will have to check with Admin guys as to why this is the case.  I also had to put the servername in the client config file. 
  • -_-

    Re: WCF NET.TCP Protocol in Silverlight


    posted by on Mar 19, 2012 16:32
    Hi,


    For creating a Silverlight chat application I can recommend the Ozeki VoIP SIP SDK. For sample source codes check this: www.voip-sip-sdk.com/p_257-silverlight-video-chat-example-voip.html


    The support team is also responsive so you can make your project even faster with the help of them.


    BR

Add Comment

Login to comment:
  *      *