simple wcf rest service example

Upload: rajesh-samanth

Post on 01-Jun-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Simple WCF REST Service Example

    1/12

    simple WCF REST Service example. Here are summarized steps.

    (1) Run Visual Studio

    () !e" #ro$ect % WCF % &WCF Service 'pplication

    () *elete +Service1.cs and Service1.svc

    (,) 'dd !e" +tem % &WCF Service item. !ame it SimpleRestService.svc.(-) Write WCF RESTul interace in +SimpleRestService.cs.

    using System.Runtime.Serialization;

    using System.ServiceModel;

    using System.ServiceModel.Web;

    namespace SimpleWcfRESTService

    {

    [Serviceontract!

    public interface "SimpleRestService

    {

    [#perationontract!

    [Web$et%&riTemplate'(Test()!

    string *oTest%);

    [#perationontract!

    [Web$et%&riTemplate ' (ustomer+ame,{id-()! ,, &R" param is

    alays string type

    string $etustomer+ame%string id);

    [#perationontract!

    [Web$et%&riTemplate ' (ustomer,{id-(/ 0odyStyle '

    WebMessage0odyStyle.0are)!

    ustomer $etustomer%string id);

    [#perationontract!

    [Web"nvo1e%Met2od ' (3#ST(/ &riTemplate ' (ustomer(/

    0odyStyle ' WebMessage0odyStyle.0are/

    Re4uest5ormat ' WebMessage5ormat.6ml)!

    ,, *efault re4uest format is 6M7

    ,, S2ould set ontent8Type9 application,:ml in re4. 2eader

    bool ddustomer%ustomer cust);

    [#perationontract!

    [Web"nvo1e%Met2od'(3&T(/ &riTemplate'(ustomer,{id-(/ 0odyStyle

    ' WebMessage0odyStyle.0are)!

    bool &pdateustomer%string id/ ustomer cust);

    [#perationontract!

  • 8/9/2019 Simple WCF REST Service Example

    2/12

    [Web"nvo1e%Met2od ' (*E7ETE(/ &riTemplate ' (ustomer,{id-()!

    bool *eleteustomer%string id);

    -

    [*ataontract%+amespace'(()!

    public class ustomer {

    [*ataMember!

    public int "d;

    [*ataMember!

    public string +ame;

    [*ataMember!

    public string ddress;

    -

    -

    (/) Write WCF REST service class t0at implements t0e interace.

    namespace SimpleWcfRESTService

    {

    public class SimpleRestService 9 "SimpleRestService

    {

    public string *oTest%)

    {

    return (Testing SimpleRestService...(;

    -

    public string $etustomer+ame%string id)

    {

    ustomer c ' *03rocessor.$etustomer%id);

    return c.+ame;

    -

    public ustomer $etustomer%string id)

    {

    return *03rocessor.$etustomer%id);

    -

    public bool ddustomer%ustomer cust)

    {

    return *03rocessor.ddustomer%cust);

    -

    public bool &pdateustomer%string id/ ustomer cust)

    {

  • 8/9/2019 Simple WCF REST Service Example

    3/12

    return *03rocessor.&pdateustomer%id/ cust);

    -

    public bool *eleteustomer%string id)

    {

    return *03rocessor.*eleteustomer%id); -

    -

    -

    For persist mec0anism t0e example uses a 0elper class or data2ase processin3. T0is

    persist mec0anism is not t0e ocus o t0e example 0ere is an example or simple d2

    processin3. 'ssume "e 0ave a *4Customer ta2le (0as +d!ame'ddress columns) in S56

    and "e created 6+!5 to S56 .d2ml or t0e ta2le. For testin3 add a e" sample data.

    using System.7in4;

    namespace SimpleWcfRESTService

    {

    public class *03rocessor

    {

    static *atalasses

  • 8/9/2019 Simple WCF REST Service Example

    4/12

    ddress ' cust.ddress

    -;

    db.*0ustomers."nsert#nSubmit%dbust);

    db.Submit2anges%);

    -

    catc2 {

    return false;

    -

    return true;

    -

    public static bool &pdateustomer%string id/ ustomer cust)

    {

    try

    {

    int cid ' int.3arse%id);

    var dbust ' db.*0ustomers.Single#r*efault%p '= p."d ''

    cid);

    dbust.+ame ' cust.+ame;

    dbust.ddress ' cust.ddress;

    db.Submit2anges%);

    -

    catc2

    {

    return false;

    -

    return true;

    -

    public static bool *eleteustomer%string id)

    {

    try

    {

    int cid ' int.3arse%id);

    var dbust ' db.*0ustomers.Single#r*efault%p '= p."d ''

    cid); db.*0ustomers.*elete#nSubmit%dbust);

    db.Submit2anges%);

    -

    catc2

    {

    return false;

    -

  • 8/9/2019 Simple WCF REST Service Example

    5/12

    return true;

    -

    -

    -

    (7) 8pdate "e2.coni3 % use webHttpBinding or REST service and add REST ("e2Http)endpoint4e0avior.

    ?@:ml version'(88 To avoid disclosing metadata information/ set t2e value

    belo to false and remove t2e metadata endpoint above before deployment

    88=

    ?serviceMetadata 2ttp$etEnabled'(true(,=

    ?>88 To receive e:ception details in faults for debugging

    purposes/ set t2e value belo to true. Set to false before deployment

    to avoid disclosing e:ception information 88=

    ?service*ebug includeE:ception*etail"n5aults'(false(,=

    ?,be2avior=

    ?,service0e2aviors=

  • 8/9/2019 Simple WCF REST Service Example

    6/12

    ?>88 "f REST be2avoir is not specified/ t2e folloing error

    occurs9

    T2e message it2 To

    D2ttp9,,local2ost,SimpleRest,SimpleRestService.svc,testD cannot be

    processed at t2e receiver/

    due to an ddress5ilter mismatc2 at t2e Endpoint*ispatc2er.2ec1 t2at t2e sender and receiverDs Endpointddresses agree.88=

    ?endpoint0e2aviors=

    ?be2avior name'(REST(=

    ?ebCttp ,=

    ?,be2avior=

    ?,endpoint0e2aviors=

    ?,be2aviors=

    ?serviceCostingEnvironment multipleSite0indingsEnabled'(true( ,=

    ?,system.serviceModel=

    ?system.ebServer=

    ?modules runllManagedModules5orllRe4uests'(true(,=

    ?,system.ebServer=

    ?,configuration=

    (9) ++S 0ostin3 % "e can create a ne" Site or t0e REST service. 4ut in t0is example "e add

    a ne" We2 'pplication called SimpleRest under t0e *eault We2 Site (port 9:). So t0e REST

    service can 2e accessed at 0ttp;

  • 8/9/2019 Simple WCF REST Service Example

    7/12

    (>) ?nce t0e REST service is pu2lis0ed to *eault We2 Site tr= a simple test 8R6 in "e2

    2ro"ser.

    0ttp;

  • 8/9/2019 Simple WCF REST Service Example

    8/12

    restart app pool t0e 8R6 a2ove s0ould 3ive !ame or customer id B 1.

    (11) HTT# ET url can 2e tested in "e2 2ro"ser 2ut i "e "ant to send some data to REST

    service via #?ST or #8T "e "rite REST clent pro3ram 'D' in 0tml or use utilit= tool suc0

    as Fiddler. To "rite REST client pro3ram "e could use various '#+s suc0 as HttpClient

    (REST Starter it) WCF We2C0annelFactor= HttpWe2ReGuest We2Client. 4ut 0ere lets

    use Fiddler tool.

    http://www.fiddler2.com/http://2.bp.blogspot.com/-KWquwZuSiSo/ULk2WRNhISI/AAAAAAAAAQs/eZXawMeiBQ4/s1600/app-pool-acct.pnghttp://www.fiddler2.com/
  • 8/9/2019 Simple WCF REST Service Example

    9/12

    +n order to test &'dd Customer in Fiddler 3o to Composer and select #?ST met0od and

    t=pe url 0ttp;

    deault order o *ataContract serialization

  • 8/9/2019 Simple WCF REST Service Example

    10/12

  • 8/9/2019 Simple WCF REST Service Example

    11/12

    (1) How to use JSON

    +nstead o I6 as a reGuest input or response output "e can use simpler DS?! ormat.

    *eault reGuest

  • 8/9/2019 Simple WCF REST Service Example

    12/12

    -

    +n order to use DS?! rom Fiddler "e set Content%T=pe to application/jsonand put $son

    ormat input in reGuest 2od= as seen 2elo". Response "ill 2e also DS?! ormat as "e set

    ResponseFormat to Dson in We2+nvo@e attri2ute.

    http://3.bp.blogspot.com/-aUFiaE9wMNk/ULvYiO7lYZI/AAAAAAAAATU/mbO5ko5rgTs/s1600/rest-json.png