WCF Self Hosing


Introduction

This article explains how to achieve WCF service self hosting, In WCF service as I already explained in my previous article, means programmatic hosting your WCF service using managed code. You can host your WCF service by using console application or windows application.





Here in this article I have used console application to host WCF service, here in my console application I have hosted WCF service at run time. It is very easy to host WCF service using program like hosting in IIS. To host your service you have to implement or use following main 3 classes

  1. Uri: This class is used to create base address (URL) for your WCF service, Its comes under system name space. fore more details Click Here
  2. ServiceHost: Servicehost class is used to create or provides a host for you service and this class is comes under System.ServiceModel namespace. Click here fore more details
  3. ServiceMetadataBehavior: This class is comes under System.ServiceModel.Description namespace and System.ServiceModel (in System.ServiceModel.dll) assembly. Fore more details click here

Getting Started

Hosting WCF service using self hosting is a setp by step process, follow the below steps to get the service hosted.

Step-1:

First build your service in release mode, because building or compiling service in release mode is faster than debug mode.

Step-2:

Add one more console project to your existing service project or you can create different console project, add the reference of your service project. For example Let's say your service project name is hellowworld service. add the reference of your hellowworld service to this project. Again add the reference of System.ServiceModel dll. to get the related class to host your service





Step-3:

In main method of console application create a object of Uri class to get the base address of your service, this class takes string argument in his constructor. The string argument is the address pattern of your service like following.

Address pattern: protocol:hostname:port/Namespace/classname

 Uri Url = new Uri("http://localhost:8090/HellowWorldService/HelloWorld");   

call AddServiceEndpoint function of servicehost calss to add endpoint to service, this function takes three argument one is service contract type, service binding and binding name optional, you can give empty string with binding name.

 host.AddServiceEndpoint(typeof(typeof(HellowWorldService.IHelloWorld), new SHttpBinding(), "");  

create object of ServiceMetadataBehavior class to get enable metadata exchange for your class. and set HttpGetEnabled propery to true

 ServiceMetadataBehavior behavior = newServiceMetadataBehavior();   
            behavior .HttpGetEnabled = true;   

Add the service behavior to service host class and finally start the service by calling open method of service host class.

 namespace MyHostingApplication  
 {   
   class Program   
   {   
    static void Main(string[] args)   
    {   
           // Create a URI to serve as the base address   
            ///  
               Address pattern protocol:hostname:port/Namespace/classname  
            ///  
            Uri Url = new Uri("http://localhost:8090/HellowWorldService/HelloWorld");   
       //Create ServiceHost   
      ServiceHost host = new ServiceHost(typeof(HellowWorldService.IHelloWorldr), httpUrl);   
      //Add a service endpoint   
      host.AddServiceEndpoint(typeof(HellowWorldService.IHelloWorld), newWSHttpBinding(), "");   
    //Enable metadata exchange   
      ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();   
      behavior.HttpGetEnabled = true;   
      host.Description.Behaviors.Add(behavior);   
   //Start the Service   
      host.Open();   
      Console.WriteLine("Service is host at " +DateTime.Now.ToString());   
      Console.WriteLine("Host is running... Press key to stop");   
      Console.ReadLine();   
    }   
   }   
 }   

Related Articles

  1. WCF IIS Hosting
  2. Hosting WCF Service

Summary

Hope you must get how to do WCF Selft hosting. if you getting any mistake in this article, please feel free to comment.

Thanks


No comments:

Post a Comment