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
Steps
- Create WCF enabling NetTcpBinding
Sample WCF Config,
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NetTcpWCFService.ServiceBehavior">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NetTcpWCFService
"
name=" NetTcpTest.NetTcpTest
">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding"
contract="NetTcpTest.INetTcpTestService
" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:4502/NetTcpTestService" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered"
transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"
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"/>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
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.Close();
}
myServiceHost = new ServiceHost(typeof(NetTcpTestService));
myServiceHost.Open();
}
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.Close();
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.
<access-policy>
<cross-domain-access>
<policy>
<allow-from>
<domain uri="*"/>
</allow-from>
<grant-to>
<socket-resource
port="4502-4534" protocol="tcp" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
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.
Hi Sammani,
ReplyDeleteIs this example meant for netTcpBinding with duplex communication.
Hi Maity,
ReplyDeleteYes this is meant for duplex communication