Click here to Skip to main content
Click here to Skip to main content
Go to top

What's the Difference between WCF and Web Services?

, 26 Dec 2010
Rate this:
Please Sign up or sign in to vote.
Difference between ASP.NET web service and programming WCF services like ASP.NET web services

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)

Share

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

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalSibeesh Venu1-Aug-14 21:35 
QuestionTypo / missing word Pinmember_Noctis_13-May-14 18:02 
SuggestionMy Vote of 5 with a slight disagreement PinmemberMember 884486023-Mar-14 23:41 
GeneralMy vote is 4 PinmemberAshish Ramteke21-Oct-13 6:44 
GeneralMy vote of 4 PinmemberHifni Shahzard10-Jul-13 23:08 
Questionnice article PinmemberMember 958150921-May-13 21:29 
GeneralMy vote of 4 PinprofessionalMohammed Hameed16-May-13 9:55 
GeneralMy vote of 5 PinmemberM Rayhan9-May-13 20:39 
GeneralMy vote of 5 PinmemberAbhinesh M6-May-13 2:45 
GeneralMy vote of 5 PinmemberNeonMika26-Mar-13 9:47 
QuestionTo summarize.. [modified] Pinmemberdevvvy30-Jan-13 16:48 
QuestionDifference between WCF, .Net Remoting and Web Services Pinmemberetechpulse8-Jan-13 20:05 
AnswerRe: Difference between WCF, .Net Remoting and Web Services PinmemberRasadul Alam Rashed28-Jan-13 23:07 
Questiongreat PinmemberJaafar Abu Sair16-Dec-12 21:13 
GeneralMy vote of 5 PinmemberSavalia Manoj M26-Nov-12 17:00 
GeneralMy vote of 4 PinmemberChamila Ranasinghe19-Nov-12 22:58 
GeneralMy vote of 5 PinmemberMember 307459812-Oct-12 5:23 
Questiongood article PinmemberElanchezian24-Sep-12 3:04 
Generalgood article Pinmembervishal.shimpi16-Sep-12 18:32 
GeneralMy vote of 5 PinmemberFarhan Ghumra22-Aug-12 23:01 
Generalexcellent PinmemberinitCode28-May-12 21:32 
GeneralExcellent Article ...Patil..! PinmemberTushar_Patil8-May-12 20:59 
GeneralMy vote of 5 Pinmembersujatapant1526-Apr-12 1:41 
GeneralMy vote of 4 PinmemberMember 877148431-Mar-12 1:37 
Questiongood work Pinmemberyazanjaradat27-Mar-12 4:29 
GeneralMy vote of 5 PinmemberReza Ahmadi17-Mar-12 1:34 
QuestionConcurrency and Sessions PinmvpDave Kerr7-Mar-12 2:40 
QuestionGood Article if you want concise 14 differences between WCF service and webservice PinmemberManishav33886-Mar-12 21:49 
QuestionGood article. Pinmemberrohit_k..27-Feb-12 19:41 
AnswerRe: Good article. PinmemberManishav33886-Mar-12 4:10 
GeneralMy vote of 4 Pinmembercoded0078-Jan-12 23:15 
GeneralMy vote of 4 PinmemberNarayan D Mali3-Jan-12 1:29 
QuestionWell Done PinmentorDave Kerr31-Dec-11 20:41 
GeneralAdding the attribute WebService and WebMethodAttribute to an Interface PinmemberMember 833794017-Nov-11 21:58 
QuestionQuestion PinmemberNGLara5-Oct-11 8:08 
AnswerRe: Question PinmemberJaydeep Jadav5-Mar-13 20:14 
GeneralMy vote of 5 Pinmembermilesganesh19-May-11 3:11 
GeneralMy vote of 1 PinmemberchetanRock17-May-11 20:22 
GeneralRe: My vote of 1 PinmemberDamith Gunawardana13-Jul-11 6:15 
GeneralMy vote of 4 PinmemberDazManCat20-Jan-11 6:01 
QuestionNew zip file please? Pinmemberfpmorrison29-Dec-10 2:41 
GeneralMy vote of 5 Pinmembersjelen28-Dec-10 0:18 
GeneralMy vote of 5 PinmemberSunasara Imdadhusen27-Dec-10 21:25 
GeneralMy vote of 5 PinmemberSandeep Ramani27-Dec-10 20:06 
GeneralMy vote of 5 PinmemberMember 313498027-Dec-10 18:31 
GeneralRe: My vote of 5 PinmemberSayYatin28-Dec-10 17:26 
Generalits good PinmemberPranay Rana26-Dec-10 18:42 
GeneralMy vote of 3 PinmemberKeith Barrow26-Dec-10 5:01 
GeneralZIP File is invalid Pinmemberfpmorrison26-Dec-10 3:03 
GeneralRe: ZIP File is invalid PinmemberYatin V Patil26-Dec-10 4:45 

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
Web03 | 2.8.140916.1 | Last Updated 26 Dec 2010
Article Copyright 2010 by Yatin V Patil
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid