In this article, I am going to show you a small Addition programme using WCF. After going through this snippet, the reader will get a clear and basic understanding of the WCF programme.
WCF (Windows Communication Framework) is a unification technology, which unites the following technologies:
- NET remoting
- Web services
It is based on SOA (Service Oriented Architecture).
The above terminologies are the core on which SOA stands. Every service must expose one or more ends by which the service can be available to the client. End consists of three important things: where, what and how:
Contract is an agreement between two or more parties. It defines the protocol how the client should communicate with your service. Technically, it describes parameters and return values for a method.
An address indicates where we can find this service. Address is a URL, which points to the location of the service.
Bindings determine how this end can be accessed. It determines how communications are done. For instance, you expose your service, which can be accessed using SOAP over HTTP or BINARY over TCP. So for each of these communications medium two bindings will be created.
The Important Principles of SOA?
WCF is based on SOA. SOA is based on four important concepts:
Boundaries are Well Defined
In SOA, everything is formalized. The client who is consuming the service does not need to know how the implementation of the service is done. If you look at some old methodologies of communication like DCOM, any changes at server level the client also has to change. Therefore, the server and client implementation was so much bound that changes need to be done at all places. In SOA, the rule is if you do enhancement you do not need to change anything at the client. SOA based application only understands that there is an end point, contract, and bindings.
Note: Just to clarify shortly about end point and contract, any SOA service is exposed through an end point. End point defines three important aspects What, Where and How.
Change is the law of nature and services will evolve. In SOA, services can be versioned and you can host those services in new ends. For instance, you have a service called as “Search Tickets (Ticket Number)“ which gives details based on Ticket Number and it's exposed on end point “ep1”. Tomorrow you want make your Search Tickets service more useful by also providing an extra option of allowing him to search by passenger name. Therefore, you just declare a new end “ep2” with service “Search Tickets (Ticket Number, Passenger Name)”. So the client who is consuming the service at end ep1 continues and at the other end, we have evolved our service by adding new ends ep2.
Services Share Only Schemas and Contracts
Services use Schemas to represent data and contracts to understand behavior. They do not use language dependent types or classes in order to understand data and behavior. XML is used to define schemas and contracts. Due to this, there is no heavy coupling between environments.
Service Compatibility is Policy Based
Policy describes the capabilities of the system. Depending on policies, the service can degrade to match the service for the client. For instance, your service needs to be hosted for two types of clients, one which uses Remoting as the communication methodology while the other client uses DCOM. An ideal SOA service can cater to both of them according to their communication policies.
Step 1: Open VS2008 , create project and choose "Windows Service Application", just give any name to your project. I named it "MyService".
Step 2: You will see the solution window, in that open "IMyService.cs".
In that, you will see
[ServiceContract] - below this your interface name is declared. There after you'll see
[OperationContract] - your function contract should be defined here. The implementation of the function will be defined in "MyService.svc.cs" as provided in the picture.
Step 3: Open "MyService.svc.cs" and write code here as I have written code for my "
addfunction". You can write your code inside your function. The picture is shown below:
Step 4: This is the most important step. In this, we are declaring the end point. Inside this, <system.servicemodel />we can define an end point as shown in the picture. End point is defined automatically. We can also define it by program.
Step 5: Save the project and run it. This will display like this. Copy the address from explorer address bar.
Let the server service run (the above page).
Step 1: Open a web application in another VS2008 and right click on solution name and then go to "Add Service Reference" one window will open paste the previously copied link to the address bar and press GO. Then the service will appear in service section, then press OK. The service reference will appear in solution explorer as below:
Step 2: Now add one button on the default page, then double click on button you'll be in code behind section. Here create the object of the service which appeared in the solution explorer. Something like this:
protected void Button1_Click(object sender, EventArgs e)
ServiceReference1.MyServiceClient cls =
Response.Write( cls.addData(5, 4));
Step 3: Now save the project and run it. If everything goes fine, then the output will be like this:
Hope this article may helped you to understand and build a simple addition program in WCF.
- 3rd June, 2009: Initial post