TechBubbles

WCF vs ASP.NET Web services

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.

Related Posts:

45 comments

45 Comments so far

  1. Bose Thirumalai April 2nd, 2009 3:27 pm

    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

  2. Darlingpretty April 22nd, 2009 11:54 am

    you also define SOA and WCF.

  3. Abhay May 11th, 2009 3:23 pm

    This is really a crisp article about the difference between WCF and web services. Nice one.

  4. jegathees May 15th, 2009 8:01 am

    Very very nice one dear……

  5. Chella May 20th, 2009 6:10 pm

    Its realy very nice and understandable one

  6. sweeny May 22nd, 2009 6:10 pm

    simple explanation for the differences. easy to grasp

  7. EmpireOne May 27th, 2009 8:14 pm

    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?

  8. abhinav sinha May 29th, 2009 6:14 am

    Good article for understanding the difference WCF and web services

  9. Vikas Sharma June 1st, 2009 7:08 am

    Nice article….Really easy to understand…Thanks…

  10. Shiva Adabala June 12th, 2009 5:13 am

    Hi Kalyan,

    Nice one. Able to understand very simple manner. Thanks

    Sams.

  11. Kamal Raturi July 8th, 2009 6:00 am

    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

  12. Sid July 22nd, 2009 2:05 pm

    very god article. Brief and Easy to understand.

  13. HariHaraDeep July 28th, 2009 1:32 pm

    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.

  14. harsh August 23rd, 2009 6:29 am

    thanks a lot

  15. Niranjan September 3rd, 2009 9:56 am

    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.

  16. Prasanna September 4th, 2009 7:11 pm

    Thank you so much for posting this article. It is simply awesome and very easy to understand!!

  17. Kalyan Bandarupalli September 27th, 2009 11:16 pm

    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

  18. [...] Difference between ASP.NET Web Service and WCF [...]

  19. Vikash Sharma January 28th, 2010 1:38 pm

    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

  20. Wilson January 29th, 2010 1:54 am

    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,

  21. gitanjali January 31st, 2010 12:57 pm

    Simply nice and very easy to understand!

  22. Lakshmi February 1st, 2010 4:55 am

    This article is really helpful..and thanks for the nice explanation!

  23. Parag February 3rd, 2010 6:11 pm

    I was searching for this one. Very nice and crisp explanation. Thank you.

  24. Rajasekaran R February 5th, 2010 12:50 pm

    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.

  25. Eldhose February 10th, 2010 7:23 am

    Very simple to understand. Thanks

  26. Chalapathi February 11th, 2010 11:45 am

    Very nice.

    yours old coolgue chalapathi

  27. Shaik Nazeer Hussain February 19th, 2010 10:10 am

    This is really cool dude.

  28. jitendra February 20th, 2010 8:22 am

    i want to more about WCF as i am new to it also in simple language

  29. jitendra February 20th, 2010 8:24 am

    i want to know more about WCF also in simple language as i am new to WCF

  30. Ahmed March 4th, 2010 10:21 pm

    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

  31. kailash March 23rd, 2010 4:01 am

    Hi Kalyan,

    Really a good article for develper bench. Keep it up to helping developers.

    Thanks,
    Kailash

  32. Prince March 25th, 2010 8:07 am

    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

  33. Vimal April 4th, 2010 8:34 am

    Really great. It is very useful for understanding the difference wcf and webservice

  34. jaivardhan joshi April 21st, 2010 11:39 am

    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.

  35. vikram May 7th, 2010 12:11 pm

    good comparison. thnx for info

  36. Ajander Singh May 27th, 2010 6:16 pm

    Good work Kalyan. it very useful to understand the difference between WCF and Web Service.

    Cheers!!!!!!

  37. Rahul May 28th, 2010 6:08 am

    Realy a nice one but good at diffrence lavel only not practicaly…..But nicely written…

    Good one..

    Thanks

  38. Mayur June 14th, 2010 6:57 am

    Thanks kalyan, it was really important differances. useful for understanding.

    Thanks,
    Mayur

  39. Suresh kumar July 5th, 2010 6:09 am

    Really nice one, you should have included some notable points supporting why we should apt for WCF service over the traditional webservices

  40. Hristo July 20th, 2010 10:06 am

    “XMLSerialization does not indicate the which fields or properties of the type are serialized into XML”

    That’s not true. You have [XmlIgnore].

  41. Archana July 23rd, 2010 9:12 pm

    Thanks much, it helped me alot!!

  42. Hitesh Agja July 31st, 2010 7:56 pm

    Really nice article. It’s a first step towards WCF learning. This article encourages to learn WCF. Nice work !

  43. Jaydev August 2nd, 2010 7:31 am

    Hi Kalyan, Is it possible to use return data type as a dataset or datatable in WCF ? Like in webservice its not possible.

    thanks

  44. Muralidharan August 11th, 2010 8:01 am

    Thanks boss.Easy to understand for difference b/w ASP.NET web service and wcf Service.

  45. Lynn September 1st, 2010 12:35 am

    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?

Leave a reply