ChatGPT解决这个技术问题 Extra ChatGPT

What are the differences between WCF and ASMX web services?

I am totally confused between WCF and ASMX web services. I have used a lot of web services in my earlier stage, and now there is this new thing introduced called WCF. I can still create WCF that function as a web service. I think there will be more stuff in WCF.

What are the differences between WCF and Web services? When should each one be used?

There's no way this is a recommendation question. Neither ASMX nor WCF is a product. They are two iterations of web services in the Microsoft stack. The question is what are the differences, and is it worth my while to learn the new one? This is not the same thing as "should I use Telerik controls or Infragistics.

m
marc_s

Keith Elder nicely compares ASMX to WCF here. Check it out.

Another comparison of ASMX and WCF can be found here - I don't 100% agree with all the points there, but it might give you an idea.

WCF is basically "ASMX on stereoids" - it can be all that ASMX could - plus a lot more!.

ASMX is:

easy and simple to write and configure

only available in IIS

only callable from HTTP

WCF can be:

hosted in IIS, a Windows Service, a Winforms application, a console app - you have total freedom

used with HTTP (REST and SOAP), TCP/IP, MSMQ and many more protocols

In short: WCF is here to replace ASMX fully.

Check out the WCF Developer Center on MSDN.

Update: link seems to be dead - try this: What Is Windows Communication Foundation?


With visual studio 2010 .net 4.0 WCF is just as easy to write as ASMX, there is no excuse to write ASMX anymore. WCF is way faster, more flexible, more secure. ASMX is legacy and nobody should still write it period.
"Most developers incorrectly assume that ASMX requires IIS; after all, it's the only use case they've ever seen. But the truth is that ASMX doesn't have any technical dependencies on IIS whatsoever." msdn.microsoft.com/en-us/magazine/cc163879.aspx
@MrNick: right. No IIS, but still HTTP only, and go compare the code for hosting ASMX to the code for hosting a WCF service.
Unfortunately the link keithelder.net/2008/10/17/wcf-vs-asmx-webservices is broken.
@codemonkeyliketab: the response is more than 6 years old - lightyears in the internet age! I tried to find a replacement for the WCF Developer Center - see my update
J
John Saunders

ASMX Web services can only be invoked by HTTP (traditional webservice with .asmx). While WCF Service or a WCF component can be invoked by any protocol (like http, tcp etc.) and any transport type.

Second, ASMX web services are not flexible. However, WCF Services are flexible. If you make a new version of the service then you need to just expose a new end. Therefore, services are agile and which is a very practical approach looking at the current business trends.

We develop WCF as contracts, interface, operations, and data contracts. As the developer we are more focused on the business logic services and need not worry about channel stack. WCF is a unified programming API for any kind of services so we create the service and use configuration information to set up the communication mechanism like HTTP/TCP/MSMQ etc


Maybe it's just me, but paragraphs #2 and #3 stand out as marketing BS.
@Net Experts u said " If you make a new version of the service then you need to just expose a new end" what are u trying to say ?
@Mou, he meant exposing a new endpoint I guess. Like <endpoint address="http://api.microsofttranslator.com/V1/soap.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_LanguageService1" contract="TranslatorService.LanguageService" name="BasicHttpBinding_LanguageService3" />
A
Andrew Hoffman

This is a very old question, but I do not feel that the benefits of ASMX have been fairly portrayed. While not terribly flexible, ASMX web services are very simple to use and understand. While WCF is more flexible, it is also more complex to stand up and configure.

ASMX web services are ready to stand up and add as a webservice reference as soon as you add the file. (assuming your project builds)

For the simple development workflow of create webservice -> run webservice -> add webservice reference, an ASMX webservice has very little that can go wrong, not much that you can misconfigure, and that is it's strength.

In response to those that assert that WCF replaces ASMX, I would reply that WCF would need to add a streamlined K.I.S.S. configuration mode in order to completely replace ASMX.

Example web.config for an ASMX webservice:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings />
  <system.web>
    <compilation targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>

I don't think WCF needs more configuration for the work that can be done using asmx webservices , and you can as well add a reference in your client side for the WCF service , WCF is way more powerful than asmx , it can do what asmx does and it will be as simple as asmx
@Coder1409 well it's certainly more configuration, because some things need to be configured. Binding, discoverability, etc. Not necessary in asmx. Service contracts, operation contracts, data contracts, data members. Not necessary in asmx. Of course these things are why wcf is superior and more powerful, but to deny that it adds complexity just isn't being honest. It's reaching to say that there are no downsides.
The first (second,third...) time i created a wcf it took me ages to figure out the config required. "Why do i need to do that?" i kept asking myself. "Why bother with something so complex?". asmx just worked instantly. So saying that it's just as simple just isn't true. One day i will fully understand and change my view - until that day GRRRR!!!
@tomjm Again, try it with a modern version of Visual Studio and .NET. Among other things, the configuration system has changed to default more settings. In particular, you'll hardly need to do more than specify the URL in order to get a simple web service, http only, with no features.
@tomjm Well that's good to know. Honestly it used to be so much config that it made sense to use a wcf config manager app. It could do everything! (except work out of the box with some good default settings) However nowadays we think more simply concerning WEB services. Wcf is still great for advanced service, but.. probably the majority of service development is webservice development, and it's hard to beat webapi2 for webservice development imo.
J
John Saunders

WCF completely replaces ASMX web services. ASMX is the old way to do web services and WCF is the current way to do web services. All new SOAP web service development, on the client or the server, should be done using WCF.


The trouble is, while ASMX was a simplistic model (meaning not terribly flexible), it was a simple model (meaning easy to use and understand for the most common web service needs). WCF adds a lot of extra complexity. While MS wants to replace ASMX with WCF, there seems to be a bit of resistance to it until MS makes the most common scenarios as simple as the old [Webmethod] way.
WCF is not at all complex. Create a simple "hello world" web service in both and see how much code you write in each. The answer: not much in either, and only slightly more in WCF. And, BTW, ASMX already has been replaced by WCF. Done deal.
"WCF is not at all complex" Ha. Tell that to my way overbloated web.config.
(Not a downvoter, but I'll tell you why) - because "this is new and that is old" is not a significant difference that anyone should care about. We don't choose tools based solely on how old they are - the hammer is really, really old, but it's still the best tool for driving nails most of the time. So, you gave a valid answer, it's just not that helpful - like the old "you are in an airplane" joke.
down voted - no reference. How do you know this information? Is it opinion? Do you work for microsoft? Add a reference and i will up vote.
S
Sagnik Majumder

There's a lot of talks going on regarding the simplicity of asmx web services over WCF. Let me clarify few points here.

Its true that novice web service developers will get started easily in asmx web services. Visual Studio does all the work for them and readily creates a Hello World project.

But if you can learn WCF (which off course wont take much time) then you can get to see that WCF is also quite simple, and you can go ahead easily.

Its important to remember that these said complexities in WCF are actually attributed to the beautiful features that it brings along with it. There are addressing, bindings, contracts and endpoints, services & clients all mentioned in the config file. The beauty is your business logic is segregated and maintained safely. Tomorrow if you need to change the binding from basicHttpBinding to netTcpBinding you can easily create a binding in config file and use it. So all the changes related to clients, communication channels, bindings etc are to be done in the configuration leaving the business logic safe & intact, which makes real good sense.

WCF "web services" are part of a much broader spectrum of remote communication enabled through WCF. You will get a much higher degree of flexibility and portability doing things in WCF than through traditional ASMX because WCF is designed, from the ground up, to summarize all of the different distributed programming infrastructures offered by Microsoft. An endpoint in WCF can be communicated with just as easily over SOAP/XML as it can over TCP/binary and to change this medium is simply a configuration file mod. In theory, this reduces the amount of new code needed when porting or changing business needs, targets, etc.

Web Services can be accessed only over HTTP & it works in stateless environment, where WCF is flexible because its services can be hosted in different types of applications. You can host your WCF services in Console, Windows Services, IIS & WAS, which are again different ways of creating new projects in Visual Studio.

ASMX is older than WCF, and anything ASMX can do so can WCF (and more). Basically you can see WCF as trying to logically group together all the different ways of getting two apps to communicate in the world of Microsoft; ASMX was just one of these many ways and so is now grouped under the WCF umbrella of capabilities.

You will always like to use Visual Studio for NET 4.0 or 4.5 as it makes life easy while creating WCF services.

The major difference is that Web Services Use XmlSerializer. But WCF Uses DataContractSerializer which is better in Performance as compared to XmlSerializer. That's why WCF performs way better than other communication technology counterparts from .NET like asmx, .NET remoting etc.

Not to forget that I was one of those guys who liked asmx services more than WCF, but that time I was not well aware of WCF services and its capabilities. I was scared of the WCF configurations. But I dared and and tried writing few WCF services of my own, and when I learnt more of WCF, now I have no inhibitions about WCF and I recommend them to anyone & everyone. Happy coding!!!


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now