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

What's the Difference between WCF and Web Services?

By , 26 Dec 2010
 

Introduction

In this article, I will explain the difference between ASP.NET web service and WCF services like ASP.NET web services. I will also discuss how we use both the technologies for developing the web services.

Background

Web Service in ASP.NET

A Web Service is programmable application logic accessible via standard Web protocols. One of these Web protocols is the Simple Object Access Protocol (SOAP). SOAP is a W3C submitted note (as of May 2000) that uses standards based technologies (XML for data description and HTTP for transport) to encode and transmit application data.

Consumers of a Web Service do not need to know anything about the platform, object model, or programming language used to implement the service; they only need to understand how to send and receive SOAP messages (HTTP and XML).

WCF Service

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data.

In what scenarios must WCF be used

  • A secure service to process business transactions.
  • A service that supplies current data to others, such as a traffic report or other monitoring service.
  • A chat service that allows two people to communicate or exchange data in real time.
  • A dashboard application that polls one or more services for data and presents it in a logical presentation.
  • Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.
  • A Silverlight application to poll a service for the latest data feeds.

Features of WCF

  • Service Orientation
  • Interoperability
  • Multiple Message Patterns
  • Service Metadata
  • Data Contracts
  • Security
  • Multiple Transports and Encodings
  • Reliable and Queued Messages
  • Durable Messages
  • Transactions
  • AJAX and REST Support
  • Extensibility

Difference between Web Service in ASP.NET & WCF Service

WCF is a replacement for all earlier web service technologies from Microsoft. It also does a lot more than what is traditionally considered as "web services".

WCF "web services" are part of a much broader spectrum of remote communication enabled through WCF. You will get a much higher degree of flexibility and portability doing things in WCF than through traditional ASMX because WCF is designed, from the ground up, to summarize all of the different distributed programming infrastructures offered by Microsoft. An endpoint in WCF can be communicated with just as easily over SOAP/XML as it can over TCP/binary and to change this medium is simply a configuration file mod. In theory, this reduces the amount of new code needed when porting or changing business needs, targets, etc.

ASMX is older than WCF, and anything ASMX can do so can WCF (and more). Basically you can see WCF as trying to logically group together all the different ways of getting two apps to communicate in the world of Microsoft; ASMX was just one of these many ways and so is now grouped under the WCF umbrella of capabilities.

Web Services can be accessed only over HTTP & it works in stateless environment, where WCF is flexible because its services can be hosted in different types of applications. Common scenarios for hosting WCF services are IIS,WAS, Self-hosting, Managed Windows Service.

The major difference is that Web Services Use XmlSerializer. But WCF Uses DataContractSerializer which is better in Performance as compared to XmlSerializer.

Key issues with XmlSerializer to serialize .NET types to XML

  • Only Public fields or Properties of .NET types can be translated into XML
  • Only the classes which implement IEnumerable interface
  • Classes that implement the IDictionary interface, such as Hash table cannot be serialized

Important difference between DataContractSerializer and XMLSerializer

  • A practical benefit of the design of the DataContractSerializer is better performance over Xmlserializer.
  • XML Serialization does not indicate which fields or properties of the type are serialized into XML whereas DataCotractSerializer
  • Explicitly shows the which fields or properties are serialized into XML
  • The DataContractSerializer can translate the HashTable into XML

Using the Code

The development of web service with ASP.NET relies on defining data and relies on the XmlSerializer to transform data to or from a service.

Key issues with XmlSerializer to serialize .NET types to XML

  • Only Public fields or Properties of .NET types can be translated into XML
  • Only the classes which implement IEnumerable interface
  • Classes that implement the IDictionary interface, such as Hash table cannot be serialized

The WCF uses the DataContractAttribute and DataMemeberAttribute to translate .NET FW types into XML.

[DataContract] 
public class Item 
{ 
    [DataMember] 
    public string ItemID; 
    [DataMember] 
    public decimal ItemQuantity; 
    [DataMember] 
    public decimal ItemPrice;
}

The DataContractAttribute can be applied to the class or a strcture. DataMemberAttribute can be applied to field or a property and theses fields or properties can be either public or private.

Important difference between DataContractSerializer and XMLSerializer.

  • A practical benefit of the design of the DataContractSerializer is better performance over XML serialization.
  • XML Serialization does not indicate which fields or properties of the type are serialized into XML whereas DataContractSerializer explicitly shows which fields or properties are serialized into XML.
  • The DataContractSerializer can translate the HashTable into XML.

Developing Service

To develop a service using ASP.NET, we must add the WebService attribute to the class and WebMethodAttribute to any of the class methods.

Example
[WebService] 
public class Service : System.Web.Services.WebService 
{ 
      [WebMethod] 
      public string Test(string strMsg) 
      { 
          return strMsg; 
      } 
}

To develop a service in WCF, we will write the following code:

[ServiceContract] 
public interface ITest 
{ 
       [OperationContract] 
       string ShowMessage(string strMsg); 
} 
public class Service : ITest 
{ 
       public string ShowMessage(string strMsg) 
       { 
          return strMsg; 
       } 
}

The ServiceContractAttribute specifies that an interface defines a WCF service contract,
OperationContract attribute indicates which of the methods of the interface defines the operations of the service contract.

A class that implements the service contract is referred to as a service type in WCF.

Hosting the Service

ASP.NET web services are compiled into a class library assembly and a service file with an extension .asmx will have the code for the service. The service file is copied into the root of the ASP.NET application and Assembly will be copied to the bin directory. The application is accessible using URL of the service file.

WCF Service can be hosted within IIS or WindowsActivationService.

  • Compile the service type into a class library
  • Copy the service file with an extension .SVC into a virtual directory and assembly into bin sub directory of the virtual directory.
  • Copy the web.config file into the virtual directory.

Client Development

Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.

WCF uses the ServiceMetadata tool (svcutil.exe) to generate the client for the service.

Message Representation

The Header of the SOAP Message can be customized in ASP.NET Web service.

WCF provides attributes MessageContractAttribute, MessageHeaderAttribute and MessageBodyMemberAttribute to describe the structure of the SOAP Message.

Service Description

Issuing a HTTP GET Request with query WSDL causes ASP.NET to generate WSDL to describe the service. It returns the WSDL as a response to the request.

The generated WSDL can be customized by deriving the class of ServiceDescriptionFormatExtension.

Issuing a Request with the query WSDL for the .svc file generates the WSDL. The WSDL that generated by WCF can be customized by using ServiceMetadataBehavior class.

Exception Handling

In ASP.NET Web services, unhandled exceptions are returned to the client as SOAP faults.

In WCF Services, unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.

History

  • 26th December, 2010: 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

Yatin V Patil
Software Developer (Senior) Infosys Technologies Limited
India India
I am working as a Technology Analyst at Infosys technologies,Pune. I used to work on C#,ASP.net,WCF,WPF,Sharepoint,PHP,Perl.
 
I blogs at http://www.Tech-Guruji.com/ .
 
You can follow me http://twitter.com/tech_guruji/
 
I am reachable at yatin.patil@webadroits.com
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionnice articlememberMember 958150921-May-13 21:29 
nice article it helped me a lot,can you share urs facebook id
thanks,Amit sharma
synechron tech
GeneralMy vote of 4professionalMohammed Hameed16-May-13 9:55 
Good article...
GeneralMy vote of 5memberM Rayhan9-May-13 20:39 
thanks for your nice article. helped me a lot.
GeneralMy vote of 5memberAbhinesh M6-May-13 2:45 
Nice One, Keep it up. Thanks.
GeneralMy vote of 5memberNeonMika26-Mar-13 9:47 
Nice beginner overview, good to get started Smile | :)
QuestionTo summarize.. [modified]memberdevvvy30-Jan-13 16:48 
Web Service == WCF with basicHttpBinding
(Both supports framework to generate proxy and remote procedure calls, sync and async)
 
Is this true? WCF is a super set, with WS* facilities not previously supported by "ASMX"? The new features includes:
1. Reliability (automatic message resent, transport and Message level)
2. Security (encryption/integrity checks, transport and Message level)
3. Transaction/2pc
 
Am I correct, in drawing up this conclusion?
 
But I think ASMX supports transaction[^] and async callbacks[^]
 
So I am still confused, I don't think you can generalized this way. WCF is just new version of same sh*t ASMX with new syntax and config voodoo.
dev


modified 30-Jan-13 22:58pm.

QuestionDifference between WCF, .Net Remoting and Web Servicesmemberetechpulse8-Jan-13 20:05 
http://www.etechpulse.com/2012/10/all-about-wcf-windows-communication.html
AnswerRe: Difference between WCF, .Net Remoting and Web ServicesmemberRasadul Alam Rashed28-Jan-13 23:07 
For more information please click here:
 
http://cybarlab.blogspot.com/2013/01/what-are-difference-between-web-service.html[^]
If you have any query please feel free to contact me.
Thanks & best regard.
 
Rasadul Alam Rashed

QuestiongreatmemberJaafar Abu Sair16-Dec-12 21:13 
great post Smile | :)
GeneralMy vote of 5memberSavalia Manoj M26-Nov-12 17:00 
Good work...!!
GeneralMy vote of 4memberChamila Ranasinghe19-Nov-12 22:58 
Easy to understand due to comparison. Good work
GeneralMy vote of 5memberMember 307459812-Oct-12 5:23 
Very nice article and easy to understand.
Questiongood articlememberElanchezian24-Sep-12 3:04 
good article,it is very useful for me,
thank u,
 
Elanchezian.A
senior software developer
Generalgood articlemembervishal.shimpi16-Sep-12 18:32 
very good article...
Vishal Shimpi,
Software Developer

GeneralMy vote of 5memberFarhan Ghumra22-Aug-12 23:01 
Excellent
GeneralexcellentmemberinitCode28-May-12 21:32 
nice job!!!
GeneralExcellent Article ...Patil..!memberTushar_Patil8-May-12 20:59 
Easy To Understand Diff...Thanks For Article Thumbs Up | :thumbsup:
Tushar Patil
Failure is not the end it is the time Required to get Success.

GeneralMy vote of 5membersujatapant1526-Apr-12 1:41 
Good Article
GeneralMy vote of 4memberMember 877148431-Mar-12 1:37 
It's nice
Questiongood workmemberyazanjaradat27-Mar-12 4:29 
thank you very much
good explanation

GeneralMy vote of 5memberReza Ahmadi17-Mar-12 1:34 
Good Article
QuestionConcurrency and SessionsmvpDave Kerr7-Mar-12 2:40 
Hi,
 
I love the article but would love to see some comments on how sessions are managed between requests on both platforms - how session state works for example. Also, could you describe how concurrency works? How are large numbers of requests dealt with?
 
Thanks,
 
Dave

QuestionGood Article if you want concise 14 differences between WCF service and webservicememberManishav33886-Mar-12 21:49 
if you want concise 14 differences between WCF service and webservice follow below link
http://www.freejobsreference.com/dotnetforums/1206-14-Top-differences-between-WCF-or-Windows-Communication-Foundation-and-WebService-.aspx[^]
QuestionGood article.memberrohit_k..27-Feb-12 19:41 
Elaborated points in easy language. Keep it up...
 
Regards,
Rohit
xpode.com
AnswerRe: Good article.memberManishav33886-Mar-12 4:10 
Good post but for 14 top more differences between WCF Service and web service follow the below link
www.freejobsreference.com/dotnet-interview-questions-answers/268-What-is-the-difference-between-WCF-Service-and-Web-Service-.aspx[^]
GeneralMy vote of 4membercoded0078-Jan-12 23:15 
I Like it
GeneralMy vote of 4memberNarayan D Mali3-Jan-12 1:29 
it is good
QuestionWell DonementorDave Kerr31-Dec-11 20:41 
The first few paragraphs of this article particularly are really nicely done, I think they very succinctly describe the differences between ASMX and WCF, and their purposes, without getting bogged down in too much detail. Well done, keep up the good work!

GeneralAdding the attribute WebService and WebMethodAttribute to an InterfacememberMember 833794017-Nov-11 21:58 
We can add the attribute WebService and WebMethodAttribute to an interface just like WCF [ServiceContract,OperationContract] and this has been introduced in ASP.net 2.0.
QuestionQuestionmemberNGLara5-Oct-11 8:08 
Hi Yatin,

My name is Noel Lara and I am working as a Programmer using PowerBuilder, but since last year I started learning SL/C#, ASP.NET/MVC with VS2010.
I'd like to congratulate you for you good article about "What's the Difference between WCF and Web Services?"
It makes more sense to me now because of the idea you have described in it.
I am a big fan of web services may it be asp.net web service or wcf or wcf ria service, that is why I want to learn it deeply.
I just have few question regarding the wcf which was not tuckled on the article.
 
My question is, can a java program or php program call this wcf web service hosted from an iis server ? or this is just intended to be called by ms language such as vb and c# ?
I hope you could get a chance to reply though I know you are too busy, and I hope this won't be the last time we get in touch with each other.

Best Regards,

Noel
AnswerRe: QuestionmemberJaydeep Jadav5-Mar-13 20:14 
Web Service itself used for exposing the data for other. So you can use WCF or ASMX service from any languages.
 
You just need to make SOAP request XML and POST it to asmx service. Even also you can consume WCF like this if WCF configured for WebHttpBinding.

GeneralMy vote of 5membermilesganesh19-May-11 3:11 
Excellent
GeneralMy vote of 1memberchetanRock17-May-11 20:22 
msdn provides better than your information.
GeneralRe: My vote of 1memberDamith Gunawardana13-Jul-11 6:15 
mate you should have to stick to MSDN then than discouraging other people.
 
thumbs up for the aticle. simple and easy to understand. keep up the good work .
GeneralMy vote of 4memberDazManCat20-Jan-11 6:01 
Short and sweet definition as to the difference between Webservices & WCF
QuestionNew zip file please?memberfpmorrison29-Dec-10 2:41 
The zip file of the code example is marked invalid on the web site.
 
The error is clearly visible when you click on the "Browse Code" tab at
 
http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=139787[^]
 
Please replace it by uploading a new, valid copy of the source code.
GeneralMy vote of 5membersjelen28-Dec-10 0:18 
Good article for beginners but a bit short. Hope this is a beginning of a series.
GeneralMy vote of 5memberSunasara Imdadhusen27-Dec-10 21:25 
Excellent!
GeneralMy vote of 5memberSandeep Ramani27-Dec-10 20:06 
Hi yatin , Nice one ...
GeneralMy vote of 5memberMember 313498027-Dec-10 18:31 
Very Nice explanation !
I wanted to know if WCF services will support the compression of the data being transferred as it is done in web services.
I have used ClientSoapExtensions.dll, ServersoapExtension.dll, NzipLib.dll and Nzipper.dll to achieve compression and decompression in soap web services.
 
Thanks and regards
Mohan Chug
mohan.chug@gmail.com
GeneralRe: My vote of 5memberSayYatin28-Dec-10 17:26 
Hi Mohan,
 
There is no built in compression in WCF.You can use Binary Encoder, which is optimized for WCF to WCF communication. Ofcourse, using the Binary Encoder means it is not interoperable. You get the binary encoder by default in the NetTcpBinding.
 
One more option is you can use the GzipEncoderSample which is part of microsoft SDK samples to see how to write a custom encoder to enable something like this.
 
Thanks,
yatin patil
Generalits goodmemberPranay Rana26-Dec-10 18:42 
its good than other article i read about this topic -- but its old --- get 4 form me
For any question : http://pranayamr.blogspot.com/
 
vote my article :

Learn SQL to LINQ ( Visual Representation )


Calling WCF Services using jQuery

GeneralMy vote of 3memberKeith Barrow26-Dec-10 5:01 
The article is a little bit light. A side by side treatment of the difference between the asmx and WCF in the article would provide better depth, as would a slightly more detailed introduction to the some of various flavours of WCF. services
GeneralZIP File is invalidmemberfpmorrison26-Dec-10 3:03 
After downloading the ZIP file, when I try to unzip it with 64-bit Windows 7 Ultimate, I get the following error message:
 
The compressed (zip) folder is empty.
 
I've downloaded the file using both IE 8 (patched through December "patch Tuesday") and Firefox 3.6.13 (the latest) but the error occurs no matter which browser I use.
 
I'd suggest that the author of the article should reupload the ZIP file.
GeneralRe: ZIP File is invalidmemberYatin V Patil26-Dec-10 4:45 
thanks for ur valuable feedback...same zip folder i can open from my laptop..i dnt know wats the issue...neways i have uploaded pdf file now..Smile | :)
GeneralRe: ZIP File is invalidmemberfpmorrison26-Dec-10 13:57 
If you click on the "Browse Code" tab at the top of the article, you'll see the following error:
 
WCFVSWebService.zip [file error]
  
Which means the zip file is still "broken" and needs to be reuploaded.
GeneralMy vote of 4memberMario Majcica26-Dec-10 2:09 
Nice article. 4 because you could add just 1% more of effort by formating properly the document for an 5! Please format it or let me or somebody else helps you with that task. Thanks for sharing!
GeneralNot EnoughmemberJohn Simmons / outlaw programmer26-Dec-10 1:59 
This is a beginners article, and as such, should go a bit deeper into the mechanics of the two different kinds of web service approaches, as well as when you would use one rather than another. Further, your formatting is non-standard, and your code snippets are not properly enclosed in <pre> tags. I would have fixed the formatting for you without mentioning it, but you've disabled editing on the article.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

GeneralRe: Not EnoughmemberYatin V Patil26-Dec-10 4:48 
hey did few formatting changes on the article....this was my first article so was not knowing much about formatting rules..will take care next time... i have removed editing restrictions from the article you can help by editing this article..thanks ...
GeneralMy vote of 5memberRaviRanjankr26-Dec-10 1:27 
Good Article.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 26 Dec 2010
Article Copyright 2010 by Yatin V Patil
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid