Wednesday, 3 June 2015

Different Option of Hosting WCF service


A WCF service to be available for the clients to consume, we need to host the WCF service. followings are the different WCF service hosting options.

Self Hosting : A WCF service can be self hosted using a console application or a WinForm application.

Windows Service : Hosting using a windows service.

Host using IIS : Hosting within IIS supports only HTTP bindings.Non Http bindings are not supported.

Windows Activation Services : Hosting a WCF service using IIS7 with WAS supports all bindings including non-http bindings such as TCP, MSMQ etc.


Self Hosting


Hosting a WCF service in any managed .net application is called self hosting.
Console applications, WPF applications, WinForm applications are all example of managed .net applications.

Advantages:

  1. Very easy to setup. Specify the configuration in app.config file and with a few lines of code we have the service up and running.
  2. Easy to debug as we don't have to attach a separate process that hosts the wcf service.
  3. Support all bindings and transport protocols.
  4. Very flexible to control the lifetime of the service though the open() and close() methods of ServiceHost.

Note : Used only for development and demonstration phase not for hosting live service.

Code Example for hosting WCF service on console application :

Note : Hosting wcf service using windows form application is similar as hosting using console application.
One more advantage of using windows form hosting is having user interface to manage service stop and start.

Step 1 : Create New Service :

 public class HelloService : IHelloService
    {

        public string HelloMessage(string Name)
        {
            return "Hello : " + Name;
        }
}



 [ServiceContract]
    public interface IHelloService
    {

        [OperationContract]
        string HelloMessage(string Name);
}


Step 2 : Add New Console Application Project : 

Add new Console application
Add reference of HelloService project in it.

class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(WcfService1.HelloService)))
            {
                host.Open();
                Console.WriteLine("host open");
                Console.Read();
            }
        }
    }


Add App.config file in console application.
Add service related configuration in app config.


<system.serviceModel>
  <bindings></bindings>
  <services>
    <service name="WcfService1.HelloService" behaviorConfiguration="mexBehaviour">
      <endpoint address="HelloService" binding="basicHttpBinding" contract="WcfService1.IHelloService"></endpoint>  
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8090/"/>
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="mexBehaviour">
        <serviceMetadata httpGetEnabled="true"/>
      </behavior>  
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>


As we run console application service will be hosted with address mention in base address tag in above config xml. We can use this address to access this service.

On further post I will discuss other hosting methods in detail.Keep reading!!!

Related Post :

Different-option-of-hosting-wcf-service
Binding-in-wcf-choosing-right-wcf
Exchanging-metadata-in-wcf
Some-interesting-facts-about-data-contract
Knowntype-attribute-in-wcf
Associating-knowntype-in-wcf
Message-contract-in-wcf
Exception-handling-in-wcf
Exception Handling in WCF - SOAP Fault in WCF
Exception Handling in WCF - Unhandled Exception in WCF

Exception Handling in WCF - Creating and Throwing Strongly Typed SOAP Fault


No comments:

Post a Comment