Introduction To WCF And Host The Application With Multiple Endpoints In Self Hosting

In this article, I will explain how to host the WCF application with multiple endpoints through self hosting. Before that, we should understand what WCF is and why we need it.

What is WCF

WCF is used to build distributed applications and introduced in .NET framework 3.0. The interoperability is the main use of WCF.

Why we need WCF

Before WCF we hada lot of distributed application frameworks in .NET, so then why do we need to go to WCF?

Just consider the following situation:
  1. One customer needs the distributed application over the HTTP protocol.
  2. Another customer needs the same distributed application through the TCP protocol.

If we want to do this without WCF, we need to develop two applications. One is ASP.NET web service to communicate over the HTTP, and another one is a .NET Remoting application to communicate over the TCP.

HTTP and another

So, we would need to develop two applications for the same business logic, and it is a waste of time. This problem is overcome by WCF. Here we configure two different endpoints.

configure two different endpoint

So, WCF supports the different types of communication. So our time is saved here, compared with the previous scenario. WCF also supports security.

I think it is enough to introduce WCF. We will see how to create a simple WCF application, configure two endpoints and host it through self hosting. We will also see a WCF client application to consume the service through TCP and HTTP protocol.

Create and configure WCF application

Step 1: Open Visual Studio 2013, open new project. The following window shows the same:

new project

Step 2: Select the Class Library project and name the project name as “HelloSample” and click OK.

Class liberary

Step 3:
Add the WCF Service.

WCF Service

Step 4: Declare method named “SayHello” in the service interface and implement it in the class.

  1. namespace HelloSample  
  2. {  
  3.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHelloService" in both code and config file together.  
  4.     [ServiceContract]  
  5.     public interface IHelloService  
  6.     {  
  7.         [OperationContract]  
  8.         string SayHello(string name);  
  9.     }  
  10. }  
  11. namespace HelloSample  
  12. {  
  13.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloService" in both code and config file together.  
  14.     public class HelloService: IHelloService  
  15.     {  
  16.         public string SayHello(string name)  
  17.         {  
  18.             return "Hello " + name;  
  19.         }  
  20.     }  
  21. }  
Step 5: Createa console project and add reference of the HelloSample project reference in it.

Console application

project

Step 6: Add an application configuration file in the console project:

Add application configuration

Step 7: Add the following configuration settings in the App.config file:
  1. <system.serviceModel>  
  2.     <services>  
  3.         <service name="HostingDifferentTrans.HelloService" behaviorConfiguration="HelloService.Behaviour">  
  4.             <endpoint address="HelloService" binding="basicHttpBinding" contract="HostingDifferentTrans.IHelloService"></endpoint>  
  5.             <endpoint address="HelloService" binding="netTcpBinding" contract="HostingDifferentTrans.IHelloService"></endpoint>-  
  6.             <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>  
  7.             <host>  
  8.                 <baseAddresses>  
  9.                     <add baseAddress="http://localhost:8080" />  
  10.                     <add baseAddress="net.tcp://localhost:8081" /> </baseAddresses>  
  11.             </host>  
  12.         </service>  
  13.     </services>  
  14.     <behaviors>  
  15.         <serviceBehaviors>  
  16.             <behavior name="HelloService.Behaviour">  
  17.                 <serviceMetadata httpGetEnabled="true" /> </behavior>  
  18.         </serviceBehaviors>  
  19.     </behaviors>  
  20. </system.serviceModel>  
Here, we add two endpoints for HTTP and TCP.

Step 8: Add this code to host the application in the console application.
  1. ServiceHost host = new ServiceHost(typeof(HostingDifferentTrans.HelloService));  
  2. host.Open();  
  3. Console.WriteLine("Service Started.");  
Step 9: Build and start the console application in the administrator.

Now, the application is ready to consume at the client application. You can access it by using the url http://localhost:8080.

Consume at the client application

For the sample application create one console application with the name “WCFClient” and follow the below steps to access the above implemented service.

Step 1:
Right click on the reference folder and select the Add and then click Service Reference option.

Service Reference Option

Step 2: The window will appear, and enter the URL of the service in the Address text box and hit the discover button. It will display the set of services available on the service.

discover button

Step 3: Select the service and hit OK button.

Step 4: Now implement the following code to access the service.
  1. Access the service through Http protocol
    1. HelloServerRef.HelloServiceClient client = new HelloServerRef.HelloServiceClient("BasicHttpBinding_IHelloService");  
    2. Console.WriteLine(client.SayHello("Sakthikumar"));  
  2. Access the service through TCP protocol
    1. HelloServerRef.HelloServiceClient client = new HelloServerRef.HelloServiceClient("NetTcpBinding_IHelloService");  
    2. Console.WriteLine(client.SayHello("Sakthikumar"));  

Note: Here we configured two endpoints. For that purpose only we mentioned the name in the HelloServiceClient object creation. If we have a single endpointthere is no need to mention it.

If you have any queries, please feel free to ask. I will clarify it.

Up Next
    Ebook Download
    View all
    Learn
    View all