Windows Communication Foundation: Development Explanation (1)

Posted: July 4, 2012 in WCF

This blog is for explaining the previous blog post about WCF. To build your own Web Service, the main two things you have to care of are the WSDL and the SOAP which defines your way of communication between the service and the client. WCF helps you manage this. All you have to do is to define the class, operation and the attributes that will be needed in the applications. To do so, in your application, build an interface class. Like the following and define the class and the operation that you will allow the application to see and use.

[ServiceContract]


public
interface
IMyService

{

[OperationContract]


string WelcomeMSg();

 

[OperationContract]


float Calculate(att t);

}

 

The interface will be called a service, you may have one or more service as much as your application needs and the number of applications and their use of the services available. The interface will be a service contract and the methods and functions in are called the operation contracts. In the class implementing this interface, it will contain the whole body code as a normal class.

 

public
class
MyService : IMyService

{


public
string WelcomeMSg()

{


return
“Starting the Service”;

}

 


public
float Calculate(att t)

{


if (t.op == ‘+’)

{


return t.firstnumber + t.secondnumber;

}


else
if (t.op == ‘-‘)

{


return t.firstnumber – t.secondnumber;

}

 


else


return 0;

}

}

 

Dealing with attributes, you might want the application and the service to share some attributes rather than entering the strings and integers in the parameters like the att in this example. So I added a new class and defined it a datacontract this is class contains all the attributes that will be shared between the client and the service, each one of them will be defined as a datamember so that both the application and the service can see. Like the following code:

 

 

[DataContract]


public
class
att

{

[DataMember]


public
float firstnumber { set; get; }

 

[DataMember]


public
float secondnumber { set; get; }

 

[DataMember]


public
char op { set; get; }

}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s