webapi如何比WEF REST轻量化? [英] How webapi is light weighted than WEF REST?

查看:79
本文介绍了webapi如何比WEF REST轻量化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是重复的问题或无聊的问题,但我需要知道答案,因为这是我可以提出问题的最大平台。



我正在寻找WebApi和WEF REST之间的区别,在几乎所有的艺术作品中,我发现WebApi比WCF REST重量轻,但没有一个说为什么。



按照我的理解(非常小的知识)都可以通过HTTP协议工作,并且可以返回JSON数据(它应该比XML更轻)。如果协议和返回数据类型都与使WebApit更轻的相同吗?



我尝试过:



我检查了几本艺术品,但没有找到确切的答案。所有人都在讨论SOAP vs REST或XML vs JSON或HTTP vs Other协议,但没有人清楚地说明为什么WebApiy比WCF REST轻。

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.

推荐答案

引用:

我正在寻找WebApi和WEF REST之间的区别,在几乎所有的艺术作品中,我发现WebApi比WCF REST重量轻,但没有一个告诉为什么。

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".

我会告诉你他们帖子的为什么部分和对这个问题的兴趣增加促使我写一篇文章,涵盖ASP.NET Web API似乎是一个好方法的大多数部分与使用相比WCF框架的Web服务。

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:

根据我的理解(非常小的知识),它们都可以通过HTTP协议工作并返回JSON数据(它应该比XML轻)。如果协议和返回数据类型都与使WebApit更轻的相同?

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?

说到这部分,WCF运行在多个协议上,而不仅仅是REST API或HTTP协议。这是WCF优于ASP.NET Web API的好处,因为它几乎可以在任何协议中进行通信,它也可以比TCP协议更深入一步。 JSON实际上比XML轻得多,我在文章中谈到了这个概念,使用C#从JSON中的零到英雄 [ ^ ]。



现在回答你的问题:使ASP.NET Web API比WCF框架更轻的主要因素是因为运行Web API所需的大多数库都是已经在ASP.NET框架中运行了。因此,如果您比较整体资源使用情况,则会有一点(或没有)差异。 ASP.NET Web API使用ASP.NET MVC来运行和执行您需要的内容。另一方面,WCF是一个完整的框架(就像ASP.NET一样),然后需要完整的资源才能工作,并且您认为它需要花费更多的资源。 Web API只是特定域名中一个(或多个)URL的处理程序,仅在请求时触发。



其次,大多数程序员也必须谈论要编写的轻量级代码。这完全是ASP.NET Web API完全取消WCF使用的地方 - 期间。基于CRUD的单个API控制器的代码如下:

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,

[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>



这是我现在写的代码,不到5分钟即可运行 - 只要我的模型对象完美运行并处理大部分事情。



对于WCF,你不能这样做。你必须管理客户和其他类似的东西,合同等。这是导致很多混乱的事情。在上面的代码中,您可以看到我已经为框架提供了所有内容,例如它将访问的位置,要由哪个函数处理的HTTP谓词以及如何映射值等等。



这些是为什么的人(包括特别是我)更喜欢在WCF上使用ASP.NET Web API。


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.


这篇关于webapi如何比WEF REST轻量化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆