WCF Part I.doc

Embed Size (px)

Citation preview

  • 8/10/2019 WCF Part I.doc

    1/23

    Windows Communication Foundation FAQ quick starter - Part 1

    Introduction

    What is .NET 3.0?

    What is Windows Card Space?

    What is WCF?

    What are the important principles of SO !Ser"ice oriented rchitecture#?

    What are ends$ contract$ address$ and %indin&s?

    Which specifications does WCF follow?

    What are the main components of WCF?

    E'plain how Ends$ Contract$ ddress$ and (indin&s are done in WCF?

    what is a ser"ice class?

    what is a ser"ice contract$ operation contract and )ata Contract?

    what are the "arious wa*s of hostin& a WCF ser"ice?

    +ow do we host a WCF ser"ice in IIS?

    what are the ad"anta&es of hostin& WCF Ser"ices in IIS as compared to self,hostin&?

    what are the ma-or differences %etween ser"ices and We% ser"ices?

    What is the difference WCF and We% ser"ices?

    What are different %indin&s supported %* WCF?

    Which are the "arious pro&rammin& approaches for WCF?

    What is one,wa* operation?

    Introduction

    In this section we will run throu&h a uic/ F for WCF. I am sure after readin& this*ou will &et a &ood understandin& of the fundamentals of WCF.In case *ou li/e the article or *our thin/ I need impro"ements please send me amessa&e at http122www. uestpond.com . I am sure e"er* one needs impro"ements.En-o*

    Clic/ here to see Windows Communication Framewor/ !WCF# , 4art 5

    http://www.codeproject.com/KB/aspnet/WCF.aspx#Introduction%23Introductionhttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhatisNET3%23WhatisNET3http://www.codeproject.com/KB/aspnet/WCF.aspx#WhatisWindowsCardSpace%23WhatisWindowsCardSpacehttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhatisWCF%23WhatisWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhataretheimportantprinciplesofSOAServiceorientedArchitecture%23WhataretheimportantprinciplesofSOAServiceorientedArchitecturehttp://www.codeproject.com/KB/aspnet/WCF.aspx#Whatareendscontractaddressandbindings%23Whatareendscontractaddressandbindingshttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhichspecificationsdoesWCFfollow%23WhichspecificationsdoesWCFfollowhttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhatarethemaincomponentsofWCF%23WhatarethemaincomponentsofWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#ExplainhowEndsContractAddressandBindingsaredoneinWCF%23ExplainhowEndsContractAddressandBindingsaredoneinWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#whatisaserviceclass%23whatisaserviceclasshttp://www.codeproject.com/KB/aspnet/WCF.aspx#whatisaservicecontractoperationcontractandDataContract%23whatisaservicecontractoperationcontractandDataContracthttp://www.codeproject.com/KB/aspnet/WCF.aspx#whatarethevariouswaysofhostingaWCFservice%23whatarethevariouswaysofhostingaWCFservicehttp://www.codeproject.com/KB/aspnet/WCF.aspx#HowdowehostaWCFserviceinIIS%23HowdowehostaWCFserviceinIIShttp://www.codeproject.com/KB/aspnet/WCF.aspx#whataretheadvantagesofhostingWCFServicesinIISascomparedtoselfhosting%23whataretheadvantagesofhostingWCFServicesinIISascomparedtoselfhostinghttp://www.codeproject.com/KB/aspnet/WCF.aspx#whatarethemajordifferencesbetweenservicesandWebservices%23whatarethemajordifferencesbetweenservicesandWebserviceshttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhatisthedifferenceWCFandWebservices%23WhatisthedifferenceWCFandWebserviceshttp://www.codeproject.com/KB/aspnet/WCF.aspx#Whataredifferentbindingssupported%20byWCF%23Whataredifferentbindingssupported%20byWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhicharethevariousprogrammingapproachesforWCF%23WhicharethevariousprogrammingapproachesforWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#Whatisonewayoperation%23Whatisonewayoperationhttp://www.questpond.com/http://www.codeproject.com/KB/aspnet/WCFPart2.aspxhttp://www.codeproject.com/KB/aspnet/WCF.aspx#Introduction%23Introductionhttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhatisNET3%23WhatisNET3http://www.codeproject.com/KB/aspnet/WCF.aspx#WhatisWindowsCardSpace%23WhatisWindowsCardSpacehttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhatisWCF%23WhatisWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhataretheimportantprinciplesofSOAServiceorientedArchitecture%23WhataretheimportantprinciplesofSOAServiceorientedArchitecturehttp://www.codeproject.com/KB/aspnet/WCF.aspx#Whatareendscontractaddressandbindings%23Whatareendscontractaddressandbindingshttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhichspecificationsdoesWCFfollow%23WhichspecificationsdoesWCFfollowhttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhatarethemaincomponentsofWCF%23WhatarethemaincomponentsofWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#ExplainhowEndsContractAddressandBindingsaredoneinWCF%23ExplainhowEndsContractAddressandBindingsaredoneinWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#whatisaserviceclass%23whatisaserviceclasshttp://www.codeproject.com/KB/aspnet/WCF.aspx#whatisaservicecontractoperationcontractandDataContract%23whatisaservicecontractoperationcontractandDataContracthttp://www.codeproject.com/KB/aspnet/WCF.aspx#whatarethevariouswaysofhostingaWCFservice%23whatarethevariouswaysofhostingaWCFservicehttp://www.codeproject.com/KB/aspnet/WCF.aspx#HowdowehostaWCFserviceinIIS%23HowdowehostaWCFserviceinIIShttp://www.codeproject.com/KB/aspnet/WCF.aspx#whataretheadvantagesofhostingWCFServicesinIISascomparedtoselfhosting%23whataretheadvantagesofhostingWCFServicesinIISascomparedtoselfhostinghttp://www.codeproject.com/KB/aspnet/WCF.aspx#whatarethemajordifferencesbetweenservicesandWebservices%23whatarethemajordifferencesbetweenservicesandWebserviceshttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhatisthedifferenceWCFandWebservices%23WhatisthedifferenceWCFandWebserviceshttp://www.codeproject.com/KB/aspnet/WCF.aspx#Whataredifferentbindingssupported%20byWCF%23Whataredifferentbindingssupported%20byWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#WhicharethevariousprogrammingapproachesforWCF%23WhicharethevariousprogrammingapproachesforWCFhttp://www.codeproject.com/KB/aspnet/WCF.aspx#Whatisonewayoperation%23Whatisonewayoperationhttp://www.questpond.com/http://www.codeproject.com/KB/aspnet/WCFPart2.aspx
  • 8/10/2019 WCF Part I.doc

    2/23

  • 8/10/2019 WCF Part I.doc

    3/23

    Fi$ure% 1 - WCF Com#onents

    What are the im#ortant #rinci#&es o' "(A )"er*ice orientedArchitecture+!

    WCF is %ased on SO . ll %i& companies are pla*in& %i& %ets on SO . So how can;icrosoft remain %ehind? So in order to implement SO architecture easil* *ou need

    to use WCF.SO is %ased on four important concepts1,

    , oundaries are we&& de'ined

    In SO $ e"er*thin& is formali ed. The client who is consumin& the ser"ice does notneed to /now how the implementation of the ser"ice is done. If *ou loo/ at some oldmethodolo&ies of communication li/e )CO;. n* chan&es at ser"er le"el the clientalso has to chan&e. Therefore$ the ser"er and client implementation was so much%ound that chan&es need to %e done at all places. In SO $ the rule is if *ou doenhancement *ou do not need to chan&e an*thin& at the client. SO %asedapplication onl* understands that there is an end point$ contract$ and %indin&s.

    Note: - Just to clarify shortly about end point and contract. Any SOA serviceis exposed through an end point. End point defines three important aspects

    hat! here and "o#. e #ill understand more details of the same in thelater $uestions.

  • 8/10/2019 WCF Part I.doc

    4/23

    , "er*ices e*o&*e

    Chan&e is the law of nature and ser"ices will e"ol"e. In SO $ ser"ices can %e"ersioned and *ou can host those ser"ices in new ends. For instance$ *ou ha"e a

    ser"ice called as =Search Tic/ets !Tic/et Num%er# =which &i"es details %ased onTic/et Num%er and its e'posed on end point =ep8@. Tomorrow *ou want ma/e *ourSearch Tic/ets ser"ice more useful %* also pro"idin& an e'tra option of allowin& himto search %* passen&er name. Therefore$ *ou -ust declare a new end =ep5@ withser"ice =Search Tic/ets !Tic/et Num%er$ 4assen&er Name#@. So the client who isconsumin& the ser"ice at end ep8 continues and at the other end$ we ha"e e"ol"edour ser"ice %* addin& new ends ep5.

    , "er*ices share on& schemas and contracts

    Ser"ices use Schemas to represent data and contracts to understand %eha"ior. The*do not use lan&ua&e dependent t*pes or classes in order to understand data and%eha"ior. A;B is used to define schemas and contracts. )ue to this$ there is nothea"* couplin& %etween en"ironments.

    , "er*ice com#ati/i&it is #o&ic /ased

    4olic* descri%es the capa%ilities of the s*stem. )ependin& on policies$ the ser"ice cande&rade to match the ser"ice for the client. For instance *our ser"ice needs to %ehosted for two t*pes of client one which uses emotin& as the communicationmethodolo&* while other client uses )CO;. n ideal SO ser"ice can cater to %oth of them accordin& to there communication policies.

    Note: - %any people assume eb services are the base for SOA. &he ans#er is '( ) right. hat #eb services lac* is the policy based Servicecompatibility. +f you host a #eb service it can only serve #ith "&&,communication channel and SOA, message. Any other type of client tryingto communicate he #ill not degrade it self. &his is #hat is provided by ./ou can host the service in one or more mode. or instance you can host a

    service using remoting and AS%0.

    What are ends0 contract0 address0 and /indin$s!

    The a%o"e terminolo&ies are the core on which SO stands. E"er* ser"ice muste'pose one or more ends %* which the ser"ice can %e a"aila%le to the client. Endconsists of three important thin&s where$ what and how1,

  • 8/10/2019 WCF Part I.doc

    5/23

    , Contract )What+

    Contract is an a&reement %etween two or more parties. It defines the protocol howclient should communicate with *our ser"ice. Technicall*$ it descri%es parameters andreturn "alues for a method.

    , Address )Where+

    n ddress indicates where we can find this ser"ice. ddress is a D B$ which pointsto the location of the ser"ice.

    , indin$ ) ow+

    (indin&s determine how this end can %e accessed. It determines howcommunications is done. For instance$ *ou e'pose *our ser"ice$ which can %eaccessed usin& SO 4 o"er +TT4 or (IN o"er TC4. So for each of thesecommunications medium two %indin&s will %e created.(elow fi&ure$ show the three main components of end. ou can see the stoc/ tic/eris the ser"ice class$ which has an end hosted on www.soa.com with +TT4 and TC4%indin& support and usin& Stoc/ Tic/er interface t*pe.

    Fi$ure 2% - End#oint Architecture

    Note: - /ou can also remember the end point by A1 #here A stands for Address! 1 for bindings and for ontract.

  • 8/10/2019 WCF Part I.doc

    6/23

    Which s#eci'ications does WCF 'o&&ow!

    WCF supports specifications defined %* WS, specifications. WS, specifications aredefined to&ether %* ;icrosoft$ I(;$ SDN and man* other %i& companies so that the*can e'pose there ser"ice throu&h a common protocol. WCF supports all specificationsdefined we will understand them one %* one.

    , essa$in$ )W"-Addressin$+%- SO 4 is the fundamental protocol for we%ser"ices. WS ddressin& defines some e'tra additions to SO 4 headers$ which ma/esSO 4 free from underl*in& transport protocol. One of the &ood thin&s a%out ;essa&etransmission is ;TO;$ also termed as ;essa&e Transmission Optimi ation;echanism. The* optimi e transmission format for SO 4 messa&es in A;B,(inar*formant usin& A;B optimi ed pac/a&in& !AO4#. (ecause the data will sent in %inar*and optimi ed format$ it will &i"e us hu&e performance &ain.

    , "ecurit )W"-"ecurit 0 W"-Trust0 and W"-"ecure Con*ersation+%- ll thethree WS, define authentication$ securit*$ data inte&rit* and

    pri"ac* features for a ser"ice.> elia%ilit* !WS, elia%le ;essa&in, This specification ensures end,to,endcommunication when we want SO 4 messa&es to %e tra"ersed %ac/ and forth man*times.

    , Transactions )W"-Coordination and W"-Atomic Transaction+%- These twospecifications ena%le transaction with SO 4 messa&es.

    , etadata )W"-Po&ic and W"- etadata e4chan$e+%- WS)B is aimplementation of WS,;etadata E'chan&e protocol. WS,4olic* defines more d*namicfeatures of a ser"ice$ which cannot %e e'pressed %* WS)B.We ha"e stressed on the WS, specification as it is a specification which a ser"icehas to follow to %e compati%le with other lan&ua&es. (ecause WCF follows WS,

    specifications other lan&ua&es li/e G H $ C:: can also e'ploit features li/e;essa&in& $ Securit* $ elia%ilit* and transactions written in C or H(.NET. This is the%i&&est achie"ement of WCF to inte&rate the a%o"e features with other lan&ua&es.

    Note: - 2uring intervie# the intervie#er expects that you *no# #hat S-3 specification are supported by and its advantages #ith respect tointeracting #ith other languages.

    What are the main com#onents o' WCF!

    We need to define three main components in WCF1,> Ser"ice class.> +ostin& en"ironment> End point

    E4#&ain how Ends0 Contract0 Address0 and indin$s are done in WCF!

  • 8/10/2019 WCF Part I.doc

    7/23

    what is a ser*ice c&ass!

    what is a ser*ice contract0 o#eration contract and 5ata

    Contract!

    In this e'ample$ we will ma/e simple ser"ice$ which displa*s the total cost of thecomplete product &roup. In simple words$ this ser"ice will ta/e three parameters perproduct cost$ num%er of products and the product name. In return the ser"ice willreturn the total cost of all the products %* multipl*in& num%er of products cost perproduct. s we &o ahead in this e'planation$ we will tr* to understand all theterminolo&ies$ which are as/ed in the a%o"e uestion.First$ *ou need to create a Winf' ser"ice pro-ect. ou can see in the %elow fi&ure weha"e selected the Winf' pro-ect.

  • 8/10/2019 WCF Part I.doc

    8/23

    Fi$ure 3% - Create new WinF6 "er*ice c&ass

    In this pro-ect$ we add a new class and name it as =ser"iceJetCost.cs@. This class willha"e our core implementation and this is the class$ which has all the action. Theser"ice class$ which has to %e e'posed to the e'ternal client. We need to use the

    Ser"ice Contract attri%ute to mar/ it as a ser"ice class.Ser"ice Contract attri%ute define sa*in& which application interface will %e e'posedas a ser"ice.

    ou can see in the %elow code snippet we ha"e made an interface and mar/ed it asSer"ice Contract. It is not essential that *ou need to use an interface *ou can alsouse a simple class and mar/ it as Ser"ice %ut interface represent a contract and donot ha"e implementation. In short$ the* stand at a "er* hi&her le"el of a%straction.So as a &ood desi&n practice,usin& interface to represent a ser"ice contract ma/esmore sense.The ne't thin& to note is the Operation Contract attri%ute.Operation Contract dictates which methods should %e e'posed to the e'ternal clientusin& this ser"ice.It defines indi"idual e'chan&e or re uest and replies. In the current sample$ we ha"edefined JetTotalCost method$ which will %e used %* the end client to &et the totalcost results.The ne't thin& to note in the code snippet is the )ata Contract attri%ute. In thepre"ious two steps$ we ha"e e'posed class as a ser"ice %* usin& Ser"ice Contractand methods %* usin& Operation Contract. E"er* operation will definitel* do some/ind of data transfer.)ata Contract attri%utes defines which t*pe of comple' data will %e e'chan&ed%etween the client and the ser"ice. The* determine which parameters to %eseriali ed.When *ou are usin& simple data t*pes li/e int$ %olo etc it is not necessar* that *ouneed to mar/ the data contract attri%ute. (ecause *ou will alwa*s find matchin&t*pes on the client. +owe"er$ comple' structure li/e one shown in the %elow code

    snippet *ou will need to define a data contract. emem%er data contract define howthis data will %e passed durin& transmission. In short data contract attri%ute definehow data will %e seriali ed will transmission.In the %elow sample we ha"e mar/ed the structure product data to %e seriali ed.

  • 8/10/2019 WCF Part I.doc

    9/23

    Fi$ure 7%- The "er*ice c&ass

    s data contract are all a%out seriali ation *ou need to importS*stem. untime.Seriali ation name space.In the ne't step$ we implement the JetTotalCost function. It -ust returns a simplestrin& with product name and the total cost of all products.Once our ser"ice class is done its time to host this ser"ice. There are "arious wa*s of hostin& a WCF ser"ice we will loo/ in to the same in the ne't uestion. For thecurrent e'ample$ we will host in their own process.

  • 8/10/2019 WCF Part I.doc

    10/23

    Fi$ure 8% - ostin$ the ser*ice

    +ostin& the WCF ser"ice needs two thin&s one is the confi& file and second is thehostin& code on startup. (ecause we are hostin& this ser"ice in its own applicationprocess this needs to %e a windows application. So first let us ha"e a loo/ whatentries do$ we need to ma/e in the pp.confi& file. In the a%o"e fi&ure$ e"er*thin& isclear %ut let us understands all the section defined in the pp.confi& file.In the confi&uration section$ we need to add a new section Ks*stem.ser"ice;odelL.The most important part of Ks*stem.ser"ice;odelL is the endpoint ta&. s said inthe pre"ious answer End &i"es three important answers Where$ What and +ow. Inshort where is the ser"ice$ what the contract of the ser"ice is and how do wecommunicate with the ser"ice.In the a%o"e code snippet$ we ha"e onl* defined the contract i.e. what and how thatis %indin&s. The where is defined in the application entr* point static "oid main !#.Therefore$ the contract attri%ute defines the interface and %indin& sa*s that the endclients can communicate usin& =+TT4@ protocol.In Static "oid ;ain method$ we create an o%-ect of Ser"ice +ost class and use theopen method to host the ser"ice. We ha"e used the D I o%-ect to define the addresswhere the ser"ice will %e hosted.

  • 8/10/2019 WCF Part I.doc

    11/23

    Fi$ure 9% - "er*ice "tarted

    If *ou compile the pro-ect$ *ou will see somethin& as shown in the a%o"e fi&ure. Thissa*s that the ser"ice is up and runnin& and read* to ser"e an* WCF client. Now itstime to de"elop consumer$ which will consume this WCF ser"ice. ;icrosoft haspro"ided a decent automation to &enerate the client. Therefore$ %elow fi&ure depictsthe "arious steps.

    Fi$ure :% - s*cuti& in action

    Jo to command prompt of windows S)M and run the followin& command1,S"cutil KSer"ice hosted D ILIn the a%o"e command KSer"ice +osted D IL is the D I on which the ser"ice is

  • 8/10/2019 WCF Part I.doc

    12/23

    hosted. One *ou run the command a&ainst the D I it will &enerate two files one isthe confi& file and the other is the pro'*. ou can see in the a%o"e fi&ure two filesare &enerated ser"iceJetCost.cs and output.confi& file. With the help of these twofiles$ we will ma/e our client.

    Fi$ure ;% - C&ient code wa&kthrou$h

    ou can see in the a%o"e fi&ure we ha"e made WFCClientJetCost pro-ect. In that$ weha"e added output.confi& and ser"iceJetCost.cs to the client pro-ect. We ha"erenamed output.confi& to app.confi&.Once we ha"e done with e"er*thin&$ its time to write the client code$ which calls thepro'* who in turn will call the ser"ice hosted. In the a%o"e fi&ure$ *ou can see weha"e the client code also. It is a simple code we first created the o%-ect of the datastructure set the "alues. Then we create the o%-ect of the ser"ice and call the

    JetTotalCost function.If e"er*thin& is compiled and *ou run the ser"er and client$ *ou should &et *ouroutput as shown %elow.

  • 8/10/2019 WCF Part I.doc

    13/23

    Fi$ure +ost in application domain or process pro"ided %* IIS Ser"er.> +ost in pplication domain and process pro"ided %* W S !Windows cti"ationSer"ice# Ser"er.

    ow do we host a WCF ser*ice in II"!

    Note: - &he best to *no# ho# to host a in ++S is by doing a small sample. So #hat #e #ill do is host the same 4et ost sample #hich #as selfhosted in the previous $uestion.

    First thin& *ou will need is to create the SHC file$ which e'poses the ser"ice class.SHC file contains the pointer to the class. ou can see from the fi&ure %elow the classattri%ute points to the class whose interface is e'posed %* the ser"ice.s"c.cs file.

    lso$ note the actual interface is in ser"ice.s"c.cs file. (elow fi&ure$ ha"e %oth thefiles ser"ice.s"c$ which has the class attri%ute which points to the ser"ice class$ andthe interface$ which resides in ser"ice.s"c.cs file. We ha"e ta/en the same sample$which was self,hosted in the pre"ious uestion.

  • 8/10/2019 WCF Part I.doc

    14/23

    Fi$ure 1 % - The "=C 'i&e and the /ehind code

    We also need to pro"ide implementation for the interface. So we ha"e made a classSer"iceJetCost which has the actual implementation. (elow fi&ure shows the samein detail. In the %elow fi&ure *ou can also see the solution files.

  • 8/10/2019 WCF Part I.doc

    15/23

    Fi$ure 11% - Im#&ementation o' "er*ice.s*c.cs

    We also need to specif* the ser"ice t*pe and endpoint in we%.confi& file. lso$ note

    we ha"e specified +TT4 %indin& %ecause we will %e hostin& the ser"ice on IIS.

  • 8/10/2019 WCF Part I.doc

    16/23

    Fi$ure 12% - We/.con'i$ 'i&e 'or hostin$ ser*ice on II"

    Now that we are done with the codin& part. We need to create the "irtual director* inIIS. In the %elow fi&ure in Step8 and Step5 we ha"e shown how to create the "irtualdirector* in IIS. One important thin& to note while creatin& "irtual director* set theaccess permission to e'ecute.

  • 8/10/2019 WCF Part I.doc

    17/23

    Fi$ure 13%- II" Con'i$uration

    In the third step$ we will pu%lish the we%site to our "irtual director*. Note the fourthstep in which we ha"e copied the s"c file so that the ser"ice can %e re uested.Note1 , S4.NET compilation has chan&ed in S4.NET 5.0. In 5.0 there is no conceptof solution files. So if *ou want to ha"e full compiled )BB *ou need to pu%lish thepro-ect to a "irtual director*.Once *ou ha"e hosted the SHC file *ou can test the same %* re uest the ser"ice.s"cfile. If e"er*thin& wor/s fine *ou will &et somethin& as shown in the %elow fi&ure

  • 8/10/2019 WCF Part I.doc

    18/23

    Fi$ure 17%- II" WCF c&ient

    Dsin& the S"cutil.e'e$ *ou will need to &enerate the pro'* class and the confi& file.The pro'* and confi& will %e same$ as we had done for self,hostin&. The oneimportant chan&e is the address. The confi& file D B now points to the ser"ice.s"c$which is hosted on IIS. ou can run the same client$ which we had created for self,hostin&. The onl* chan&e *ou will need to do is chan&e the endpoint address.

    Fi$ure 18%- (ut#ut o' WCF c&ient at II"

    BOB ou should &et the same output$ which we had recei"ed$ for self,hostin&.

    what are the ad*anta$es o' hostin$ WCF "er*ices in II" as com#aredto se&'-hostin$!

  • 8/10/2019 WCF Part I.doc

    19/23

    There are two main ad"anta&es of usin& IIS o"er self,hostin&1,

    Automatic acti*ation

    IIS pro"ides automatic acti"ation that means the ser"ice is not necessar* to %erunnin& in ad"ance. When an* messa&e is recei"ed %* the ser"ice it then launchesand fulfills the re uest. (ut in case of self hostin& the ser"ice should alwa*s %erunnin&.

    Process rec c&in$

    If IIS finds that a ser"ice is not health* that means if it has memor* lea/s etc$ IISrec*cles the process. O/ let us tr* to understand what is rec*clin& in IIS process. For

    e"er* %rowser instance$ a wor/er process is spawned and the re uest is ser"iced.When the %rowser disconnects the wor/er$ process stops and *ou loose allinformation. IIS also restarts the wor/er process. (* default$ the wor/er process isrec*cled at around 850 minutes. So wh* does IIS rec*cle. (* restartin& the wor/erprocess it ensures an* %ad code or memor* lea/ do not cause issue to the wholes*stem.In case of self,hostin& %oth the a%o"e features$ *ou will need to code *ourself. Bot ofwor/ ri&ht . That is wh* IIS is the %est option for hostin& ser"ices until *ou arereall* doin& somethin& custom.(elow fi&ure shows where the rec*cle option is located in IIS. ou need to clic/ onthe )efault ppool and then 4roperties.

  • 8/10/2019 WCF Part I.doc

    20/23

    Fi$ure 19%- II" rec c&e o#tion

    what are the ma>or di''erences /etween ser*ices and We/ser*ices!

    What is the di''erence WCF and We/ ser*ices!

    We% ser"ices can onl* %e in"o/ed %* +TT4. While Ser"ice or a WCF component can%e in"o/ed %* an* protocol and an* transport t*pe. Second we% ser"ices are notfle'i%le. +owe"er$ Ser"ices are fle'i%le. If *ou ma/e a new "ersion of the ser"icethen *ou need to -ust e'pose a new end. Therefore$ ser"ices are a&ile and which is a"er* practical approach loo/in& at the current %usiness trends.

    What are di''erent /indin$s su##orted / WCF!

    WCF includes predefined %indin&s. The* co"er most of %indin&s widel* needed inda*,to,da* application. +owe"er$ -ust incase *ou find that *ou need to definesomethin& custom WCF does not stop *ou. So let us tr* to understand what each%indin& pro"ides.

  • 8/10/2019 WCF Part I.doc

    21/23

    (asic+ttp(indin&1 , This %indin& is used when we need to use SO 4 o"er +TT4. This%indin& can also %e confi&ured to %e used as +TT4S. It can %e also confi&ured tosend data in plain te't or in optimi ed form li/e ;TO;.

    Note: - %&O% is discussed in one of the pervious $uestions in this chapter.

    Ws tt# indin$% , It is same li/e (asic+ttp(indin&. In short$ it uses SO 4 o"er+TT4. (ut with it also supports relia%le messa&e transfer$ securit* and transaction.WS, elia%le ;essa&in&$ securit* with WS,Securit*$ and transactions with WS, tomicTransaction supports relia%le messa&e.NetTc# indin$% , This %indin& sends %inar*,encoded SO 4$ includin& support forrelia%le messa&e transfer$ securit*$ and transactions$ directl* o"er TC4. The %i&&estdisad"anta&e of NetTcp(indin& is that %oth ser"er and client should %e also madein .NET lan&ua&e.NetNamedPi#es indin$% ,Ths %indin& Sends %inar*,encoded SO 4 o"er namedpipes. This %indin& is onl* usa%le for WCF,to,WCF communication %etween processes

    on the same Windows,%ased machine.

    Note: - An interprocess control 5+, 6 protocol is used for exchanginginformation bet#een t#o applications! possibly running on differentcomputers in a net#or*. &he difference bet#een Named pipes and & , isthat named pipes have good performance in terms of communication #ith in

    processes. 1ut #hen it comes to communicate across net#or* & , holds thebest choice. So if you are using to communicate #ith process it7s thebest choice to use in terms for performance. Named pipes do not perform#hen the traffic is heavy as compared to & ,+,.

    Net smq indin$% , This %indin& sends %inar*,encoded SO 4 o"er ;S; . This%indin& can onl* %e used for WCF,to,WCF communication.

    Which are the *arious #ro$rammin$ a##roaches 'or WCF!

    What is one-wa o#eration!

    IsOneWa* e ual to true ensures that the client does not ha"e to wait for the

    response. So methods mar/ed %* IsOneWa* to true should alwa*s return "oid. Inthis$ the caller does not &et an*thin& in return so it is called as one,wa*communication.In order to understand one,wa* implementation in WCF lets ma/e a codewal/throu&h of a sample.Note1 , ou can find code for the same in =WCFIsOneWa*@ folder in C).

  • 8/10/2019 WCF Part I.doc

    22/23

    Fi$ure 1:% - (ne-Wa in action

    %o"e is the code snippet$ which descri%es practicall* how one wa* wor/s in WCF.The a%o"e &i"en code snippet is num%ered. (elow is the e'planation accordin& to thenum%ers mar/ed in fi&ure1,8 , This is the code snippet of the ser"er ser"ice. We ha"e created a method calledas do+u&eTas/. )o+u&eTas/ ma/es the method sleep for 000 ;S and then displa*s

    the time when the tas/ is completed.5 , This code snippet is for client. It creates a pro'* o%-ect of ser"iceIsOneWa* andcalls the do+u&eTas/ method. fter callin& the do+u&eTas/$ the client e'ecutioncontinues ahead. So as a proof$ we displa* the time when the method callin& wascompleted.3 , This screen shot shows the output &i"en %* %oth ser"er and client. The topwindow displa*s the ser"er output and the %elow windows displa*s the client output.

  • 8/10/2019 WCF Part I.doc

    23/23

    Note: - /ou can find the code for the same in +sOne ay folder. or generating the proxies you have to follo# the same steps #hich are sho#nin the previous steps.

    So run the ser"er pro&ram first i.e. Ser"iceIsOneWa* and run the client later. ou will

    see the client runs the do+u&eTas/ and mo"es ahead. Therefore$ the clientcompletion time is less than the ser"er is. One more thin& to understand is that onewa* does not &i"e an* notification %ac/ of completion. Therefore$ it is li/e fire andfor&ets.

    ?icense