To make any WCF service as active that service most be hosted with in a runtime environment which creates the service and controls the life time of the service
- WCF services are designed to run on any windows process that supports managed code.
- WCF services supports following types of hosting mechanisms / hosting options.
- Hosting using console Applications / managed applications
- Hosting within windows services
- Hosting with IIS
- Hosting with windows process activation service or activation domain.
Hosing with console applications / managed application
- WCF services can be hosted in any managed application
- This is most flexible option and required least infrastructure and resources.
- In this mechanism we embed the service with console applications and use the “service host” class to host our service
- The services hosted in console applications can be from windows forms applications or WPF application
- Hosting a WCF service with in the console application useful during the application development process
This makes easy to debug and trace the application
Hosting with windows services
- This hosting option requires register the application our WCF service within the windows service.
- Once our WCF service is registered at windows serve then it will be controlled by the service control mar of the windows services
- The service is implemented as both windows service and as WCF service by inheriting from both the i.e., service base of windows services as from contract interface of WCF service.
- Then the code is written appropriately in on start & on stop events of windows service.
- Then the service is installed with in the windows services of control panel using “install util exe”
- Once the service is placed in control panel service the lipetime of the service is controlled by the
- This hosting option is available in all versions windows.
Hosting with IIS
- Hosting with IIS option is integrated along with .net
- When WCF service is hosted with IIS it provide following facilities:
- Id shutdown
- Process beatth monitoring
- Message based activations
IIS hosted services will support only “ Http” transport and doesn’t support other transport like Tcp etc.
Hosting with windows process activation service
This is the new mechanism available with windows vista., windows7, windows 2008 server operation system.
This is not supported in windows Xp
Microsoft introduced this mechanism with IIS 6.0 version (with windows Xp we have IIS 5.0 version)
To work with first two hosting we should know about service host class and URI class.
These 2 classes are available in system service model namespace.
Service Host class
This class is used to set the required properties for a service, to host the service.
Methods with service host class
Add service end point (implemented contract (address) class, binding contract);
Abort():- This method is used to terminate the service
Close():- This method is used to close the service
Open():- This method is used to start a service.
Add service Endpoint, (, , ) :- This method is used to add the required end point to the service
Example to host the service using console application
Create a new console application with the name example 6 Add a new class with the name IEMP tails.cs change the class to interface in IE details.cs Add reference to system. Service mode namespace
Using system data;
Using system. Service model;
interface IEmp details
data table get deptdata(int D);
create a new class file with the name cls empi .cs & write the following code.
public class cls emp Details : IE mp Details
Public Data Table Get Deptdata (int D)
sqlconnection con=new sql connection (—-);
string S = “Select * from emp Details where Deptno=”+D;
sqldata Adpater da = new sqldataadapter (s.con);
dataset Ds=new Dataset();
return ds. Tables;
Change the program.cs file name to cls sample.cs and write the following code.
using system. ServiceModel;
Class cls sample
vri obju = new uri (“net.tcp://localhost:9100/”);
service host objs = new serviceHost (type of (cls emp Details), obju);
c.WL (“service started and running”);
C WL (“press any key to terminate—–“);
To mention the end point for the service add app.config file into the application.
Go to selection Explorer Select the solution click with right mouse button click on add click on new item select the template application configuration fileclick on add write the following code.
<xml version = “1.0” encoding = “utf – 8”?>
<service name = “Example6.Cls Emp Details”>
<endpoint address = “Example6. Cls Emp details” Binding = “net TCP Binding” Contract = “Example6. IEmp details”/>
Creating client component to consume the service costed using console application
Create a new windows ohms application Design the form Add reference the service hosted using consol applications.
Steps to add reference
Go to solution explorer select the solution click with right mouse button click on add service reference type the “net TCP://local host:9100/” click on go button
we find the service class name “Cls empdetails” change the reference to me to SRI click on Add, this will create proxy impotent at client side
(Note:– Before adding this reference, the service hosted in console application i.e., example 6 should be running otherwise. We can’t add the reference and can’t consume the service) à write the following code.
Private void btnsubmit click(—–)
SRI.Empdetails client obj1 = new sRI.Emp Details client();
DGV sample. Datasource = bj1. Getdeptdata (convert to Int32 (txtdeptno. Text));
àRun the application & click.
This client will work a long as the service component created at console application is running. If can closeness.