22
DecDifference between WCF and Web API and WCF REST and Web Service
Difference between WCF and Web API and WCF REST and Web Service: An Overview
The .Net framework has several technologies that allow you to create HTTP services such as Web Service, WCF, and now Web API. There are a lot of articles on the internet that may describe whom you should use, and gaining clarity on this topic is emphasized in ASP.NET Core training. Nowadays, you have a lot of choices for building HTTP services on the .NET framework.
In this Web API Tutorial, I would like to share my opinion with you on Web Service, WCF, and now Web API. For more information about Web API refer What is Web API and why to use it ?.
Web Service
It is based on SOAP and returns data in XML form. It supports only HTTP protocol. It is not open source but can be consumed by any client that understands XML. It can be hosted only on IIS.
WCF
It is also based on SOAP and returns data in XML form. It is the evolution of the web service(ASMX) and supports various protocols like TCP, HTTP, HTTPS, Named Pipes, and MSMQ. The main issue with WCF is its tedious and extensive configuration. It is not open source but can be consumed by any client that understands XML. It can be hosted within the application on IIS or using Windows service.
WCF Rest
To use WCF as WCF Rest service you have to enable webHttpBindings. It supports HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively. To enable other HTTP verbs you have to do some configuration in IIS to accept requests of that particular verb on .svc files. Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified. It supports XML, JSON, and ATOM data formats.
Web API
This is the new framework for building HTTP services easily and simply. Web API is open source an ideal platform for building RESTful services over the .NET Framework. Unlike the WCF Rest service, it uses the full features of HTTP (like URIs, request/response headers, caching, versioning, and various content formats). It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, and unit testing makes it more simple and robust. It can be hosted within the application or on IIS. It is lightweight architecture and good for devices that have limited bandwidth like smartphones. Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML, or whatever format you want to add as a MediaTypeFormatter.
Read More: Top Rest API Interview Questions and Answers |
Difference between WCF and Web API and WCF REST and Web Service
Feature | WCF | Web API | WCF REST | Web Services (ASMX) |
Purpose | General-purpose service framework | Building HTTP services (RESTful) | RESTful services over WCF | Building SOAP-based services |
Communication | SOAP, REST, one-way, duplex | RESTful (HTTP) | RESTful (HTTP) | SOAP (HTTP) |
Hosting | IIS, self-hosting, WAS | IIS, self-hosting | IIS, self-hosting, WAS | IIS |
Programming model | Contract-based, attributes | MVC-based | Contract-based, attributes | Contract-based |
Data format | XML, JSON, binary | JSON, XML, and other formats | JSON, XML | XML |
Security | WS-Security, message security | HTTP-based security | HTTP-based security | WS-Security |
Extensibility | Custom bindings, behaviors | Middleware, filters | Custom bindings, behaviors | Limited |
Ideal for | Enterprise-level services, interoperability | Modern web, mobile, and cloud apps | RESTful services within WCF | Legacy systems, interoperability |
Summary
Web Services (SOAP), WCF (flexible but complex), WCF REST (limited REST support), and Web API (modern, RESTful, open-source) are all alternatives for implementing HTTP services in the .NET framework. Web API is appropriate for modern programs, WCF/REST for certain circumstances, and Web Services for legacy systems. Each choice has advantages and disadvantages, so choose cautiously based on your requirements.