Objective
This article is targeted to very new WCF developer. This will explain different Hosting options.
Hosting of WCF Service 

  1. WCF service cannot exist in Void.
  2. Every WCF service must be hosted in a Windows process called the Host process.

      


Service Explanation 

We are going to use WCF service listed below for all type of Hosting.. (This article is not subjected to explain, how to create a WCF service, so I am assuming that, you know how to create WCF service else read my other articles)

Contract

is as follows,

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace HotsingSamples

{

[ServiceContract]


public
interface
IService1

{

[OperationContract]


int AddofTowNumbers(int value1, int value2);

}

}

Service implantation

is as follows,

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace HotsingSamples

{


public
class
Service1 : IService1

{


public
int AddofTowNumbers(int value1, int value2)

{


int res;

res = value1 + value2;


return res;

}

}

}

Build the service. And now we will host the service in different manner.

IIS Hosting

  1. Main advantage of hosting a service in IIS is Host process is launched automatically upon the first client request.
  2. Disadvantage is that only HTTP protocol could be used.
  3. Another disadvantage is all the service will run on the same port.
  4. Here Host Process life cycle is managed by the IIS.

 Step 1

  1. Right click on service in solution explorer and select Publish.


 

  1. Click on browse button. See the button in red circle below. 


  2. Either you can use existing Virtual Directory or you could create a New Virtual Directory. To create a New Virtual Directory, first click on Default Web sites ( see below image , circle in Green) and then click on Create new virtual directory (see below Image circle in Red)

  3. Give any Alias name for the Virtual directory of your choice. USTTEMP is alias name here. After giving alias name browse to the folder where Service is residing. Here service is saved in E:\\WorkHere\WCF\HotsingSample folder.

     

  4. Click on Open

  5. Click on Publish to publish the service in IIS.

  6. After clicking on Publish, a message will get display at bottom of visual studio saying that successfully published. See the Image in Red circle below.

     

Step 2

  1. Click on Start->Run and type inetmgr. It will open the IIS window.

  2. In IIS, you could able to see TEMPUST virtual folder.

     

  3. Just click on that, in right hand pane you will see service listed in this Virtual directory. Right click on service (name of you service) and select browse

  4. In browser, you could see the service running.

  5. At client side to consume this service, provide this URL while adding service reference.

    http://localhost/USTTEMP/Service1.svc

    Points to be noted in URL

    1. It does not have any port number because it is running on port number 80.
    2. USTTEMP is name of the Virtual folder.
    3. Service1.svc is name of the Service.

Self Hosting

 

 

  1. This requires adding a managed code to host the process.
  2. The code could be either a Windows form application or Console application.
  3. Here host process must be running before client makes a call to the service.
  4. Mainly Console applications are used for the purpose of self hosting.
  5. The Console/Windows application must specifically create and open an instance of ServiceHost object.
  6. The ServiceHost then remains open and available until it is no longer needed.
  7. Configuration about the Service is being kept in the App.Config file. 

Step 1

Right click on Service Project and add a new project by selecting Console Application as Project type. Name of console application project is here Hosting. You are free to give any name.


Step 2

Right click on console application (Hosting) and add an App.Config file.




Step 3

Add reference of System.ServiceModel in Console application project.

Add reference of Service project to the console application project.

Step 4

Add below code in Main function

using (ServiceHost host = new
ServiceHost(typeof(Service1)))

{

host.Open();


Console.WriteLine(“Press <enter> to terminate the Application”);


Console.ReadKey(true);

}

  1. We are creating Instance of a Service Host.
  2. We are passing Type of Contract as argument of the constructor of the Service Host.
  3. Opening the Host.

Step 5 

Configuring the App.config file.

<?xml
version=1.0
encoding=utf-8 ?>

<configuration>

<system.serviceModel>

<services>

<service
name=HotsingSamples.Service1
behaviorConfiguration=HotsingSamples.Service1Behavior>

<!– Service Endpoints –>

<endpoint
address=“”
binding=basicHttpBinding
contract=HotsingSamples.IService1>

<!–

Upon deployment, the following identity element should be removed or replaced to reflect the

identity under which the deployed service runs. If removed, WCF will infer an appropriate identity

automatically.


–>

<!–<identity>

<dns value=”localhost”/>

</identity>–>

</endpoint>

<endpoint
address=mex
binding=mexHttpBinding
contract=IMetadataExchange/>

<host>

<baseAddresses>

<add
baseAddress =http://localhost:8585//>

</baseAddresses>

</host>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior
name=HotsingSamples.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>

</configuration>

  1. Adding the End Point
  2. Adding the base address through configuration.

 

Step 6

Run the console application to host the service.


Happy Coding


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s