In this article, I would like to give a brief idea on using
NetTcpBinding with Silverlight application and hosting configuration to be done on IIS 7.
Note: Only IIS7 has support for net.tcp binding.
One of the most important features in Silverlight 4 is the introduction of
NetTcpBinding support; we can communicate with a WCF service using net.tcp protocol.
Advantages of using NetTcpBinding
- Performance: Silverlight applications using
NetTcpBinding communicates with WCF service in your intranet zone. Performance of
NetTcpBinding compared to HTTP polling duplex is the key reason to consider using the WCF net.tcp protocol in duplex communication scenarios.
NetTcpBinding offers major performance gains when compared to
httpPollingDuplex on these parameters:
- Increase in throughput of data notifications a client application can receive from the server.
- Increase in the maximum number of connected clients the server can support concurrently.
- Duplex Communication: Supports Duplex communication between client and service.
- IDE Integration: Net.tcp protocol is well integrated with the Add Service Reference feature in Visual Studio 10. A WCF service proxy to a WCF service exposed using the net.tcp binding can be easily generated for a Silverlight 4 client application. Net.tcp protocol comes with Silverlight configuration support (
ServiceReferences.ClientConfig), so the details of the binding, including service address, can be changed without recompiling the application.
- Improved library factoring to help optimize the Silverlight application (XAP) size. Applications that utilize only request/response exchange patterns using WCF have all the required components available in Silverlight core. Applications that require duplex message exchange patterns must include the System.ServiceModel.Extensions.dll in the XAP, as well as one or both of System.ServiceModel.NetTcp.dll and System.ServiceModel.PollingDuplex.dll, depending on the protocol the application is using. System.ServiceModel.Extensions.dll contains functionality common for all duplex communication scenarios. System.ServiceModel.NetTcp.dll supports the WCF net.tcp protocol and System.ServiceModel.PollingDuplex.dll the HTTP polling duplex protocol. When a proxy is created in a Silverlight 4 project using the Add Service Reference feature of Visual Studio 10, appropriate library references will be added to the project depending on the actual protocols the WCF service exposes.
NetTcpBinding is subject to TCP port limitations, Silverlight applications with
NetTcpBinding can run only on ports 4502-4534.
Configuring web.config for Silverlight Application
- Create a new Visual Studio solution which consists of a Silverlight 4 application and website to serve the application. Once we have the application in place, we need to consume the WCF service. To do this, we need to configure the (web.config) file of the Silverlight web application. By default, the code in web.config looks as shown below:
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
We need to change the configuration to the following:
<binding name="InsecureTcp" receiveTimeout="Infinite">
<security mode="None"/> Net Tcp
<endpoint address="" binding="netTcpBinding" bindingConfiguration="InsecureTcp"
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
Configuring IIS for Silverlight Application
The configuration changes for hosting the WCF and Silverlight application on IIS on Windows 7 is as follows:
- Install IIS in the machine
To install IIS, click on Start->Run and type appwiz.cpl in the Command window and press Enter. Now click on Turn Windows features on/off.
- Enable Internet Information Services and ensure that all the subfolders under IIS are checked and Click on Ok. This will install IIS on your machine.
Fig.1 Install IIS
- WCF services with net tcp binding inside IIS are hosted on Windows Activation Services (WAS). To enable WAS hosting on IIS, a few configurations are needed to be done. You must enable Windows Communication Foundation Non-Http Activation on IIS.
Fig.2 Install WCF Non-HTTP Activation
- Go to the solution explorer of the project->Right click on the Silverlight web project-> Click on Properties. Then modify the web site configuration to run under IIS by selecting "Use Local IIS Web server". It will auto fill a project URL and then Click on 'Create Virtual Directory' to set up the application in IIS.
Fig.3 Modify web site to run under IIS.
Configuring Properties for the Hosted Application
- Go to Inetmgr->Project/Website->Advanced Settings.
Fig.4 IIS Settings
Change the ASP.NET configuration to the version of the .NET Framework in which the WCF Service is built.
Fig.5 Change Application Pool.
Fig.6 Set Application Pool to ASP.NET v4.0 to use Framework4
Also add the net.tcp protocol in the Enabled Protocols under Behavior section as shown below:
Fig.7 Enable net.tcp Protocol
- Go to Inetmgr->Project/Website->Edit Bindings.
Fig.8 Edit Bindings
- In Site Bindings->Edit.
Fig.9 Click on Edit to add port number
- Add the Port on Binding Information i.e., 4502 as we already know Net Tcp binding supports ports(4502-4535) shown below: click on Ok.
Fig.10 Adding the port number.
- Make sure that the Net Tcp Listener Adapter is started and running in Services.msc:
Fig.11 Check for Net Tcp Adapters in Services.msc
Note: After this configuration, you will be able to access your application either using IpAddress of server or using machine name. The best way to configure is use machine name/ IpAddress in both places (IIS and web.config).