Hosting options with WCF services

Ratings:
(4)
Views: 700
Banner-Img
Share this blog:

Hosting Services

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:
  1. Id shutdown
  2. Process beatth monitoring
  3. 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

Abort () Close () Open () 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

11Create a new console application with the name example 6 11Add a new class with the name IEMP tails.cs 11 change the class to interface in IE details.cs 11 Add reference to system. Service mode namespace Using system data; Using system. Service model; Namespace Example6 { [service contract] interface IEmp details { [operation contract] data table get deptdata(int D); } } 11create a new class file with the name cls empi .cs & write the following code. using system.data; using system.data.sqlclient; namespace example6 { 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(); (DS, “Emp”); return ds. Tables[0]; } } } 11Change the program.cs file name to cls sample.cs and write the following code. using system. ServiceModel; Namespace Example6 { Class cls sample { Static voidMain() { vri obju = new uri (“net.tcp://localhost:9100/”); service host objs = new serviceHost (type of (cls emp Details), obju); Objs.open(); c.WL (“service started and running”); C WL (“press any key to terminate-----“); Console. Readline(); Objs. Close(); } } } 11To mention the end point for the service add app.config file into the application. Steps:- 11Go to selection Explorer 11 Select the solution 11 click with right mouse button 11 click on add 11 click on new item 11 select the template application configuration file11click on add 11 write the following code. <xml version = “1.0” encoding = “utf – 8”?> <configuration> <system.servicemodel/> <services> <service name = “Example6.Cls Emp Details”> <endpoint address = “Example6. Cls Emp details” Binding = “net TCP Binding” Contract = “Example6. IEmp details”/> </service> </services> </system.ServiceModel> </configuration> 11Creating client component to consume the service costed using console application Client Design Screenshot_74   11Create a new windows ohms application 11 Design the form 11 Add reference the service hosted using consol applications. Steps to add reference 11Go to solution explorer 11 select the solution 11 click with right mouse button 11click on add service reference 11 type the “net TCP://local host:9100/” 11 click on go button 11we find the service class name “Cls empdetails” 11 change the reference to me to SRI 11 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. 11This client will work a long as the service component created at console application is running. If can closeness.

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox