Wednesday, November 13, 2013

Self Hosting in WCF



                 In this article we are going to see self hosting in WCF service with client and server example. Let us begin.

WCF:

WCF is a combination of multiple technologies which includes COM+, MSMQ, .NET Remoting, Web services, etc. for communication. 

Self Hosting:

Self hosting is one kind which makes hosting services in our own application. With Self Hosting what we need to configure our service endpoint and call open() method of ServiceHost to make the service ready to be invoked. To host WCF service you need WCF runtime and .NET application where you want to host your service. 

Hosting:

Hosting is the process of making the service available for the client.

Using three types we can host service in WCF:
1. Self-Hosting
2. IIS Hosting
3. WAS 

Self hosting step by step: 

It has the major benefits of providing us full control over the service like starting, stoping and error handling/logging can be done in our host application. The amount of code that needs to be written to self host a WCF service is very small and it is very easy too. Use it if you full control on the service.

Step 1: Create WCF Class Library
Create Wcf class library by File -> new ->project-> WCF service library.

Step 2: Create WCF service
Create WCF service with following methods and implement it


Step 3:  Modify App.Config accordingly, as below



Description:
<add baseAddress="http://localhost:8888/WcfServiceLibrary/CalculatorService/"/>

               Above address is the Service address which is going to be communicated, define some other port number like 8888 to avoid conflict, default port number is 80.

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>

            Above end point is used to exchange meta data between client and server.

Step 4: Create Console Application, which act as server and add WCF Service Library, System.ServiceModel DLL to it 


Step 5:  Implement Server by using Uri, ServiceHost and add ServiceMetadataBehavior
 To it




Step 6: Create client to consume service from server

1. Create Console application as client to use service.

2.  Run the server application and copy the server url http://localhost:8888/WcfServiceLibrary/CalculatorService/
From App.config in server

3. Add Service reference to client application and paste the address from App.Config


Step 7: Create object for service and invoke


Project Description:


No comments:

Post a Comment