A Quick Rundown of Web Services in ASP.NET : Web API and WCF

I recently received a shout-out on Twitter from a fellow developer regarding a post on the ASP.NET Forums I had written last year. He mentioned that it had helped him out quite a bit in the area of Web Services, so I thought I would take the time to share the post on here.

The question itself was as follows :

Can anyone explain what is the difference between Web Service, Web Service RESTful service, WCF and Web API.

What is a Web Service?

Since the question pertains to Web Services, let's use the following paraphrased definition provided from this Stack Overflow discussion as there is no reason to reinvent the wheel :

A Web Service is a function that can be accessed by other programs over the web (via HTTP).

To clarify a bit, when you create a website or application that outputs HTML, its target is the browser and by extension the human being reading the page in the browser. A web service is different in this regard in that rather than directly targeting a browser, it is designed to be used / consumed by an actual application.

Basically, you can think of a Web Service as an application that can communicate with other applications to either send data, receive data or exposes certain methods for doing either of those things.

Types of Web Services

Now when it comes to Web Services, they can typically be broken down into two types that define how these Web Services operate: REST and SOAP.

You can find a great summary of both of these types of services here and detailed below :

  • REST - The general sweet spot for using REST is when you are exposing a public API over the internet to handle CRUD operations on data. REST is focused on accessing named resources through a single consistent interface.

  • SOAP - SOAP brings its own protocol and focuses on exposing pieces of application logic (not data) as services. SOAP exposes operations. SOAP is focused on accessing named operations, each implement some business logic through different interfaces.

You can find another in-depth discussion here that details many of the differences between the two protocols.

Two Roads Diverged...

You have two major options when working with Web Services in ASP.NET. The older approach being Windows Communication Foundation (WCF) and the newer being Web API.

I'll include a few resources below on how to get started with using either of these technologies.

Windows Communication Foundation (WCF)

Windows Communication Foundation is a framework that was designed by Microsoft to construct and build Web Services that can be used to provide an API and other services to other web applications.

Web Services are excellent if your application's architecture calls for it and you have multiple applications that might need to work together and access some of the same data (such as passing data or other objects across).

Web API

ASP.NET Web API is a development environment aimed at developing RESTful Web Services that will allow your application to easily send and receive HTTP Requests and perform operations based on the types of requests that are made to it (such as providing information about a user if provided their ID etc.) It was designed to replace the previous Windows Communication Foundation (WCF) approach to developing Web Services within .NET.

Which should I use?

You'll generally see WCF used more commonly with SOAP-based services as Web API simply does not support them. Web API generally shines when you are working purely with HTTP requests and is considered RESTful.

A few benefits that you'll often hear regarding choosing Web API over WCF :

  • Web API is generally going to be more lightweight which benefits devices with limited bandwidth.
  • Since Web API relies on HTTP, it can be bi-directional (request / response) and also supports everything that the HTTP Protocol does (e.g. caching, headers, versioning, various content types).

There really isn't any particular reason to ALWAYS choose one over the other. They each serve to accomplish similar goals (web services) in different ways and have their own strengths and weaknesses. I would recommend reading through an article that compares and contrasts the two such as this one to help get a better idea of what you need as there are so many different factors at play here.

There really isn't any reason that you couldn't implement both for a single type of application and use Web API for your web-oriented activities and WCF for more of your larger data-oriented activities.

Resources

I would recommend checking out the following resources pertaining to WCF :

With regards to Web API, I would recommend visiting the Web API area of ASP.NET. It features tutorials and walk-throughs that not only explain what Web API is, but how to use it in various scenarios.

If you need some generic information on Web Services within ASP.NET in general, the following might be useful :