Hello folks, hope you all are doing well. Today I want to explain regarding difference between WCF and Web API and which should you pick for your project with detailed explanation. I have already discussed regarding Difference between WCF and Web Service and Difference between SOAP and REST, You should look into these concepts as well ,to understand difference between WCF and Web API.
Let’s try to understand WCF and Web API in brief.
Table of Contents
WCF
- It is based on SOAP and return data in XML form.
- It is the evolution of the web service(ASMX) and support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.
- The major issue with WCF is tedious and extensive configurations.
- Supports any client who supports XML.
- Can be hosted as self hosted, IIS and as a Windows Service also.
Web API
- This is used to build HTTP service with easy and simple way.
- Used to build REST-ful services over the .net Framework
- It uses full features of HTTP(like URIs, Request Response headers, caching, versioning, various content formats)
- Also supoorts MVS features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
- Hosted with in application or on IIS.
- It is light weight and good for devices which have limited bandwidth like smart phones.
Lets find difference between WCF and Web API as below by comparing features.
Features | Web API | WCF |
Endpoint | HTTP based | Soap based |
Service Type | Front End | Back End |
Supports | caching, compression, versioning | None of as Web API |
Uses Framework Of | asp.net | WCF |
Orientation | Resource Oriented | Service Oriented |
Transports | http only | http, tcp, MSMQ, Named pip |
Message Pattern | Request Reply | Request reply, One way, duplex |
Configuration Overhead | Less | Much |
Security | Lesser than WCF (web standard security) | Very high(WS-I standard) |
Hosting | IIS, Self Hosting | IIS, Windows Service, Self hosting |
Performance | Fast | Slower than Web API |
Introduced in | .Net 4.0 | .Net 3.5 |
Content format | Any media format | SOAP+XML |
Service Interface | URL Patternd, HTTP methods | Service Contracts |
State Management | Stateless | Stateless with Per Call |
Caching mechanism | Built in to HTTP prefer application control | Handled by application |
Error handling | HTTP status coder filters, exceptions | Faults, behaviors |
Types | Opt-out | Opt-in |
Uses | Use SignalR for asynchronous signalling(polling,websocket,long polling) | Use WCF data services for full OData support |
Now Let’s try to understand when should we pick either of them in your project.
- Within the company Network and .NET based clients : Use WCF with TCP binding (Fast communication than HTTP).
- Outside the company Network, and use diverse technologies like PHP, Python etc: Use Web API with REST
- WCF is a best fit for scenarios like message queues, duplex communication, end-to-end message security, one way messaging, distributed transactions, etc. WEB API is a best fit to create a resource-oriented services using HTTP/Restful and it works well with MVC-based applications.
- WCF was created to develop SOAP-based services and bindings. Since WCF is SOAP based, which uses standard XML schema over HTTP, it could lead to slower performance. WEB API is a better choice for simpler, light weight services. WEB API can use any text format including XML and is faster than WCF.
- WEB API can be used to create full-blown REST Services. WEB API doesn’t require any data contracts and doesn’t require configurations to the level of WCF.
- If performance and quick development cycles are your criteria, WEB API is a better choice over WCF.