Introduction
In this post I will explain the Difference between ASP.NET web service and programming WCF services like ASP.NET web services. It also discusses how we use the both technologies for developing the web services.
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 can not be serialized.
The WCF uses the DataContractAttribute and DataMemeberAttribute to translate .NET FW types in to 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 XMLserialization.
- XMLSerialization does not indicate the which fields or properties of the type are serialized into XML where as DataCotratSerializer Explicitly shows the 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 a 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 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 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.
As several people asking me the WCF benefits over web services, I outlined the points here
1.WCF supports more of WS-* standards than web services
2.As I mentioned in the article WCF supports multiple bindings HTTP,TCP,MSMQ,WS-HTTP etc where as web service supports only HTTP.
3.WCF can maintain transactions like COM+
4.It has JSON integration
5.It can be hosted on IIS,WAS, Self hosting and windows services
6.Web services have no instance management ie you can not have a singleton web service or session full webservice
Hi Kalyan
This article was really cool and it was briefly explained.
Thanks for providing such an explanation.
I was searching for the difference between WCF & Web Service for very long time and now i am able to identify the difference.
Thanks dude
you also define SOA and WCF.
This is really a crisp article about the difference between WCF and web services. Nice one.
Very very nice one dear……
Its realy very nice and understandable one
simple explanation for the differences. easy to grasp
I have done both and the WCF folder structures and dependence on the setup wizards are a nightmare if you make any mistakes. WCF is not worth the extreme learning curve you must make to follow verses the simple ASP WebService structure. Why do we make things so complicated?
Good article for understanding the difference WCF and web services
Nice article….Really easy to understand…Thanks…
Hi Kalyan,
Nice one. Able to understand very simple manner. Thanks
Sams.
Hi Kalyan,
U have written a nice article. I hope your articles will be helping devlopers in future. It will be appriciated if u present something on .Net Remoting vs(OR in) WCF.
thanks
very god article. Brief and Easy to understand.
Did you know you can also take advantage of Windows Communication Foundation in conjunction with SharePoint? We start off by reviewing the SharePoint APIs (the SharePoint Object Model and the SharePoint Web Services) before introducing WCF. You will learn how to host you WCF services and also how to use SharePoint as a WCF host. At the end of this session you will know when to use the out of the box SharePoint APIs and when to use WCF to accomplish the right task.
thanks a lot
This is really very nice article to understand difference beween WebService and WCF.
Here i have one confusion in issue with XML serializer. According to this article:
â– Only the classes which implement IEnumerable interface.
But i think the collection class that does not implement iEnumerable can not be serialized using xmlSerializer. Normal class can be serialized using xml serializer.
Thank you so much for posting this article. It is simply awesome and very easy to understand!!
To all
amodttu@yahoo.com asked me about benefits of WCF over Web services. Here they are
1.WCF supports more of WS-* standards than web services
2.As I mentioned in the article WCF supports multiple bindings HTTP,TCP,MSMQ,WS-HTTP etc where as web service supports only HTTP.
3.WCF can maintain transactions like COM+
4.It has JSON integration
5.It can be hosted on IIS,WAS, Self hosting and windows services
6.Web services have no instance management ie you can not have a singleton web service or session full webservice
[…] Difference between ASP.NET Web Service and WCF […]
Hi,
Thx for the article.
Could you please provide more details on some practicall scenarios where we would go for WCF over Webservices?
Thanks,
Vikash
this a really cool article….. looks like there are still people like myself catching up with WCF… so is WCF first introduced in VS 2008?
cheers,
Simply nice and very easy to understand!
This article is really helpful..and thanks for the nice explanation!
I was searching for this one. Very nice and crisp explanation. Thank you.
Hi,
It’s a good article for the diferences. Can you tell me some pratical scenarios where we would go for WCF over Webservices?
Thanks in Advance.
Very simple to understand. Thanks
Very nice.
yours old coolgue chalapathi
This is really cool dude.
i want to more about WCF as i am new to it also in simple language
i want to know more about WCF also in simple language as i am new to WCF
This is also a good article on differences between wcf web services and dotnet remoting
http://www.codeproject.com/KB/WCF/WCF_Web_Service_Remoting.aspx
Hi Kalyan,
Really a good article for develper bench. Keep it up to helping developers.
Thanks,
Kailash
Hi Kalyan,
Very nice information and its a simple explaintion too..
Could you confirm one of the points … “where as web service supports only HTTP”.
TCP binding/call is possible in ASP webservice.. here below is the url for example..
http://msdn.microsoft.com/en-us/library/aa529311.aspx
Thanks,
Prince
Really great. It is very useful for understanding the difference wcf and webservice
A good link explaining the difference between WCF and We-services is
http://jai-on-asp.blogspot.com/2010/04/difference-between-web-services-and-wcf.html
Hope this helps.
good comparison. thnx for info
Good work Kalyan. it very useful to understand the difference between WCF and Web Service.
Cheers!!!!!!
Realy a nice one but good at diffrence lavel only not practicaly…..But nicely written…
Good one..
Thanks
Thanks kalyan, it was really important differances. useful for understanding.
Thanks,
Mayur
Really nice one, you should have included some notable points supporting why we should apt for WCF service over the traditional webservices
Just updated the article for your comments. Now you can see these points at the end of the article.
“XMLSerialization does not indicate the which fields or properties of the type are serialized into XML”
That’s not true. You have [XmlIgnore].
Thanks much, it helped me alot!!
Really nice article. It’s a first step towards WCF learning. This article encourages to learn WCF. Nice work !
Hi Kalyan, Is it possible to use return data type as a dataset or datatable in WCF ? Like in webservice its not possible.
thanks
Please follow this link for your question
http://stackoverflow.com/questions/12702/net-returning-datatables-in-wcf
Thanks boss.Easy to understand for difference b/w ASP.NET web service and wcf Service.
I really liked your article. It seems though that WCF is very complicated whereas web services are pretty straight forward.
Can you give an example of when you would choose to use WCF over a webservice? Why would all of the additional overhead be necessary?
hi..this is a very useful content…keep writing..
Thanks
Simple and clear. Very helpful article. Thank you.
I really liked your article. It seems though that WCF is very complicated whereas web services are pretty straight forward.
Can you give an example of when you would choose to use WCF over a webservice? Why would all of the additional overhead be necessary
Hi Kalyan,
Can you put an article on Configuring the WCF Service behaviour either in the Configuration file or at runtime.
Thanks,
laxmi.
Hi
Please follow this link http://msdn.microsoft.com/en-us/library/ms734765.aspx for configuration settings. Hope it answers your question.
Regards
Kalyan
Simple and clear. Very helpful article. Thank you.
Nice Article. Brief and spot on.
This is really a very good and crisp information. Thank u.
it’s nice really nice article helped me lot for my interviews.
Hello Kalyan,
Very Informative article. I have developing webservice for iphone app using asp.net.
We are able to host on local server.
Please let me know on how we can host the wcf web service on hosting server like godaddy.com etc.
Please let me know and help me.
Thanks in advance
-Damodar
Hi
Please read http://www.edoverip.com/edoverip/index.php/2009/01/30/running-wcf-on-godaddy/
post and let me know if you get any specific error.
Regards
Kalyan
The article is good technically, i don’t see any conceptual differences and IMHO its much needed as well