• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Data Contract in WCF services

Arithmetic operations using data contract in WCF services

11Create a new WCF service with the name example2

11Change the interface name form I service.cs to I arithematic.cs 11 change the class name from service.cs to cls arthematic.cs11 change the service file name from service.svc to cls arithematic.svc 11double click on I arithematic.cs & write the following code.

11In data contract we declare all data fields & properties.

11To provide data facilities to the services contract we use data contract.

11Data contract class object reference is used in service contract

[Service contract]

Public interface I arithematic

{

[operation contract]

Int add (Arithmetic data obj1);

[operation contract]

Int subtract (arithematic data obj1);

[operation contract]

Int multiply (arithematic data obj1);

[operationcontact]

Int divide (arithematic data obj1);

}

[Data contract}

Public class arithematicdata

{

Int num1, num2, result;

[data member]

Public int p num1

{

Set{ num 1 = value;}

Get {return num1:}

}

[data member]

Public int p num 2

{

Set {num 2 = value;}

Get { return num2:}

[Data member]

Public int presult

{

Set { result = value;}

Get {return result;}

}

}

Double click on cls arithmetic.cs file in solution explorers write the following code

Pulbic class cls arithematic : I arithematic
{

Public int add (arithematic data obj1)

{

Obj1. Presult = obj 1.pnum1 + obj 1 .pnum2; return obj1. Presult;

}

Public int subtract (arithematic data obj1)
{

Obj1. Presult = obj1. Pnum1 – obj1. Pnum2;
return obj1. Presult;
}
public int multiply (arithmetic data obj1)
{
obj1. Presutl = Obj1. Pnum1* obj1.pnum2: return obj1. Presult:
}

Public int divide (arithematic data obj1)
{
obj1. Presult=obj1. Pnum1/obj1.pnum2; return obj1. Presutl;}}

Change in cls arithematic.sve file the service name to cls arithematic à to to web. Config file change the service name to cls arithematic & contract ot I arithmetic à run the application & check the path of cls Arithematic.Svc

 

Example to consume the WCF service / creating client component:-

 

Screenshot_63

 

Create a new web site with the name “check example2” 11desing the webpage. 11add reference to example2 WCF service class i.e., cls arithematic.svc with namespace servie reference name as “SR1” 11 write the following code.

Using SR1:

Arithematic client objs = new Arithematic client ();

Arithematic data objd = new arithematic data ();

Protected void btn add – click (—)

{
objD.pnum1= convert. TOInt 32 (txtnum1.text)

objD.pnum2= convert. TOInt 32 (txtnum2.text)

objD.pnum = objs. Add (objD);

text result. Text = objD. Presult. Tostring();

}

Protected void btnsubtruct_ click (=—)

{

objD.pnum1= convert. TOInt 32 (txtnum1.text)

objD.pnum2= convert. TOInt 32 (txtnum2.text)

objD.pnum = objs. Add (objD);

text result. Text = objD. Presult. Tostring();

}

Protected void btndivide – click (—)

{

objD.pnum1 = convert. Toint32 (textnum1.Text);

objD.pnum2 = convert. Toint32 (textnum2.Text);

objD.preset = objs. Divide (obj D):

txtresult . text = ObjD. Presutlt. Tostring();

}}

Summary
Review Date
Reviewed Item
Data Contract in WCF services
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Data Contract in WCF services"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.