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:
- One customer needs the distributed application over the HTTP protocol.
- 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.
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.
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:
Step 2: Select the Class Library project and name the project name as “HelloSample” and click OK.
Step 3: Add the WCF Service.
Step 4: Declare method named “SayHello” in the service interface and implement it in the class.
- namespace HelloSample
- {
-
- [ServiceContract]
- public interface IHelloService
- {
- [OperationContract]
- string SayHello(string name);
- }
- }
- namespace HelloSample
- {
-
- public class HelloService: IHelloService
- {
- public string SayHello(string name)
- {
- return "Hello " + name;
- }
- }
- }
Step 5: Createa console project and add reference of the
HelloSample project reference in it.
Step 6: Add an application configuration file in the console project:
Step 7: Add the following configuration settings in the
App.config file:
- <system.serviceModel>
- <services>
- <service name="HostingDifferentTrans.HelloService" behaviorConfiguration="HelloService.Behaviour">
- <endpoint address="HelloService" binding="basicHttpBinding" contract="HostingDifferentTrans.IHelloService"></endpoint>
- <endpoint address="HelloService" binding="netTcpBinding" contract="HostingDifferentTrans.IHelloService"></endpoint>-
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8080" />
- <add baseAddress="net.tcp://localhost:8081" /> </baseAddresses>
- </host>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="HelloService.Behaviour">
- <serviceMetadata httpGetEnabled="true" /> </behavior>
- </serviceBehaviors>
- </behaviors>
- </system.serviceModel>
Here, we add two endpoints for HTTP and TCP.
Step 8: Add this code to host the application in the console application.
- ServiceHost host = new ServiceHost(typeof(HostingDifferentTrans.HelloService));
- host.Open();
- 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.
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.
Step 3: Select the service and hit OK button.
Step 4: Now implement the following code to access the service.
- Access the service through Http protocol
- HelloServerRef.HelloServiceClient client = new HelloServerRef.HelloServiceClient("BasicHttpBinding_IHelloService");
- Console.WriteLine(client.SayHello("Sakthikumar"));
- Access the service through TCP protocol
- HelloServerRef.HelloServiceClient client = new HelloServerRef.HelloServiceClient("NetTcpBinding_IHelloService");
- 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.