You are on page 1of 10

Article Browse Code Stats Revisions (5) By Yatin V Patil, 26 Dec 2010 Alternatives 4.

71 (51 votes) Comments & Discussions (41)

What's the Difference between WCF and Web Ser

Download source - 161.38 KB

Introduction

In this article, I will explain the difference between ASP.NET web service and WCF services like ASP.NET 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 W the Simple Object Access Protocol (SOAP). SOAP is a W3C submitted note (as of May 2000) that uses st 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 progr language used to implement the service; they only need to understand how to send and receive SOAP m and XML).

WCF Service

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. can send data as asynchronous messages from one service endpoint to another. A service endpoint can continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint a service that requests data from a service endpoint. The messages can be as simple as a single charact 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 presentatio 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 tha traditionally considered as "web services".

WCF "web services" are part of a much broader spectrum of remote communication enabled through W a much higher degree of flexibility and portability doing things in WCF than through traditional ASMX b designed, from the ground up, to summarize all of the different distributed programming infrastructure Microsoft. An endpoint in WCF can be communicated with just as easily over SOAP/XML as it can over change this medium is simply a configuration file mod. In theory, this reduces the amount of new code 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 WC logically group together all the different ways of getting two apps to communicate in the world of Micr 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 flexibl services can be hosted in different types of applications. Common scenarios for hosting WCF services a hosting, Managed Windows Service.

The major difference is that Web Services Use XmlSerializer. But WCF Uses DataContractSerial 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 overXmlser XML Serialization does not indicate which fields or properties of the type are serialized into XML

whereasDataCotractSerializer 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 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 typ

[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 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 se XML Serialization does not indicate which fields or properties of the type are serialized into XML whereasDataContractSerializer 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 andWebMetho 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 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 .asm code for the service. The service file is copied into the root of the ASP.NET application and Assemblyw 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 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 andMessageBodyMemberAttr 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 servic WSDL as a response to the request.

The generated WSDL can be customized by deriving the class of ServiceDescriptionFormatExtens

Issuing a Request with the query WSDL for the .svc file generates the WSDL. The WSDL that generated b 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 settin 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 Li

About the Author

Yatin V Patil
Software Developer (Senior)Infosys Technologies Limited India Member Follow on Twitter I am working as a Technology Analyst at Infosys technologies,Pune. I used to work on

C#,ASP.net,WCF,WPF,Sharepoint,PHP,Perl.

Difference between WCF and Web service

Web service is a part of WCF. WCF offers much more flexibility and portability to develop a service when com

service. Still we are having more advantages over Web service, following table provides detailed difference b

Features
Hosting

Web Service
It can be hosted in IIS hosting, Windows service

WCF

It can be hosted in IIS, windows activati

Programming [WebService] attribute has to be added to the class Model Operation XML Encoding Transports Protocols [WebMethod] attribute represents the method exposed to client One-way, Request- Response are the different operations supported in web service System.Xml.serialization name space is used for serialization XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom Can be accessed through HTTP, TCP, Custom Security

[ServiceContraact] attribute has to be a exposed to client of operations supported in WCF serialization XML 1.0, MTOM, Binary, Custom

[OperationContract] attribute represent

One-Way, Request-Response, Duplex a

System.Runtime.Serialization namespac

Can be accessed through HTTP, TCP, Na MSMQ,P2P, Custom

Security, Reliable messaging, Transactio

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

Article Top

Sign Up to vote Poor

Comments and Discussions

You must Sign In to use this message board.


Go

Search this forum Profile popups

Spacing

Noise

Very High

Layout

Open All 10

First

My vote of 5
Very nice article and easy to understand.
Sign InView ThreadPermalink

Member 3074598

5:23 12

good article
good article,it is very useful for me, thank u, Elanchezian.A senior software developer
Sign InView ThreadPermalink

Elanchezian

3:04 24

good article
very good article... Vishal Shimpi, Software Developer

vishal.shimpi

18:32 16

Sign InView ThreadPermalink

My vote of 5
Excellent
Sign InView ThreadPermalink

Farhan Ghumra

23:01 22

excellent
nice job!!!
Sign InView ThreadPermalink

initCode

21:32 28

Excellent Article ...Patil..!


Easy To Understand Diff...Thanks For Article

Tushar_Patil

20:59 8

Tushar Patil Failure is not the end it is the time Required to get Success.

Sign InView ThreadPermalink

My vote of 5
Good Article
Sign InView ThreadPermalink

sujatapant15

1:41 26

My vote of 4
It's nice
Sign InView ThreadPermalink

Member 8771484

1:37 31

good work
thank you very much good explanation
Sign InView ThreadPermalink

yazanjaradat

4:29 27

My vote of 5
Good Article
Sign InView ThreadPermalink Last Visit: 18:00 31 Dec '99 Last Update: 17:42 13 Oct '12

Reza Ahmadi

1:34 17

Refresh

123

General

News

Suggestion

Question

Bug

Answer

Joke

Rant

Permalink | Advertise | Privacy | Mobile Web01 | 2.6.121012.1 | Last Updated 26 Dec 2010

Sr . N o 1

Features

Web Service

WCF

File Format/Exten sion Hosting

The File extension of web service is .asmx WebService can be hosted in IIS as well asWebService can hostedoutside of IIS. HTTP, TCP, Custom

The file extension of WCF service is .svc

It is flexible because it can be hosted in IIS, Windows Activation Services(WAS), Managed Windows Services and Self-Hosting HTTP, WS-HTTP, TCP, Custom, Named Pipes, MSMQ, P2P(Point to Point) etc Done throughDataContractSerializer

5 6 7

Transport Protocols/Bin ding Data Transformati on Serialization NameSpace Supported Operations Encoding

Done through XML serializer.

System.XML.Seriali System.RunTime.Serialization zation One-Way and One-Way, Request-Response and Duplex Request-Response XML1.0, MTOM XML1.0, MTOM, Binary (Message Transmission Optimization Mechanism), DIME (D irect Internet Message Encapsulation) WebMethods Uses WebMethods t Uses DataContractAttributesand DataMemb and o translate .Net FW erAttribute to translate .Net FW types in to DataContract types in to XML. XML. [WebService] [ServiceContract] attribute has to be has to be added to the added to the class class [OperationContract] attribute represents [WebMethod] the method exposed to the client. attribute represents the method exposed to the client. Messaging Uses It can send/receive message in any transport only SOAP(Simple protocol message format. By default it uses Object Access SOAP for communication.

1 0

Security

1 1 1 2

Performance Exception Handling

Protocol) Not much secured as compared to WCF. Cant protect the data between Server and Client. Using certificates can protect the data but it is complicated. Normally we use UserName/Password. Slower than WCF Unhandled Exceptions returns the client as SOAP faults. Hash Table cannot be serialized.

More secured thanWebServices. WCF does not need IIS to run, it can run as a System Service on the Server, using a command ambient. WCF is a service and not a Web Service.

1 3

Limitations

Better than WebService. The performance measures in terms of xml serialization. Unhandled Exceptions does not return to the to client as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to the Client for the purpose of debugging. The DataContractSerializer translate the Hash table into the XML.

Only public Public/Private properties/fields can be properties/fields can be serialized. serialized

You might also like