Archive for the ‘WCF’ Category

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

One of my previous posts was about the Service Oriented Architecture (SOA), this time will introduce to you the Windows Communication Foundation (WCF) Microsoft Implementation for the SOA. As usually Microsoft made it so easy for developers for building their own service. The WCF implements automatically the WSDL for the developers rather than writing the whole WSDL using XML for the service.

The WCF is composed of 2 main things the interface and the service itself, certainly you will need to define your endpoints and to configure it to the required URL programmatically. One of the main targets of the WCF is to allow multiple different platforms to reach each other and to execute some functions from each other’s like explained in the SOA. The WCF can be hosted as a normal Web service on IIS, or even can be hosted in another application. The WCF can be used developing Composite Apps, however the Composite Apps don’t only depend on WCF.

When developing using WCF, you have to define the functions that the clients will consume. The interface class must be defined as service contract, also you need to define the operations or the methods the client will consume otherwise the methods won’t be seen by the client.

You can find an example application in this link with a complete explanation in it. It is a small application just to clarify the WCF Technology.


 

If you are a developer you will certainly hear a lot about SOA (Service Oriented Architecture). So what is this? Let me get it the most important part a developer, it is a concept or you can use the services or code that someone else wrote, In other words you don’t have to write the whole program from scratch, you will consume other’s programs or services already built. Certainly that helps people a lot, it helps you save time, effort and money, let me give you an example when you integrate Bing’s map in your application, all you have to do is to add the services in your application as service reference if you are using visual studio and if not, it will take more effort adding the URL, however you will be able to consume the service, its functions and you may reuse them as long as you need.

This was the simple explanation for the SOA from the point of view of a developer; let us go a little deeper. Any SOA mainly depends on 2 things WSDL: Web Services Description language for describing web services and how developers can access these services and the second thing is the SOAP protocol to describe the communications protocols. The WSDL is a normal XML file mainly contains all the required information to invoke the web service. The SOAP protocol is simple XML-based to let applications exchange information over HTTP.

Most of the SOA available are web-based but that doesn’t mean that all the SOA must be web-based other can be hosted in apps.

How the SOA can help you in developing an application? As I said before in the beginning of the post, it helps you saving time, efforts and money, however this is not all. The interoperability is also one of the biggest advantages of the SOA. It helps you build applications on several different platforms consuming the service; these platforms no matter what programming language are written with, no matter what platforms they are built on, they can read the SOA and consume the service and may reuse the code as much as it needs.