netTcpBinding In WCF Service With IIS Hosting

In this article, you will learn about netTcpBinding in WCF Service with IIS hosting.

Today, we will learn about creating the WCF Service using netTcpBinding and hosting it on IIS Server. If you are new to WCF, then please read my previous article (Create Simple WCF Service And Host It On Console Application).

Now, let's make simple WCF Service which gives the sum of two numbers.
  1. Start the Visual Studio with administrator rights and create one class library project named as 'WCFAddition'.



  2. This will generate default class file automatically. So, we have to delete it and add one new item as 'WCF Service' with the same name, 'WCFAddition'. This will generate one class file and one interface (WCFAddition.cs and IWCFAddition.cs).

  3. Open the interface file and create method 'SumOfTwoNumber'.
    1. [ServiceContract]  
    2. public interface IWCFAddition {  
    3.     [OperationContract]  
    4.     void DoWork();  
    5.     [OperationContract]  
    6.     int SumOfTwoNumber(int num1, int num2);  
    7. }  

  1. Now, implement this interface on class file.
    1. public class WCFAddition: IWCFAddition {  
    2.     public void DoWork() {}  
    3.     public int SumOfTwoNumber(int num1, int num2) {  
    4.         return num1 + num2;  
    5.     }  
    6. }  

  1. Now, let's build the project solution and then add new web site in same solution for IIS hosting.


  1. Give reference of WCF Service to this new website, as shown below.


  1. Now, open Service.svc file and modify it as below.
    1. <%@ ServiceHost Language="C#" Debug="true" Service="WCFAddition.WCFAddition"%>    

  1. The main part will start now and we have to make changes in web.config file. So here, first we have defined endpoint with netTcpBinding and then defined the base address for the same, as show below.
    1. <services>  
    2.     <service name="WCFAddition.WCFAddition">  
    3.         <endpoint binding="netTcpBinding" bindingConfiguration="ultra" contract="WCFAddition.IWCFAddition" />  
    4.         <host>  
    5.             <baseAddresses>  
    6.                 <add baseAddress="net.tcp://localhost:5050/implementclass" />  
    7.                 <add baseAddress="http://localhost:5051/implementclass" /> </baseAddresses>  
    8.         </host>  
    9.     </service>  
    10. </services>  

  1. Define the behavior for the netTcpBinding.
    1. <behaviors>  
    2.     <serviceBehaviors>  
    3.         <behavior>  
    4.             <serviceMetadata httpGetEnabled="true" />  
    5.             <serviceDebug includeExceptionDetailInFaults="true" /> </behavior>  
    6.     </serviceBehaviors>  
    7. </behaviors>  

  1. Define binding as netTcpBinding with its default parameter properties. Also, define the security mode.
    1. <bindings>  
    2.     <netTcpBinding>  
    3.         <binding name="ultra" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647" portSharingEnabled="false" transactionFlow="false" listenBacklog="2147483647">  
    4.             <security mode="None">  
    5.                 <message clientCredentialType="None" />  
    6.                 <transport protectionLevel="None" clientCredentialType="None" /> </security>  
    7.             <reliableSession enabled="false" /> </binding>  
    8.     </netTcpBinding>  
    9. </bindings>  

  1. Full web.config
    1. <?xml version="1.0"?>  
    2. <configuration>  
    3.     <appSettings>  
    4.         <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings>  
    5.     <system.web>  
    6.         <compilation debug="false" targetFramework="4.5.1" />  
    7.         <httpRuntime targetFramework="4.5.1" /> </system.web>  
    8.     <system.serviceModel>  
    9.         <services>  
    10.             <service name="WCFAddition.WCFAddition">  
    11.                 <endpoint binding="netTcpBinding" bindingConfiguration="ultra" contract="WCFAddition.IWCFAddition" />  
    12.                 <host>  
    13.                     <baseAddresses>  
    14.                         <add baseAddress="net.tcp://localhost:5050/implementclass" />  
    15.                         <add baseAddress="http://localhost:5051/implementclass" /> </baseAddresses>  
    16.                 </host>  
    17.             </service>  
    18.         </services>  
    19.         <bindings>  
    20.             <netTcpBinding>  
    21.                 <binding name="ultra" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647" portSharingEnabled="false" transactionFlow="false" listenBacklog="2147483647">  
    22.                     <security mode="None">  
    23.                         <message clientCredentialType="None" />  
    24.                         <transport protectionLevel="None" clientCredentialType="None" /> </security>  
    25.                     <reliableSession enabled="false" /> </binding>  
    26.             </netTcpBinding>  
    27.         </bindings>  
    28.         <behaviors>  
    29.             <serviceBehaviors>  
    30.                 <behavior>  
    31.                     <serviceMetadata httpGetEnabled="true" />  
    32.                     <serviceDebug includeExceptionDetailInFaults="true" /> </behavior>  
    33.             </serviceBehaviors>  
    34.         </behaviors>  
    35.         <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>  
    36.     <system.webServer>  
    37.         <modules runAllManagedModulesForAllRequests="true" />  
    38.         <!--  
    39.   
    40. To browse web app root directory during debugging, set the value below to true.  
    41.   
    42. Set to false before deployment to avoid disclosing web app folder information.  
    43.   
    44. -->  
    45.         <directoryBrowse enabled="true" /> </system.webServer>  
    46. </configuration>  

  1. Now, when we have built the solution, we have to host it on IIS. So, open the IIS type 'inetmgr' command on run window.


  1. It will open IIS. Go to ‘Sites’ and add new website


  1. New window is open now. Give it a name as 'WCF_Addition' and assign physical path of your IIS project.


  1. Now, in 'binding' section, select Type as http and add port '5051'. Click on OK button.


  1. Now, select your web site and click on ‘Advance settings', as shown in the below image.


  1. In 'Behavior' => 'Enable Protocols' = http,net .tcp and click on OK.



  1. Now, click on 'Binding' link >> 'Add' button >> and add "net.tcp" and "505:*"


  1. Our service is hosted now. Let's check it.


  1. You can see that the service is hosted successfully on IIS and you can browse it on given URL.