Click here to Skip to main content
Click here to Skip to main content

A Step by Step WCF Small Program For Beginners

, 3 Jun 2009
Rate this:
Please Sign up or sign in to vote.
A Step by Step WCF Small Program For Beginners

Abstract

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.

Introduction

WCF (Windows Communication Framework) is a unification technology, which unites the following technologies:

  • NET remoting
  • MSMQ
  • Web services
  • COM+

It is based on SOA (Service Oriented Architecture).

What are Ends, Contract, Address, and Bindings?

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 (What)

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.

Address (Where)

An address indicates where we can find this service. Address is a URL, which points to the location of the service.

Binding (How)

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.

Services Evolve

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.

Code Snippet

(Service/Server Side)

Steps Involved:

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, 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.

Code Snippet

(Client Side)

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 = 
	new wcfProxycall.ServiceReference1.MyServiceClient();
cls.Open();
Response.Write( cls.addData(5, 4));
cls.Close();
}

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.

Thanks.

History

  • 3rd June, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

divyaloksuman
Software Developer (Senior)
India India
Divyalok Suman has done B.E in Computer Science from bangalore ,Karnataka, India . Past 6 years he is working on web-based and windows based solutions in Asp.net MVC 1.0/2.0/3.0/4.0, ASP.NET 2.0/3.5/4.0, C# 2.0/3.5/4.0, AJAX, Web Services, MS SQL Server 2005/2008,Win Forms, Window services. He is also an MCP and MCTS . He has good knowledge of Object Oriented Programming, 3-Tier Architecture and Designing.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 107893073-Jun-14 21:25 
Questionvery nice tutorial.. PinmemberTharanVJ29-Jan-13 18:27 
GeneralStep by Step WCF application Pinmemberumeshfaq7-Aug-12 2:15 
GeneralMy vote of 3 PinmemberRanel50429-Jan-12 23:14 
GeneralReally thanks PinmemberRamendra Kumar Sinha9-Jan-12 20:22 
GeneralMy vote of 5 PinmemberUnareshraju2-Jan-12 2:00 
QuestionNice Article PinmemberSaily27-Oct-11 9:57 
QuestionGood article Pinmemberp_karthik_p22-Jun-11 3:17 
QuestionThis is nice article PinmemberMember 221380018-Jun-11 20:00 
GeneralGood article, But Images are Missing PinmemberKiran Kumar Veerabatheni5-Apr-11 21:06 
GeneralDear Divyaloksuman, PinmemberMyLinh_it9-Jan-10 5:18 
GeneralDear,divyaloksuman Pinmembergangzhiyong7-Jan-10 19:35 
GeneralRe: Dear,divyaloksuman PinmemberMyLinh_it9-Jan-10 5:15 
Dear Gangzhiyong ,
You can double click on the pictures, you'll see them more clearly.
Regards,
GeneralRe: Dear,divyaloksuman Pinmembergangzhiyong10-Jan-10 15:17 
AnswerRe: Dear,divyaloksuman Pinmemberdivyaloksuman14-Jan-10 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 3 Jun 2009
Article Copyright 2009 by divyaloksuman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid