Saturday 10 August 2013

WCF - Host WCF service application.

In This article we are going to see hosting a WCF application in various ways.
1. Self Hosting
2. IIS

Self Hosting 
    After developing the WCF application, It should be hosted so now we are currently see how we are going to Host the application as Self Hosted.


  1. Open a Visual studio 2008 and above 2010.
  2. Create a console application .
  3. Add Reference "System.ServiceModel"


    class Program
    {
        static void Main(string[] args)
        {
           
            Uri httpUrl = new Uri("http://localhost:8090/Service/Calculator"); // Hosting Url
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService.Service1), 
                                                      httpUrl))
            {
                host.AddServiceEndpoint(typeof(CalculatorService.IService1), 
                       new   WSHttpBinding(), "");
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);

                host.Open();
                Console.WriteLine("Service is host");
                Console.WriteLine("Host is running... Press any key to stop");
                Console.ReadLine();
            }
         
        }
    }

Press F5 to host the WCF service..

Output



Above image shows that WCF service is self hosted.
Now launch the service by the following URL  http://localhost:8090/Service/Calculator





IIS HOSTED 
     Hosting the WCF service application in IIS , Before  hosting check the web.config file for the binding, end points,and behaviour information for example let we consider a sample of binding in Web.config

Web.Config

<system.serviceModel>
    <services>
      <service name="CalculatorService.Service1" behaviorConfiguration="CalculatorService.Service1Behavior">
          
        <endpoint address="" binding="basicHttpBinding" contract="CalculatorService.IService1">         
        </endpoint>                             
       
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>                         
        <behavior name="CalculatorService.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to               false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        
      </serviceBehaviors>
    </behaviors>   
  </system.serviceModel>

Hosting the WCF in IIS do the following steps 

1.   Type Inetmgr in run and press enter
2.   Right click on Sites and select a Add a WebSite 





3.   Give a name "Calculator" 
4.   Select AppPool as Framework 4.0
5.   In Binding select http type and port 70.
6.   Select Ok.





Now Launch the Service by following URL http://localhost:70/Service1.svc





From this article we can see how to host the WCF service in various ways.