Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

What is WCF?

Posted on 4/12/2008 @ 1:03 PM in #WCF | 16 comments | 9254 views

WCF stands for "Windows Communication Foundation".

It is that part of .NET 3.0, which is the newest coolest way of establishing communication between two pieces of executing computer code.

Now, we have had DDL, DCOM, Remoting, Web Services, WSE, .. why .. YET ANOTHER .. way of communication? Why oh why do I have to learn YET another thing?

Well, I hear ya.

WCF is fundamentally different from previous communication technologies. Another common "jargon-phrase" you will hear thrown around a lot is "SOA" or Service Oriented Architecture. Extremely simplified, think of SOA has different pieces of code/services running around the world and clients can make use of those services. These service hosts take care of internal complexities, and the communication infrastructure allows for things such as versioning and the communication infrastructure.

Thus, a big tenet of SOA is isolation. Let me explain with an example.

Say, you are consuming a web service, that accepts a zipcode, and returns you a weather forecast. All you, the client, ever cares about is .. the zipcode (input), and the forecast (output). Beyond the input/output .. or request/response .. the service host .. never talks to the client. There is an isolation between the client and server, and they interoperate on only what they had agreed to interoperate upon, i.e. the contract. So, this brings me to the first important piece in WCF, "The Contract".

Contract: This is the heart of your service - what does your service do!

In WCF, a contract is usually implemented as an interface decorated by the [ServiceContractAttribute]. For instance, in this particular case, your contract would look like this -

   1: [ServiceContract]
   2: public interface IWeatherService
   3: {
   4:     [OperationContract]
   5:     public WeatherForecast GetForeCast(int zipCode);
   6: }

But is a contract all I need? Well no .. because I need to know atleast 3 peices of information before I can use such a service -

  • Where can I find this service - The Address.
  • What protocol will I use in using this service - The Binding.
  • And of course the contract - when I know where the service is (address), and how I will technologically speaking use it (binding) - what the heck will I do with it .. the Contract.

So, in WCF, you have ABC's -

The Basics

Address: Address is where your service can be found. Usually in the format of scheme://domaon[:port]/path. Example http://localhost:8080/MyService

Binding: A binding refers to specific protocols used by a particular endpoint. Example, http, tcp, msmq etc.

Contract: This is the heart of your service - what does your service do!

These 3 together, form an "EndPoint".

EndPoint: EndPoint is how a ServiceHost, exposes a service, so clients can invoke it's operations defined in the contract. (Read that again!).  So, how is an EndPoint different from an Address? An EndPoint have 3 peices - Address, Binding, and Contract.

Now, note that in the contract I showed above, I am using a return type called "WeatherForecast". The "WeatherForecast" is a class I wrote, that represents a business object which lets me conveniently hold the details of the data I am accepting or sending. This along with all details of the endpoint constitute, the "MetaData" for the service.

Also, for "WeatherForecast" to go over the wire, it must be Serializable. Being Serializable means, the object can be hydrated, dehydradted from a stream/bunch of bytes, into a living class instance. Thus, if the human body was serializable, teleportation like Star Wars shows us all the time, would be possible. The business object "Human Body" would be dehydrated into "information" - sent over a communication channel(s), and then rehydrated at the other side - to produce a human being on the other side. Sure beats my commute.

And given the MetaData for a service, a consumer of the service, can now create a Proxy - which is an empty shell, or a type, that exposes all details of the service.

Proxies: A client communicates with a host using a proxy. A proxy is an empty type that exposes all operations in a service contract, and hides serialization/sending over wire details. A single proxy, uses a single endpoint.

Finally, the client, and the host, talk to each other through various channels. And around these channels, you may have various behaviors associated with the service.

Channels: Channels, is what a message goes through when it goes between a client and a host. Usually you have multiple stacked channels.

Behavior: Behaviors modify the message as they flow through a channel stack. A good example of a behavior is authorization.

So .. overall ..

You have a host, and a client, both agree on a contract. The host exposes EndPoints which are a combination of Address, Binding and Contract. The client uses a proxy, that is tied to a particular endpoint - thus tied to a particular contract. And the actual communication related details are abstracted in Channels and Behaviors.

That one line above, summarizes WCF.

So, what makes WCF different?

  • You first think of the Contract - What am I trying to get done!?
  • Then you think of where will I host it, and what technology will I use to host it.
  • Then you worry about the nitty gritties such as authentication etc.

For the very first time, you have a communication technology, that lets you first focus on what you're trying to get done, and worry about communication later.

I say, time to write a quick application huh?


On 4/12/2008 6:04:11 PM Leniel Macaferi said ..
Hi Sahil,

Great post explaining the basics of WCF. Keep up the good work!

I was going to read this post http://blah.winsmarts.com/2008-4-Writing_the_WCF_Hello_World_App.aspx but then I followed your advice and started with this one.

Leniel Macaferi


On 4/12/2008 9:08:09 PM Sahil Malik said ..
Hey Leniel :),

I checked out your blog man. Dude you're like some hifi scholar. Please feel free to shed some words of wisdom here too .. ok? :)

Sahil


On 4/12/2008 9:09:12 PM Tasneem said ..
You do have a knack of explaining jargons in simple words. I am looking forward to see examples using different type of bindings especially MSMQ.


On 4/12/2008 11:29:13 PM Sahil Malik said ..
Thanks Tasneem .. see I explain stuff only I understand myself .. LOL, so it becomes simple.

Look at me .. as an MSDN translator.


On 5/6/2008 9:51:04 AM Patrice RAUCQ said ..
Hi Sahil,


Your explanations are very clear and usefull. Using the same talent could you explain what can decide the developer when he/she has to choose a binding. What makes the difference between TCP, HTTP, MSMQ, etc. if all technologies are available in the production environment ?

Thanks in advance,

Patrice.


On 5/6/2008 9:52:08 AM Sahil Malik said ..
Thanks Patrice - I'll do that shortly. Had a few busy days, but should be able to get back to this serious in 2 weeks.


On 5/28/2008 3:38:07 AM Torben Bach said ..
Excelent post - short, clear and to the point :-)


On 7/3/2008 2:03:20 AM Aarti said ..
Hi Sahil,

I have to learn WCF for some project requirement. When i started going thru msdn, I found it so boring and dry and complicated...and what not..i had slowly started losing interest in the technology itself..:(

But after reading ur post i understood the basics very quickly..such free and simple lingo...


Very nice article..!!


On 8/12/2008 12:02:08 PM Matt said ..
Great post I was able to figure out the basics first read. I have to make one point, there are no transporters in Star Wars that would have to be Star Trek :).


On 8/12/2008 1:05:38 PM Sahil Malik said ..
Matt - I am the most non-geek nerd you'll meet. I've never even seen the star*.* movies.


On 8/22/2008 1:32:47 PM Arvinth Chandramouli said ..
I am trying to design an n-tier app. I am trying to design a data access layer and want to remote the business objects. Please shed your opinion about using strongly typed datasets vs creating my own data access code for business objects and remoting vs WCF. thanks very much.


On 9/2/2008 8:34:01 PM Lloyd Phillips said ..
Great article, you certainly do have a knack for putting things into layman's terms. I can't stand reading MSN they just go on with geeky jargon rather than just saying it how it is in a few sentances. I've struggled to understand the concepts of WCF when researching it for the last few weeks. I've been looking at creating an AJAX based site using the Dojo toolkit. I need to connect much of the display to an asp.net back end but really wanted to do it via JSON as XML is so much more bloated. Having seen some bits on the web I thought WCF would be a good option for this as I can make AJAX calls and get JSON returned. It's looking like I might be correct. I'd LOVE to se some examples from you on creating a project that returns JSON via WCF. Keep these articles coming!


On 12/6/2008 7:04:03 AM waqar qureshi said ..
hello,


ur article is good enough, but i am facing a problem for the last 7 days and no body is able to help me.


i wanna create wcf services for our company website but it works fine on localhost but i am not able to deploy on our website through your example. Even if i publish my wcf service on our website through other methods, i am not able to access it from new asp.net project. what are the changes i have to make in web.config so that i am able to deploy and if already published, to access it.

ur help will be highly appreciated.


On 4/1/2009 4:18:21 PM WarNov said ..
Hey Sahil,

They in MSDN must include an introduction like this for every theme they have there.


It is true, that I spent like two hours reading their WCF Introduction, lost in a million of hyperlinks and never got an idea as clear as the one I got reading your post.

Congratulations!


On 4/1/2009 4:54:41 PM Sahil Malik said ..
Thank you Warnov. I will never write for MSDN Mag. It seems like you need to have a "friend" there to be able to write for them. And I don't think their "friends" do a good job. Many of their articles I feel are too arcane and more complex than necessary .. simply because the author never took the time to digest the information before presenting it to the reader. Frankly, a good article is what takes a complex concept and makes it simple. MSDN articles seem to show off their complexity. WTF!

S


On 1/5/2010 1:03:33 AM Sarnendu De said ..
Good article,WCF is clearly explained for beginner but not cleared why we need WCF instead of Remoting, Web Services.It will be helpful if you can clear.


Thanks


Sarnendu De

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.