Working with WCF services

Working with WCF services:-

Go to VS.NET 11 click on File 11 click on new 11 click on website 11 select WCF service Template 11 select the language as visual c# 11 select the location as http11 type the service application name (http://Local host /example1)

Click on OK.

When we create web service by default 3 files will be created.

1) I service .cs (this is interface file which contains service contracts & Data contracts).

2) service .cs (this is a class file & contains implementation of the interface file)

3) service.svc ( this is WCF service file)

First 2 files i.e., I service.cs & service.cs are placed in separate folder “app_code”

Double click on app-code folder change I service.cs file name ot “I arithematic.cs”

Change the service.svc filename to “clsarithematic.svc”

Double click on I arithmetic.cs file change the interface name to I Arithmetic

Delete the code available in interface

Delete the code from data contract attribute to remaining

Write the following code in interface,

[service contract]

Public interface I Arithmetic

{

[operationcontract]

Int add (int x, int y):

[operation contract]

Int subtract (int x, int y);

[operationcontract]

Int multiply (int x, int y);

[operationcontract]

Int divide (int x, int y);

}

Go to solution explorer  à Double click on cls Arithmetic file in app-code folder à change the class ham ot  cls arithmetic which inherits from I Arithmetic interface à Delete the total code available in the class  & write the following code,

Public class cls Arithmetic: I arithmetic

{

Public int add (int x, inty)

}

Return x to y:

}

 

Public int subtract (intx, inty)

{

return x-y;

}

Public int multiply (intx, inty)

{

return x*y;

}

Public int divide (int x, int y)

{

return x/y;

}

}

Go to  solution explorer 11 double click on web.config fole

Go to the tag<system.servicemodel> 11 change the service name to clsarithematic & contract to I arithematic like,

<system.servicemodel>

<services>

<service name =”cls arithematic” ……………….>

<end point address – “” binding = “Wshttpbinding” contract = “ I Arithematic”>

11Goto solution explorer 11 double click on cls Arithmetic .svc 11 change the name service to cls arithmetic & code behind file name also from service.cs to cls arithmetic.cs  11 build the application & run the application.

 

Consuming the WCF service:-

11Create a new website with the name “ check Example 1”

11Design the webpage

Go to solution exploreràselect the solution click with right mouse button 11click on “Add service reference “ 11 Type http://local host /example1/arithematic.svc” in dropdown list box (this is the address we get in the address bar of internet explorer we run WCF service ) 11 change the namespace name “ servvicereference 1” to “SR1” 11 click on ok 11 write the following code,

Screenshot_62