Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I know this could be the duplicate question or boring question but I need to know the answer as this is the biggest platform I can have to ask question.

I was searching difference between WebApi and WEF REST and in almost every artical I found that WebApi is lightweighted than WCF REST but none of them told "why".

As per my understanding(very small knowledge) both work over HTTP protocol and can return JSON data (it supposed to be lighter than XML). If protocol and return data type both are same than what make WebApit lighter?

What I have tried:

I've checked several articals but didn't find exact answer. All are talking about SOAP vs REST or XML vs JSON or HTTP vs Other protocols but no one clearly said why WebApiy is lighter than WCF REST.
Posted
Updated 21-Sep-16 11:46am

1 solution

Quote:
I was searching difference between WebApi and WEF REST and in almost every artical I found that WebApi is lightweighted than WCF REST but none of them told "why".
I will tell you the "why" part of their posts and the increased interest in this question is motivating me to write an article covering the most parts where ASP.NET Web API seems to be a good approach as compared to using Web Services by WCF framework.
Quote:
As per my understanding(very small knowledge) both work over HTTP protocol and can return JSON data (it supposed to be lighter than XML). If protocol and return data type both are same than what make WebApit lighter?
Speaking about this part, WCF runs on multiple protocols and not just a REST API or HTTP protocol. That is the benefit of WCF over ASP.NET Web API because it can communicate in almost-any protocol, it can go one step deeper than HTTP too — in TCP protocol. JSON is actually very much lighter than XML, I speak about this concept in my article, From zero to hero in JSON with C#[^].

Coming to your question now: The main factor that makes ASP.NET Web API lighter than WCF framework is because most of the libraries that are required to run Web API are already running in the ASP.NET framework itself. So if you compare the overall resource usage, there will be a little (or no) difference at all. ASP.NET Web API uses, ASP.NET MVC to run and perform what you need to. WCF on the other hand, is a full framework (just like ASP.NET) and would then require full resources to work and you believe it to take and consume more resources. Web API is just a handler for one (or more) URLs in the specific domain name and is only triggered when requested.

Secondly, most programmers must also be speaking about the light-weight code to be written. That is entirely the place where ASP.NET Web API kills the usage of WCF at all — Period. The code for a single CRUD-based API controller is just the following,
C#
[Route("api/tickets")]
public class TicketsApiController : ApiController {
    public List<ticket> GetTickets() {
        return Model.GetAllTickets();
    }
 
    [HttpGet]
    [Route("{id}")]
    public Ticket GetTicket(int id) {
        return Model.GetAllTickets().Where(x => x.Id == id).FirstOrDefault();
    }

    [HttpPost]
    public void SaveTicket([FromBody] Ticket ticket) {
        if(ticket != null) {
            Model.Add(ticket);
        }
    }
    
    [HttpPut]
    public void UpdateTicket(int id, [FromBody] Ticket ticket) {
        if(ticket != null && !(id < 0)) {
            Model.UpdateTicket(id, ticket);
        }
    }
  
    [HttpDelete]
    public void DeleteTicket(int id) {
        Model.Remove(Model.Find(x => x.Id == id));
    }
}
</ticket>

This is the code that I wrote just right now, in under 5 minutes and would work — provided that my Model object works perfectly and handles most of the things.

In the case of WCF, you cannot do that. You have to manage clients and other similar stuff, contracts etc. That is the thing that causes a lot of confusions. In the above code, you can see that I have provided everything to the framework such as the location where it will be access, the HTTP verbs which are to be handled by which function and how values are to be mapped and so on.

These are a few of the "why"s people (including and especially me) prefer to use ASP.NET Web API over WCF.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900