Wednesday, April 21, 2010

Difference between web services and WCF

These days all the projects that are upgrading from .net 2.0 to .net 3.5 or to .net 4.0 are facing the issue of choosing between web-services and WCF (Windows Communication Foundation). In this post I am going to discuss the difference between them and look at points which would make clear what to choose and throw light to web-service vs WCF issue. For Best Practices in WCF read my another post on the topic. For difference between various types of bindings in WCF read this post.

WCF Definition

WCF is a part of the .NET Framework that provides a unified programming model for rapidly building service-oriented applications that communicate across the web and the enterprise.

The following figure shows the features that the WCF provides compared to other technology in .net:


ASP.NET Web services was developed for building applications that send and receive messages by using the Simple Object Access Protocol (SOAP) over HTTP. The structure of the messages can be defined using an XML Schema, and a tool is provided to facilitate serializing the messages to and from .NET Framework objects. The technology can automatically generate metadata to describe Web services in the Web Services Description Language (WSDL), and a second tool is provided for generating clients for Web services from the WSDL.

WCF is for enabling .NET Framework applications to exchange messages with other software entities. SOAP is used by default, but the messages can be in any format, and conveyed by using any transport protocol. The structure of the messages can be defined using an XML Schema, and there are various options for serializing the messages to and from .NET Framework objects. WCF can automatically generate metadata to describe applications built using the technology in WSDL, and it also provides a tool for generating clients for those applications from the WSDL.

There are different points to consider for WCF:
1.    WCF is architecturally more robust and promotes best practices.
2.    If you know what you are doing its "silky smooth" if not you are in for a ride.
3.    Do you have enough time to complete the conversion of your services?
Two further aspects:
1) No matter how you decide this for the server-side, you can easily consume Webservices and WCF Services using only WCF on the client-side. This is of value, if you consume multiple services with a single client.
2) If you consider Cloud Computing: It is possible to host WCF Services on Windows Azure.

Finally I would say that, if you have the time, the bling and the muscle to do the upgrade. Its worth it. If asmx is satisfying all the needs, you may persist with web services.

ASMX is great and simple - but it's very limited in many ways:
·         you can only host your web services in IIS
·         you can only reach your web services over HTTP
·         security is very limited

WCF remedies this - and offer much more beyond that. You can host your WCF services in IIS - or self-host in a console app or Win NT Service, as need be. You can connect your WCF services using HTTP, TCP/IP, MSMQ, Peer-to-peer protocols, named pipes for on-machine communications and much more.
I'd definitely recommend you go with WCF. It's a tad more complex than ASMX, but it also offer just sooo much more capabilities and choices!





Major difference between the two is that Web Services use XmlSerializer but WCF uses DataContractSerializer which is better in performance as compared to XmlSerializer. Some key issues with XmlSerializer to serialize .NET types to XML are:
* 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.

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 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.








Quick benefits of WCF over Web-Services (ASMX):
1) For internal (behind firewall) service-to-service calls we use the net:tcp binding, which is much faster than SOAP
2) We enabled both a net:tcp endpoint and a "web" endpoint on the same service with only a configuration file update (no code changes)
3) We were able to create AJAX-supporting RESTful web services with only configuration changes and using the DataContractJsonSerializer that's already built in. To do this otherwise, we would have had to write an HTTP Handler (ashx) and handle most of the Json serialization and url parsing by hand.
4) As our site needs to scale for performance optimization and stability, we are looking at converting to using an MSMQ-based messaging structure that is asynchronous AND guaranteed and participates in transactions; WCF provides an MSMQ bindng that requires little-to-no code change in our services--just reference updates and setting up MSMQ properly with the existing services (and adding attributes for Transactional boundaries).

BUT BE WARNED: Really invest in learning this. There are things like argument-name-changes during development that actually don't break the service references but result in null arguments being passed (built-in version skew handling), hosting models to consider (Windows Service vs. IIS), and instantiation models and FaultExceptions to all REALLY understand. We didn't going in and we had some pains. But we plowed ahead and are VERY happy with our learnings and the flexibility and growth opportunities we have not being tied to ASMX anymore!



Comparison of performance between WCF and Web-Services

WCF is 25%—50% faster than ASP.NET Web Services, and approximately 25% faster than .NET Remoting. The following figure would give a clear picture. It's comparison between the two over the throughput time taken for message. The message objects are taken of three samples i.e. 1 , 10 and 100 objects per message. Over here we clearly see that WCF is fast and perform better than Web-Services and thus WCF wins the battle WCF vs Web Services.






Migrating ASP.NET Web Services to WCF

Migration is a key issue in existing applications and many a times we get confused as to what to do and how to in migration related tasks. Some of the key points of the MSDN migration article are as follows:

WCF has several important advantages relative to ASP.NET Web services. While ASP.NET Web services tools are solely for building Web services, WCF provides tools that can be used when software entities must be made to communicate with one another. This will reduce the number of technologies that developers are required to know in order to accommodate different software communication scenarios, which in turn will reduce the cost of software development resources, as well as the time to complete software development projects.

Even for Web service development projects, WCF supports more Web service protocols than ASP.NET Web services support. These additional protocols provide for more sophisticated solutions involving, amongst other things, reliable sessions and transactions.

WCF supports more protocols for transporting messages than ASP.NET Web services. ASP.NET Web services only support sending messages by using the Hypertext Transfer Protocol (HTTP). WCF supports sending messages by using HTTP, as well as the Transmission Control Protocol (TCP), named pipes, and Microsoft Message Queuing (MSMQ). More important, WCF can be extended to support additional transport protocols. Therefore, software developed using WCF can be adapted to work together with a wider variety of other software, thereby increasing the potential return on the investment.

WCF provides much richer facilities for deploying and managing applications than ASP.NET Web services provides. In addition to a configuration system, which ASP.NET also has, WCF offers a configuration editor, activity tracing from senders to receivers and back through any number of intermediaries, a trace viewer, message logging, a vast number of performance counters, and support for Windows Management Instrumentation.




Given these potential benefits of WCF relative to ASP.NET Web services, if you are using, or are considering using ASP.NET Web services you have several options:
  • Continue to use ASP.NET Web services, and forego the benefits proffered by WCF.
  • Keep using ASP.NET Web services with the intention of adopting WCF at some time in the future. The topics in this section explain how to maximize the prospects for being able to use new ASP.NET Web service applications together with future WCF applications. The topics in this section also explain how to build new ASP.NET Web services so as to make it easier to migrate them to WCF. However, if securing the services is important, or reliability or transaction assurances are required, or if custom management facilities will have to be constructed, then it is a better option to adopt WCF. WCF is designed for precisely such scenarios.
  • Adopt WCF for new development, while continuing to maintain your existing ASP.NET Web service applications. This choice is very likely the optimal one. It yields the benefits of WCF, while sparing the cost of modifying the existing applications to use it. In this scenario, new WCF applications can co-exist with existing ASP.NET applications. New WCF applications will be able to use existing ASP.NET Web services, and WCF can be used to program new operational capabilities into existing ASP.NET applications by virtue of WCF ASP.NET compatibility mode.
  • Adopt WCF and migrate existing ASP.NET Web service applications to WCF. You may choose this option to enhance the existing applications with features provided by WCF, or to reproduce the functionality of existing ASP.NET Web services within new, more powerful WCF applications.



Description of Bindings in WCF

System-provided bindings are used to specify the transport protocols, encoding, and security details required for clients and services to communicate with each other. The system-provided WCF bindings are listed in the table. More details on the bindings can be found in the WCF documentation. WCF also allows you to define your own custom bindings.

BindingsDescriptions
BasicHttpBindingA binding that is suitable for communication with WS-Basic Profile conformant Web Services like ASMX-based services. This binding uses HTTP as the transport and Text/XML as the message encoding.
WSHttpBindingA secure and interoperable binding that is suitable for non-duplex service contracts.
WSDualHttpBindingA secure and interoperable binding that is suitable for duplex service contracts or communication through SOAP intermediaries.
WSFederationHttpBindingA secure and interoperable binding that supports the WS-Federation protocol, enabling organizations that are in a federation to efficiently authenticate and authorize users.
NetTcpBindingA secure and optimized binding suitable for cross-machine communication between WCF applications
NetNamedPipeBindingA secure, reliable, optimized binding that is suitable for on-machine communication between WCF applications.
NetMsmqBindingA queued binding that is suitable for cross-machine communication between WCF applications.
NetPeerTcpBindingA binding that enables secure, multi-machine communication.
MsmqIntegrationBindingA binding that is suitable for cross-machine communication between a WCF application and existing MSMQ applications.

Tutorials

For more on how to create a WCF service refer the following:



On how to host WCF service on IIS refer the following:



More Study...

For more study on WCF there's the MSDN WCF Developer Center which has everything from beginner's tutorials to articles and sample code. For further reading on WCF refer to MSDN article on WCF in .net 4.0


The standards supported by ASP.NET Web services are documented in XML Web Services Created Using ASP.NET. The more extensive list of standards supported by WCF are listed at Web Services Protocols Supported by System-Provided Interoperability Bindings.


For cloud computing and Microsoft Technologies refer my Cloud Computing and .Net article.

For parallel computing and .net refer my Parallel Computing and .Net article.

For more detailed performance comparison refer to this MSDN article.

If you are considering for migration from Web Services to WCF refer to this MSDN article. Also to avoid migration in future of Asp.Net application refer to this MSDN article.

Also read this very informative MSDN article explaining in details the differences regarding development of web-services and WCF.


Do keep me updated with your views and suggestions on the article....

59 comments:

  1. Quite a nice post... it helped me a lot to understand the issues related to migration from web-services to WCF.

    ReplyDelete
  2. Good post. I would further like to share the following definition of WCF:

    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.

    ReplyDelete
  3. What an article!!
    Thanks dude

    ReplyDelete
  4. All information at one place, cool :-)

    ReplyDelete
  5. Thanks all for your appreciation and support !

    ReplyDelete
  6. Thanks dude for the updates and bringing out the differences between them. Surely by reading this article many would be enlightened !

    Excellent work...

    ReplyDelete
  7. Good article... and well supported by the videos... everything at one place. Thanks !!!

    ReplyDelete
  8. nice artical !!!!!!!1

    ReplyDelete
  9. Really good one and good blog
    http://dotnetwithexamples.blogspot.com

    ReplyDelete
  10. Very nice article for beginners. Thanks for efforts.

    ReplyDelete
  11. Good article with well explained videos. It really helped me a lot in understanding WCF and now i am comfortable in migrating from web-services to WCF. Thanks Jaivardhan Joshi.

    ReplyDelete
  12. good post, i guess migrate from web service to wcf is like updating from xp to vista/7

    xp will do the work, but vista/7 is the premier version

    ReplyDelete
  13. Nice article. Currently I have seen that many are still sticking with Web-Services instead of migrating to WCF. The reason for it could be that they are still comfortable with it and all their requirements are met with Web-Services.

    ReplyDelete
  14. Web-services are going to fade out as time passes... by 2012 they would be completely out of market and WCF will rule. Interesting thing to see is what replaces WCF !

    ReplyDelete
  15. Lacks originality...

    ReplyDelete
  16. IS there a good book that will have practical real world examples?

    ReplyDelete
  17. great article thank u very much

    ReplyDelete
  18. thanks..it a good article

    ReplyDelete
  19. Good article. Thanks for taking your time to help other people. I am a developer also but just can't seem to find enough time to do what you are doing.
    Thanks

    ReplyDelete
  20. Good article. It helped me understand the difference between web-services and wcf, due to which I was able to clear the technical interview rounds at Infosys. Thanks Jai :)

    ReplyDelete
  21. Thanks for all your appreciation and support !

    ReplyDelete
  22. very good to clear the difference between wcf and web service

    ReplyDelete
  23. I notice an error about json:
    Standard asmx webservice can easily support both json and soap at the same time.
    But, yes you need to rebuild your sourcecode instead of doing a configuration change.

    If you declare:
    [WebService]
    [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {
    then your webservice will support both json and xml(soap) depending of the request received.

    And you can configure which property are serialized in json/XML with [XmlIgnore].
    You can even do this at runtime with the unknown syntax: ShouldSerializeX (google it)

    ReplyDelete
  24. Can WCF web services be self hosted so that they don't need IIS and yet provide the same interface and behave in same way as asmx web services?

    ReplyDelete
  25. Nice Article..
    Got much information @ one place..

    ReplyDelete
  26. Yes it is possible to host WCF web services as self hosted that do not use IIS. For more read this article.
    http://msdn.microsoft.com/en-us/library/ms730158.aspx

    ReplyDelete
  27. Muy interesante y completo tu post..Congratulations!!!!

    ReplyDelete
  28. Very simple and clear article.. Thanks.

    ReplyDelete
  29. Thanks Jai,

    It is really useful article for understanding difference between Web Service and WCF.

    ReplyDelete
  30. Well, glad to see this informative post.. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic.

    ReplyDelete
  31. Excellent post bringing out the minute features of WCF and its perspective with web-services.
    Thanks Jai.

    ReplyDelete
  32. Very good article Jai

    ReplyDelete
  33. Excellent article and well supported by videos.

    ReplyDelete
  34. Excellent stuff - thanks heaps!!!

    ReplyDelete
  35. Very good Article.

    ReplyDelete
  36. quite informative... thanks a lot

    ReplyDelete
  37. good one.. really summarizing one.. for webservice and wcf

    ReplyDelete
  38. Gr8 work guy.. Very nice..!!

    ReplyDelete
  39. great article. very informative. good work!

    ReplyDelete
  40. it will be understandable and nice.

    ReplyDelete
  41. Great information on asp.net to wcf migration. Thanks for sharing.

    ReplyDelete
  42. Hi, nice Difference between web services and WCF .Thanks for your help..

    -Aparna
    Theosoft

    ReplyDelete
  43. Providing effective Website Development Dubai web design to improve the customers for websites. Business people develop websites for world wide business. Giving impressible graphics to website full fill the customer needs.

    ReplyDelete
  44. thanks for your valuable posting it was very informative. Anar .NET Solutions

    ReplyDelete
  45. I am glad to find your impressive way of writing the post.Thanks for sharing the post.i'm sharing your information to all friends.If you
    Want more details kindly click here

    ReplyDelete
  46. Great Work. This post is worth everyone’s attention. web design company in chennai

    ReplyDelete
  47. Informative article, just what I was looking for.seo services chennai

    ReplyDelete

Comments to this post

LinkWithin

Related Posts with Thumbnails