.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> WCF >> Post New Resource Bookmark and Share

 Subscribe to Articles

How to Host WCF Services Tutorial

Posted By :Dhananjay Kumar      Posted Date :09/03/2010   Points :25   Category: WCF    URL: http://www.dhananjaykumar.net

How to Host WCF Services Tutorial. This Step by step Tutorial is targeted to very new WCF developer. This will explain different Hosting options for WCF Services.
 


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.

    hosting1.gif

    hosting2.gif

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:

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

hosting3.gif

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

hosting4.gif

c. 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)

hosting5.gif

d. 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.

hosting6.gif

e. Click on Open

hosting7.gif

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

hosting8.gif

g. 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.

hosting9.gif

Step 2:

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

hosting10.gif

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

hosting11.gif

c. 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

hosting12.gif

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

hosting13.gif

e. 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
hosting14.gif


  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.

hosting15.gif

Step 2:

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

hosting16.gif

hosting17.gif

hosting18.gif

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.

    EndPoint information could be added either through config file or through the managed code.

Step 6

Run the console application to host the service.

hosting20.gif 


Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend