14 March 2014

Self-Hosting -- WCF Hosting

         In web service, we can host the service only in IIS, but WCF provides the user to host the service in any application (e.g. console application, Windows form etc.). Very interestingly developer is responsible for providing and managing the life cycle of the host process. Service can also be in-pro i.e. client and service in the same process. Now let's us create the WCF service which is hosted in Console application. We will also look in to creating proxy using 'Client Base' abstract class.

Note: Host process must be running before the client calls the service, which typically means you have to Pre-Launch it.

Step 1: First let's start create the Service contract and it implementation. Create a console application and name it as MyCalculatorService. This is simple service which returns addition of two numbers.

Step 2: Add the System.ServiceModel reference to the project.

                               

Step 3: Create an ISimpleCalculator interface, Add ServiceContract and OperationContract attribute to the class and function as shown below. You will know more information about these contracts in later session. These contracts will expose method to outside world for using this service.

MyCalculatorService.cs

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.ServiceModel;

Namespace MyCalculatorService

{

    [ServiceContract ()]

    Public interface ISimpleCalculator

    {

        [OperationContract ()]

        Int Add (int num1, int num2);

    }

}
Step 4: MyCalculatorService is the implementation class for IMyCalculatorService interface as shown below.

MyCalculatorService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Namespace MyCalculatorService
{
    Class SimpleCalculator:ISimpleCalculator
    { 
Public int Add (int num1, int num2)
{   
Return num1 + num2;
}
    }
}
Step 5: Now we are ready with service. Let's go for implementing the hosting process. Create a new console application and name it as 'MyCalculatorServiceHost'
Step 6: Service Host is the core class use to host the WCF service. It will accept implemented contract class and base address as contractor parameter. You can register multiple base addresses separated by commas, but address should not use same transport schema.
Uri httpUrl = new Uri ("http://localhost:8090/MyService/SimpleCalculator");
Uri tcpUrl= new Uri ("net.tcp://localhost:8090/MyService/SimpleCalculator");
Service Host host = new Service Host (typeof (MyCalculatorService.SimpleCalculator), httpUrl, tcpUrl);
Multiple end points can be added to the Service using AddServiceEndpoint() method. Host. Open () will run the service, so that it can be used by any client.
Step 7: Below code show the implementation of the host process.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
using System.ServiceModel;
using System.ServiceModel. Description;
namespace MyCalculatorServiceHost
{
  class Program
    { 
static void Main(string[] args)
        {   
//Create a URI to serve as the base address
Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator");
  Service Host host = new Service Host (typeof (MyCalculatorService.SimpleCalculator), httpUrl);
    //Add a service endpoint
   host. AddServiceEndpoint (typeof (MyCalculatorService.SimpleCalculator) , new WsHttpBinding (), "");
   //Enable metadata exchange
  ServiceMetadata Behavior smb = new ServiceMetadata Behavior ();
  smb.HttpGetEnabled = true;
  host.Description.Behaviors.Add(smb);
     //Start the Service
   host.Open ();
   Console.WriteLine ("Service is host at " + DateTime.Now.ToString());
   Console.WriteLine ("Host is running... Press <Enter> key to stop");
   Console.ReadLine ();
       }
    }
}
Step 8: Service is hosted, now we need to implement the proxy class for the client. There are different ways of creating the proxy
  • Using SvcUtil.exe, we can create the proxy class and configuration file with end points.
  • Adding Service reference to the client application.
  • Implementing Client Base<T> class
Of these three methods, Implementing Client Base <T> is the best practice. If you are using rest two method, we need to create proxy class every time when we make changes in Service implementation. But this is not the case for ClientBase<T>. It will create the proxy only at runtime and so it will take care of everything.
MyCalculatorServiceProxy.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.ServiceModel;
Using MyCalculatorService;
Namespace MyCalculatorServiceProxy
{
Public class MyCalculatorServiceProxy:
//WCF create proxy for ISimpleCalculator using Client Base
Client Base<ISimpleCalculator>,ISimpleCalculator
  { 
Public int Add (int num1, int num2)
{
//Call base to do function
Return base.Channel.Add (num1, num2);
}
    }
}
Step 9: In the client side, we can create the instance for the proxy class and call the method as shown below. Add proxy assembly as reference to the project.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.ServiceModel;
Namespace MyCalculatorServiceClient
{
  Class Program
    {
Static void Main (string [] args)   
{ 
MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy ;
    Proxy= new MyCalculatorServiceProxy.MyCalculatorServiceProxy();  
Console.WriteLine ("Client is running at " +DateTime.Now.ToString()); 
Console.WriteLine ("Sum of two numbers... 5+5 ="+proxy. Add (5,5));
Console.ReadLine ();
}
    }
}
Step 10: End point (same as service) information should be added to the configuration file of the client application.
<? Xml version="1.0" encoding="utf-8”?>
<Configuration>
  <system.serviceModel>
    <Client>
      <endpoint address ="http://localhost:8090/MyService/SimpleCalculator"  
Binding="wsHttpBinding" Contract ="MyCalculatorService.SimpleCalculator"> 
     </endpoint>
   </client>
</system.serviceModel>
</configuration>
Step 11: Before running the client application, you need to run the service. Output of the client application is shown below.
    This self host shows advantage such as in-Pro hosting, programmatic access and it can be used when there need singleton service. 

No comments:

Post a Comment