1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Hosting and Consuming WCF Service

Discussion in 'ASP.NET' started by MinalS, Sep 10, 2014.

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    30
    Trophy Points:
    0
    There are different ways for hosting a service depending on the accessibility of the service. The ways are as mentioned below:
    1. Self hosting a WCF Service
    2. Hosting a WCF service in Windows services
    3. Hosting a WCF service in IIS
    4. Hosting a WCF service in WAS

    1. Self hosting a WCF Service



    Self hosting a WCF service is to check the functionality of the service. It is flexible and easy to host the self service. It is effective for the user to develop and deploy the service. The trace information can be easily collected by the user.

    To self host the WCF service, user needs to create the .NET application that can be a console or Windows application. The following steps are performed by the user for self hosting the WCF service.

    1. Add the reference of the System.ServiceModel namespace in the application.
    Code:
    using System.ServiceModel;
    
    2. After adding the namespace, user needs to embed the code for the WCF service in the application. The code is as shown below:
    Code:
    namespace ConsoleApplication1
    {
    	[ServiceContract]
    	public interface ICar
    	{
    		[OperationContract]
    		string GetDetails ( string a );
    	}
    	[ServiceBehavior]
    	public class car: ICar
    	{
    		public string GetDetails ( string a)
    		{
    			return ( “Welcome User” + a);
    		}
    }
    class Program
    {
    	static void Main ( string[ ] args)
    	{
    		ServiceHost hostobj = new ServiceHost ( typeof ( car) );
    		BasicHttpBinding bindobj = new BasicHttpBinding ();
    		Hostobj.AddServiceEndpoint ( typeof ( ICar ), bindobj,
    		“http://localhost:7070/MyWCFService” );
    		hostobj.Open();
    		Console.WriteLine(“Service is Ready”);
    		Console.ReadLine();
    		Hostobj.Close();
    	}
    }
    
    In the above code, the object hostobj of the ServiceHost class is created. The object type is similar to the class1 type. The Binding object bindobj is created. The type of binding is specified by the binding object. The AddServiceEndPoint() method is used to specify the contract, binding object and the address.

    3. After the code is added in the WCF service application, the code for hosting the WCF service in the client application is as shown below:

    Code:
    namespace ConsoleApplication3
    {
    	[ServiceContract]
    	public interface ICar
    	{
    		[OperationContract]
    		string GetDetails ( string a);
    	}
    }
    class Program
    {
    	static void Main ( string [ ] args )
    	{
    		BasicHttpBinding bindobj = new BasicHttpBinding();
    		ChannelFactory<ICar> obj = new ChannelFactory <ICar> 
    		( bindobj, new EndPointAddress   (“http://localhost:7070/MyWCFService”));
    		ICar objc= obj.CreateChannel();
    		Console.WriteLine( “Enter the name”);
    		string msg = Console.ReadLine();
    		string rmsg = objc.GetDetails(msg);
    		Console.WriteLine (rmsg);
    	}
    }
    
    In the above code, the hostobj of the ServiceHost class is created. The type of the object is same as the Class1 type. The bindobj object is the binding object. It is used to specify the binding type. The EndPointAddress() method is used to call the method.

    The ChannelFactory class is used to send the messages to the service. The CreateChannel() method is used to initialize the service with reference to the proxy. The method is called and the result is returned.

    The output displayed when the server is executed is as shown below:

    [​IMG]

    The output when the client is executed is as shown below:

    [​IMG]

    Advantages of Self - Hosting a WCF Service

    1. Easy to Debug:It is easy to debug the service as it is in familiar debugging environment.
    2. Flexibility: The WCF service provides flexibility of starting and stopping the service through the Open() and Close() methods.
    3. Easy to Deploy: IT is easy to deploy a self hosted WCF service as user needs to copy the application on the computer on which it is to be deployed.
    4. Supports all Bindings and Transports: It supports all the bindings and transports provided by the .NET Framework.
    Limitations of Self - Hosting a WCF service
    1. Manageability difficulty: It is difficult to manage the self hosted service because it does not provide the built in features.
    2. Limited Features: It has a limited support for high availability, easy manageability, robustness and versioning. The application is only accessible to the computer on which the application is present.

    2. Hosting a WCF service in Windows services



    There is a requirement that the WCF service installed on the computer should start automatically when the computer starts and stop when the system shuts down. To control the execution of the service, user needs to host the service in the Windows Service.

    A Windows Service is as service that is installed on the computer and is managed by the operating system. The service is inherited from the ServiceBase class.

    1. To host the WCF service in a Windows Service, user needs to embedded the service in the Windows Service. The system.ServiceModel namespace is used added. The code snippet is as shown below:

    Code:
    using System.ServiceModel;
    namespace WindowsService
    {
    	[ServiceContract]
    	public interface IMyInterface
    	{
    		[OperationContract]
    		string Display();
    		
    	}
    public partial class Service1: ServiceBase
    {
    	ServiceHost host;
    	public Service1()
    	{
    		InitializeComponent();
    	}
    	protected override void OnStart ( string[ ] args)
    	{
    		Type serviceType = typeof( MyWCFService);
    		host = new ServiceHost ( servicetype);
    		host.Open();
    	}
       	protected override void OnStop( )
    	{
    		if ( host != null)
    		host.Close();
    	}
    	public class MyWCFService : IMyInterface
    	{
    		public string Display()
    		{
    			return ( “Hello User” );
    		}
    	}
    }
    
    In the above code, the object host of the ServiceHost class is created. The OnStart() method is called to manually start the service. The OnStop() method is used to manually stop the service.

    2. Select ‘Project’ , ‘Add New Item’ option from the list. Click and select the ‘Application Configuration File’ from the list. Add the file in the application.

    3 Add the following code in the application configuration file as shown below:

    Code:
    
    <? xml version=”1.0” encoding=utf-8” ?>
    <configuration>
    	<system.ServiceModel>
    		<services>
    		<service name=”service2” behaviorConfiguration=”ServiceBehaviour” >
    		<host>
    		<baseAddress>
             <add baseAddress=”htp://localhost:8020/MyWCFService” /> 
    </baseAddress>
    </host>
    <endpoint address=”htp://localhost:8020/MyWCFService” 
    binding=”basichttpbinding” contract=”Service2” bindingConfiguration=”NoSecurity”></endpoint>
    </service>
    </services>
    <bindings>
    	<basicHttpBinding>
    	<binding name=”NoSecurity” >
    	<security mode=”None” >
    	</security>
    	</binding>
    	</basicHttpBinding>
    </bindings>
    <behaviors>
    	<serviceBehaviors>
    	<behavior name=”ServiceBehavior” >
    	<serviceMetadata httpGetEnabled=”true” />
    	<serviceDebug includeExceptionDetialInFaults=”true” />
    	</behavior>
    	</serviceBehaviors>
    </behaviors>
    	</system.ServiceModel>
    </configuration>
    
    4. Click the design view of the service class.
    5. Right click on the design view and select ‘Add Installer’ option. The Installer file appears as shown below:

    [​IMG]

    6. Right click on the serviceInstaller1 option and select Properties option.
    7. Change the name of the ServiceName property. Set the property type to Manual.
    8. Right click on the serviceProcessInstaller1 option. Select the Properties option and change the Account property to the LocalSystem.
    9. Save and compile the Windows Service.
    10. Open the Command Prompt for the Visual Studio application. Open it as Administrator.
    11. Type the following command in the command prompt.

    Code:
    installutil bin\Service1.exe 
    
    12. The service is executed. Click ‘Start’, Select ‘Run’ option.
    13. Type services.msc command and click Open
    14. The services window will appear as shown below:

    [​IMG]

    To consume the WCF service, user needs to add the reference of the service to the client application. The base address of the service containing the protocol used by the service, the port number and the name of the service is added. The proxy object of the service is created

    Advantages of Hosting a WCF service in Windows Services
    1. Recovery: The SCM tool is used to recover the Windows Service whenever the system failure occurs. It has a built in support to start the service when the failure occurs.
    2. Automatic starting: The StartUp type of the service can be setup to automatic by using SCM or setting the StartType property of the serviceInstaller to automatic. The service executes automatically as soon as it is hosted.
    3. Support for bindings and transports: User can use all the bindings and transports provided by the .NET framework.
    4. Security identity: The SCM tool allows user to implement the security of the service hosted in a Windows service. The LogOn property of the service is used.
    Limitations of Hosting a WCF service in Windows Services
    1. Deployment: In order to deploy a WCF service, user needs to create an installer to deploy the service on the computer.
    2. Limited Features: It has limited support for easy manageability and versioning.

    3. Hosting a WCF service in IIS


    Sometimes the client needs to access the service through the Intranet. The Web server known as IIS is used for hosting the service. The service hosted on the IIS communicates with client application through the HTTP protocol.

    The steps for adding a service on the IIS Web server are as follows:

    1. Click Start menu and type inetmgr command in the search box.

    [​IMG]

    2. The IIS server appears in the control panel as shown below:

    [​IMG]

    3. Double click on the Internet Information Services and the window will appear.

    [​IMG]

    4. Expand the Default Web Site node and right click on it. Click New , Virtual directory option.

    5. In the Add Virtual Directory box, add the Alias name as IISHostService and enter the physical path of the service. Click OK.

    [​IMG]

    6. The IISHostService contains the Service added in it. Add the physical path in the textbox. Click OK

    7. The IISHostService appears in the left pane with the fields added in it.

    [​IMG]

    8. Close the Internet Information Services window.

    9. Open the Visual Studio 2010 application. Click File, select New Website option

    10. Select the website and click Add Service Reference option

    [​IMG]

    11. Type the following in the address combo box and click OK.

    Code:
    http://localhost/IISHostService/Service.svc
    [​IMG]

    12. User can now view the details of the service in it.

    Advantages of Hosting a WCF service in IIS

    The advantages are as follows:
    1. Process recycling: The IIS server restarts the process to check that any fault code does not affect the system where the service is hosted.
    2. Automatic activation: The automatic activation means the service is activated when the client access the service and consumes it. They service may not be running before the client accesses the service.

    4. Hosting a WCF service in WAS



    To enable the service to communicate through different protocols, user needs to host the service in Windows Activation Services (WAS). It is an operating system embedded in the IIS 7.0 which is built up for the Windows Vista. It allows the service to use both the HTTP and non – HTTP protocols in the service.

    To consume the service, user needs to add the reference of the service to the client application. The endpoint address containing the port number, protocol to be used, location of the service, and the name of the service is added to the application.
     

Share This Page