What is the difference between ASMX, WCF, and ASP.NET Web API?
Application architecture has evolved from monolithic architecture to SOA architecture in order to make better separation, then to more refined microservice architecture today.
Applications need to communicate among themselves, and to achieve this purpose, Microsoft has developped technologies like ASMX, WCF, and ASP.NET Web API.
Let’s explore them together today.
1. ASMX
ASMX (ASP.NET Web Services) is the primary web service technology in .NET 1.0 and .NET 2.0.
ASMX provides the ability to build web services that send SOAP (Simple Object Access Protocol) messages over HTTP protocol.
1.1. SOAP message
SOAP message is in XML format.
SOAP message request sample:
1.2. ASMX web service code sample
1.3. WSDL
WSDL (Web Service Description Language) is a language to describe the service.
1.3.1. WSDL structure
1.3.2. PrintService’s WSDL detail
1.3.3. PrintService’s WSDL code
See more information about WSDL: W3C WSDL Specification
1.3.4. SOAP vs WSDL vs UDDI
1.4. ASMX service consumption
To consume the ASMX service, you can use any web browser or SoapUI.
1.4.1. Consume ASMX service in Web browser
Call:
Result:
1.4.2. Consume ASMX service in SoapUI
To consume the WCF service in SoapUI, you must enter a .WSDL suffix after .asmx.
Call and result:
1.5. ASMX project source project
Download SOAP ASMX service sample project
2. WCF
WCF (Windows Communication Foundation) is a framework for building service-oriented applications. WCF is introduced since .NET 3.0.
2.1. WCF Architecture
2.2. WCF communication binding
2.3. SOAP WCF service sample code
WCF can transfer SOAP messages over HTTP protocol by default, just like ASMX service.
SOAP WCF service sample code.
2.4. SOAP WCF WSDL
2.4.1. SOAP WCF WSDL detail
2.4.2. SOAP WCF WSDL code
2.5. SOAP WCF service consumption
Add WSDL in SoapUI:
PrintService.svc’s .svc means Service.
SOAP WCF service consumption:
2.6. WCF service bindings
In addition, WCF provides much more features to build secure, complex WCF services. WCF can work with other protocols like TCP, HTTPS, and UDP.
Here are all the WCF Bindings:
2.7. REST WCF service sample code
WCF also can act as RESTful service with WebHttpBinding.
REST WCF service sample:
2.8. REST WCF WADL
WADL (Web Application Description Language) is a machine-readable XML description of HTTP-based web services.
2.8.1. REST WCF WADL structure
2.8.2. REST WCF WADL code
2.9. REST WCF service consumption
2.10. SOAP and REST WCF services source projects
3. ASP.NET Web API
ASP.NET Web API pass messages over HTTP or HTTPS protocol.
Modern ASP.NET Web APIs are RESTful.
3.1. REST
REST (RepreSentational State Transfer)
REST Web API has the following characteristics:
- Client-server architecture
The principle behind the client-server constraints is the separation of concerns.
- Statelessness
The client-server communication is constrained by no client context being stored on the server between requests. Each request from any client contains all the information necessary to service the request, and the session state is held in the client. The session state can be transferred by the server to another service such as a database to maintain a persistent state for a period and allow authentication.
- Cacheability
Well-managed caching partially or completely eliminates some client-server interactions, further improving scalability and performance.
- Layered system
- Code on demand (optional)
Servers can temporarily extend or customize the functionality of a client by transferring executable code
- Uniform interface
The uniform interface constraint is fundamental to the design of any RESTful system.
The four constraints for this uniform interface are:
- Resource identification in requests
- Resource manipulation through representations
- Self-descriptive messages
- Hypermedia as the engine of application state (HATEOAS)
3.2. Web API sample code
3.3. Web API consumption
3.4. Web API source project
Download ASP.NET Web API sample project