WCF vs ASMX WebServices


kick it on DotNetKicks.com

This question comes up a lot in conversations I have with developers. 

“Why would I want to switch to ASMX services?”

One analogy I have come up with to explain the difference between the two is an airplane analogy. 

I associate ASMX services with a Cessna 150 cockpit and I associate WCF services with a 747 Jumbo Jet cockpit. 

image

I’ll be honest, I’ve never flown a plane but I think I’m smart enough to crank a Cessna 150 if I had to figure it out.  The above screen shot is a real Cessna 150 cockpit.  There are a few gauges, some knobs, etc.  Really not that bad.  And I think this explains ASMX really well.  ASMX services are easy to crank and jump start.  Think about it.  We take an existing Asp.Net website and add a file called “asmx”.  Any method we attribute with [WebMethod] magically turns into a service.  Objects we pass in and pass out that have public getters and setters will be serialized into XML.

   1: [WebMethod]
   2: public string FlyToDestination(Location loc)
   3: {
   4:     // up up and away
   5: }

The barrier to cranking and getting going with an ASMX service is really simple.  And because it is simple, we’ve adopted them on a massive scale.  Let’s turn our attention to WCF.

image

WCF is a jack of all trades.  Think of it like a large plane that can repurposed for hauling passengers, hauling cargo, military use or whatever.  We are dealing with a plane but it is all about how that plane is configured.  Because things can be configured a lot of ways with the WCF 747 plane there are a lot more buttons in the airplane cockpit as compared to the Cessna 150.  The flip side of this is that once you understand how to configure WCF you’ve got one of the most versatile plane in the world!

From a developers stand point, the same logic or code written in an ASMX service works in WCF.  The code between the curly braces is the same.  The bottom line in either case is a method gets called and an object gets passed in (we call this a message when talking about services).  The difference in WCF is we program to a specific contract.  Here’s the same example above done in WCF.

   1: [ServiceContract]
   2: public interface IAirportService
   3: {
   4:     [OperationContract]
   5:     string FlyToDestination(Location loc);
   6: }
   7:  
   8: public class AirportService : IAirportService
   9: {
  10:     
  11:     public string FlyToDestination(Location loc)
  12:     {
  13:         // up up and away
  14:     }
  15: }

Instead of attributing the specific method like we do in ASMX, in WCF we attribute the Interface.  Programming to an Interface is a good thing and the reason this is done is to loosely couple the host of the service from the implementation.  Doing this opens the door for hosting WCF services not only in IIS but console apps, Winforms, WPF, etc.  Since we’ve programmed to a contract (which is just an Interface), any class that implements the Interface can be a service.  This is powerful.  This is powerful because how we expose this service is up to us (again it is all about configuration).  Because we can expose our AirportService a number of ways we can see that WCF provides developers the ability to write code once and repurpose their code as needed. 

image

Exposing a WCF service requires a little more training from this point forward ( just like flying a 747) because we have to understand how to configure the service.  It is this little bit of extra effort required to understand WCF configuration that stops a lot of developers from using WCF.  This is a shame because when the developer using ASMX wants to guarantee message delivery, participate in transactions, or use binary serialization instead of XML, they’ve got a lot of work ahead of them as compared to WCF.   

The moral of the story is ASMX is simple and because it is simple, it isn’t very powerful.  Take the time to learn about WCF because this is the future of the .Net platform, thus it will be time wisely spent.  If you’ve been holding back I encourage you to step out of your old ASMX habits and learn the ways of WCF. 

If you would like to dig a little deeper with WCF I encourage you to check out my “Demystifying Windows Communication Foundation” power point.  In there you’ll find a lot more details about how WCF works.

http://keithelder.net/Presentations/DemystyfyingWCF/DemystifyingWindowsCommunicationFoundation.ppt

Happy messaging.

posted @ Friday, October 17, 2008 12:48 PM

Print

Comments on this entry:

# re: WCF vs ASMX WebServices

Left by Mitchel Sellers at 10/17/2008 1:53 PM
Gravatar

Awesome comparison between the two!

# re: WCF vs ASMX WebServices

Left by newman at 10/20/2008 8:58 AM
Gravatar

Another thing that is great about WCF is that its architecture forces you into writing more cleanly separated code. Even with ASMX web services good developers would separate their logic into an assembly using the service as just a communication mechanism.

WCF makes this easier to achieve by enforcing it in the architecture. You can easily use your WCF service classes just like a regular library. Doing so allows for better code reuse and the fact that your services are exposed over the network via[insert method here] it just a configuration detail.

# re: WCF vs ASMX WebServices

Left by Pete Brown at 10/23/2008 8:56 PM
Gravatar

If you want to get into WCF and create a service that is just about as simple as an ASMX service, create a new "Silverlight-Enabled WCF Service" (assuming you have the SL tools installed). That creates a WCF service that is SOAP 1.1, does not use a separate interface definition, and supports serializing additional types like, say, Uri, that ASMX does not. It sets up the config file for you as well.

You get Cesna controls on top of your 747 :)

Pete

# re: WCF vs ASMX WebServices

Left by Joe Brinkman at 10/26/2008 3:02 PM
Gravatar

One counter argument to WCF is that configuration of WCF by the web administrator is not a trivial excercise. More and more cr@p is getting shoved into web.config with each new .Net release, and what used to be manageable by your typical web admin is quickly becoming something that is only understood by web developers. This is not a good thing.

# re: WCF vs ASMX WebServices

Left by Sugandha at 11/4/2008 11:08 PM
Gravatar

Awesome..indeed :)

# re: WCF vs ASMX WebServices

Left by Zeeshan Umar at 12/17/2008 1:52 AM
Gravatar

Microsoft is now making things more complex. Consider the case of normal web admins. Can they configure the webconfig files, since WCF adds lots of configurations in webconfig file.

However technology is good I also view an article related to the performance of WCF and ASMX. Check out it:

geekswithblogs.net/.../65067.aspx

# re: WCF vs ASMX WebServices

Left by Woooah at 12/18/2008 11:04 AM
Gravatar

Nevermind WCF and ASMX..... whose flying the plane?!?!?!!!

# re: WCF vs ASMX WebServices

Left by Mirage at 1/6/2009 1:25 PM
Gravatar

Our WCF application does not use any webconfig at all. Everything is done programmatically, and user friendly UI's in the product allow relevant knobs to be turned.

Forcing admins to edit XML isn't a WCF problem, it's a software design and application development problem.

# re: WCF vs ASMX WebServices

Left by graffic at 2/22/2009 12:23 PM
Gravatar

The boeing 747 has 38 years of changes over it's body. WCF not so many.

I guess the idea of this post of "hey! do you trust in a boeing 747? trust WCF too" tries to generate a bit of missunderstanding.

WCF is new, and still lacks "things". But it will depend on your project. You'll be surprised about some common functionality missing on WCF.

Choose your tool carefully :)

# re: WCF vs ASMX WebServices

Left by Ivan TRoy at 3/24/2009 9:42 PM
Gravatar

Microsoft and WCF really sucks !!!, it does not provide WSDL 2.0 support nor 100% xml-schema compliance nor fully WSDL contract first design.

# re: WCF vs ASMX WebServices

Left by Kamran at 4/5/2009 2:51 PM
Gravatar

Yah Lacking support of WSDL 2.0 is a very disgusting thing.Other wise it's a great innovation of integrating nearly all type of communication

# re: WCF vs ASMX WebServices

Left by Havinski at 4/5/2009 3:14 PM
Gravatar

I don't get it, why are you two so bent out of shape of not supporting WSDL 2.0? That spec was just released in middle or late of 2007 and the development of WCF was already WELL underway.

I'm sure the teams are looking at the spec and figuring out what to do with it.

There's a lot of platforms that don't even support nested schemas like the WSDL's generated for WCF out of the box.

I don't see this as a big deal. Get over it and move on.

# re: WCF vs ASMX WebServices

Left by Darlingpretty at 4/22/2009 5:36 AM
Gravatar

You mean that WCF will replace .net remoting , i can see WCF architecture is pretty same as .net remoting.

# re: WCF vs ASMX WebServices

Left by TheElder at 4/22/2009 7:07 AM
Gravatar

@Darlingpretty

The architecture of WCF vs .Net remoting couldn't be more different. That's just one of the things WCF unifies as the communication platform.

# re: WCF vs ASMX WebServices

Left by Anonymous Coward at 4/24/2009 2:26 AM
Gravatar

WCF has some interesting ideas, but its layers of complexity pretty much cause it to screw the pooch, both in terms of programability and performance. All those layers of abstraction have a significant cost. At launch MS has was only able to claim about a 20% performance edge over standard web services, which begs the question of "why bother". In our experience at _major_internet_company_ WCF falls apart at any scale above small enterprise (~2000 users). If you're in the workgroup-to-enterprise pocket, maybe it offers something truly useful beyond what one could roll themselves, but I haven't found it.

# re: WCF vs ASMX WebServices

Left by TheElder at 4/26/2009 9:20 AM
Gravatar

20% isn't even close with the numbers you can achieve with WCF compared to ASMX depending on the situation.

I have a blog post I'm working on that will show how things compare. Stay tuned.

At your company you may be running into the built-in throttling with WCF. It is out of the box tuned down for DOS attacks. You have to chose a fair balance between what you want to allow.

Make no bones about it. It is more complex because, well IT DOES MORE. Having a single programming model is a huge will in my book. It is something we need and we're just seeing the surface scratched with WCF.

# re: WCF vs ASMX WebServices

Left by kumal at 5/31/2009 11:56 AM
Gravatar

thanks for the article. Agree wcf is lot more complex compared to asmx. But the fact that asmx is being phased out by wcf does not leave us any choice but to learn wcf, does it?

One question i have tho, yes understood wcf is more feature rich than asmx. but in terms of interoperability between platforms, and if our applications do not need all the additional features such as security, i can safely assume, asmx offers the same interoperability as wcf, yes?

# re: WCF vs ASMX WebServices

Left by TheElder at 6/2/2009 9:19 AM
Gravatar

Kumal,

There's something other than that to consider and that is speed or performance.

If you are going from .Net to .Net, meaning your server is .Net and all of the clients hitting your service is .Net, then you can or will get a tremendous speed increase using WCF over ASMX.

I have a sample app that I built that benchmarks the various bindings as well as asmx. A WCF service using netTcpBinding (which is a binary binding) processed 100 messages in 40 milliseconds. The exact same call made with ASMX was 323 milliseconds.

Even if you use the basicHttpBinding which is essentially what an ASMX service is you will still see a performance increase merely because WCF's serialization is faster.

# re: WCF vs ASMX WebServices

Left by kumal at 6/2/2009 12:32 PM
Gravatar

Thanks for the response, nice to know someone actually monitors their blog ;-)

I have also built an app to compare asmx and tcp performance, and to be honest (also i could be doing something wrong), i do see difference when sending a large amount of data, but not much difference when sending a smaller ones, is that expected?

Would love to see your benchmark, you have a link to the sample app?

# re: WCF vs ASMX WebServices

Left by Eran at 6/3/2009 3:54 PM
Gravatar

Can't read the ppt in any way

# re: WCF vs ASMX WebServices

Left by TheElder at 6/3/2009 4:03 PM
Gravatar

Kumal,

You definitely have something wrong as what you are implying flies in the face of computer science. Binary is just faster. I don't have a link to the sample app because I'll need to write up a whole article on just how to configure it. Which I plan on doing one day.

# re: WCF vs ASMX WebServices

Left by TheElder at 6/3/2009 4:06 PM
Gravatar

Eran,

There is a video of a prensentation I did of that deck this past week that you can watch (even better than getting just the deck) that I will share where I do this presentation. I'll update this once it is ready.

# re: WCF vs ASMX WebServices

Left by kumal at 6/8/2009 2:29 PM
Gravatar

dont hurt to ask..... would you mind if i send you my solution, and tell me what i am doing wrong?

# re: WCF vs ASMX WebServices

Left by Keith Elder at 6/10/2009 3:34 PM
Gravatar

No promises I'll get to it quickly but send it over, my email is on the about page. May make for a good blog post.

-Keith

# re: WCF vs ASMX WebServices

Left by Monsy A.K at 6/11/2009 7:47 AM
Gravatar

Nice comments on WCF. I think we can share more and more about the architecture and implementation about WCF. I am started the experiment. Lets see. I will post my doubts as the implementation progress. Its a good article

Your comment:






 
 
 
Please add 5 and 7 and type the answer here:
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678