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.
- Start the Visual Studio with administrator rights and create one class library project named as 'WCFAddition'.
- 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).
- Open the interface file and create method 'SumOfTwoNumber'.
- [ServiceContract]
- public interface IWCFAddition {
- [OperationContract]
- void DoWork();
- [OperationContract]
- int SumOfTwoNumber(int num1, int num2);
- }
- Now, implement this interface on class file.
- public class WCFAddition: IWCFAddition {
- public void DoWork() {}
- public int SumOfTwoNumber(int num1, int num2) {
- return num1 + num2;
- }
- }
- Now, let's build the project solution and then add new web site in same solution for IIS hosting.
- Give reference of WCF Service to this new website, as shown below.
- Now, open Service.svc file and modify it as below.
- <%@ ServiceHost Language="C#" Debug="true" Service="WCFAddition.WCFAddition"%>
- 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.
- <services>
- <service name="WCFAddition.WCFAddition">
- <endpoint binding="netTcpBinding" bindingConfiguration="ultra" contract="WCFAddition.IWCFAddition" />
- <host>
- <baseAddresses>
- <add baseAddress="net.tcp://localhost:5050/implementclass" />
- <add baseAddress="http://localhost:5051/implementclass" /> </baseAddresses>
- </host>
- </service>
- </services>
- Define the behavior for the netTcpBinding.
- <behaviors>
- <serviceBehaviors>
- <behavior>
- <serviceMetadata httpGetEnabled="true" />
- <serviceDebug includeExceptionDetailInFaults="true" /> </behavior>
- </serviceBehaviors>
- </behaviors>
- Define binding as netTcpBinding with its default parameter properties. Also, define the security mode.
- <bindings>
- <netTcpBinding>
- <binding name="ultra" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647" portSharingEnabled="false" transactionFlow="false" listenBacklog="2147483647">
- <security mode="None">
- <message clientCredentialType="None" />
- <transport protectionLevel="None" clientCredentialType="None" /> </security>
- <reliableSession enabled="false" /> </binding>
- </netTcpBinding>
- </bindings>
- Full web.config
- <?xml version="1.0"?>
- <configuration>
- <appSettings>
- <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings>
- <system.web>
- <compilation debug="false" targetFramework="4.5.1" />
- <httpRuntime targetFramework="4.5.1" /> </system.web>
- <system.serviceModel>
- <services>
- <service name="WCFAddition.WCFAddition">
- <endpoint binding="netTcpBinding" bindingConfiguration="ultra" contract="WCFAddition.IWCFAddition" />
- <host>
- <baseAddresses>
- <add baseAddress="net.tcp://localhost:5050/implementclass" />
- <add baseAddress="http://localhost:5051/implementclass" /> </baseAddresses>
- </host>
- </service>
- </services>
- <bindings>
- <netTcpBinding>
- <binding name="ultra" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647" portSharingEnabled="false" transactionFlow="false" listenBacklog="2147483647">
- <security mode="None">
- <message clientCredentialType="None" />
- <transport protectionLevel="None" clientCredentialType="None" /> </security>
- <reliableSession enabled="false" /> </binding>
- </netTcpBinding>
- </bindings>
- <behaviors>
- <serviceBehaviors>
- <behavior>
- <serviceMetadata httpGetEnabled="true" />
- <serviceDebug includeExceptionDetailInFaults="true" /> </behavior>
- </serviceBehaviors>
- </behaviors>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true" />
- <!--
-
- To browse web app root directory during debugging, set the value below to true.
-
- Set to false before deployment to avoid disclosing web app folder information.
-
- -->
- <directoryBrowse enabled="true" /> </system.webServer>
- </configuration>
- Now, when we have built the solution, we have to host it on IIS. So, open the IIS type 'inetmgr' command on run window.
- It will open IIS. Go to ‘Sites’ and add new website
- New window is open now. Give it a name as 'WCF_Addition' and assign physical path of your IIS project.
- Now, in 'binding' section, select Type as http and add port '5051'. Click on OK button.
- Now, select your web site and click on ‘Advance settings', as shown in the below image.
- In 'Behavior' => 'Enable Protocols' = http,net .tcp and click on OK.
- Now, click on 'Binding' link >> 'Add' button >> and add "net.tcp" and "505:*"
- Our service is hosted now. Let's check it.
- You can see that the service is hosted successfully on IIS and you can browse it on given URL.