C# WCF Web API 和 ASP.NET Web API 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9451298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between WCF Web API and ASP.NET Web API
提问by Not loved
I've done a bit of work in the past using WCF WebAPI and really liked a lot of its features, I'm just playing with ASP.NET Web API at the moment and it seems completely different (IE completely removed from WCF).
我过去使用 WCF WebAPI 做了一些工作,并且非常喜欢它的很多功能,我现在只是在玩 ASP.NET Web API,它似乎完全不同(IE 完全从 WCF 中删除)。
Does anyone know which features of WCF WebAPI are included in ASP.NET 4 Web API?
有谁知道 ASP.NET 4 Web API 中包含 WCF WebAPI 的哪些功能?
采纳答案by Not loved
Ive done a little more reading around this and found a few pages by MS people on this:
我对此进行了更多阅读,并找到了 MS 人员关于此的几页:
The WCF Web API abstractions map to ASP.NET Web API roughly as follows
WCF Web API 抽象映射到 ASP.NET Web API 大致如下
WCF Web API -> ASP.NET Web API
WCF Web API -> ASP.NET Web API
- Service -> Web API controller
- Operation -> Action
- Service contract -> Not applicable
- Endpoint -> Not applicable
- URI templates -> ASP.NET Routing
- Message handlers -> Same
- Formatters -> Same
- Operation handlers -> Filters, model binders
- 服务 -> Web API 控制器
- 操作 -> 动作
- 服务合同 -> 不适用
- 端点 -> 不适用
- URI 模板 -> ASP.NET 路由
- 消息处理程序 -> 相同
- 格式化程序 -> 相同
- 操作处理程序 -> 过滤器,模型绑定器
and http://wcf.codeplex.com/discussions/319671
和http://wcf.codeplex.com/discussions/319671
The integrated stack supports the following features:
集成堆栈支持以下功能:
- Modern HTTP programming model
- Full support for ASP.NET Routing
- Content negotiation and custom formatters
- Model binding and validation
- Filters
- Query composition
- Easy to unit test
- Improved Inversion of Control (IoC) via DependencyResolver
- Code-based configuration
- Self-host
- 现代 HTTP 编程模型
- 完全支持 ASP.NET 路由
- 内容协商和自定义格式化程序
- 模型绑定和验证
- 过滤器
- 查询组合
- 易于单元测试
- 通过 DependencyResolver 改进控制反转 (IoC)
- 基于代码的配置
- 自托管
回答by WDRust
From what I've learned, Microsoft did a little bit of naming confusion here.
据我所知,微软在这里做了一些命名混淆。
I'm assuming you know what WCF is all about, this big framework built on top of XML to allow user to build distributed services with a wide variety of technologies (from SOAP to REST to MSMQ etc.).
我假设您知道 WCF 是什么,这个构建在 XML 之上的大型框架允许用户使用各种技术(从 SOAP 到 REST 到 MSMQ 等)构建分布式服务。
It's hard as hell to use (for me at least) and requires a lot of bootstrap to have it working, and eventually they realized this and started providing some default configuration for simple http services (WCF REST starter kit anyone?). ASP.NET MVC was gaining momentum and some of the features it provided (automatic arguments matching for example) started to show up in WCF.
它很难使用(至少对我而言)并且需要大量引导程序才能使其工作,最终他们意识到这一点并开始为简单的 http 服务提供一些默认配置(WCF REST 初学者工具包任何人?)。ASP.NET MVC 正在获得动力,它提供的一些功能(例如自动参数匹配)开始出现在 WCF 中。
Now that's the situation:
现在的情况是这样的:
Announcement: WCF Web API is now ASP.NET Web API! ASP.NET Web API released with ASP.NET MVC 4 Beta. The WCF Web API and WCF support for jQuery content on this site wll removed by the end of 2012.
公告:WCF Web API 现在是 ASP.NET Web API!ASP.NET Web API 随 ASP.NET MVC 4 Beta 一起发布。到 2012 年底,该站点上的 WCF Web API 和 WCF 对 jQuery 内容的支持将被删除。
http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api
http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api
And that's better imho.
恕我直言,这更好。
I'm quite sure it should be possible to host asp.net mvc4 webapi on top of WCF (if you ever need that), but i can't find documentation that can prove me right (or wrong).
我很确定应该可以在 WCF 之上托管 asp.net mvc4 webapi(如果您需要的话),但是我找不到可以证明我是对(或错)的文档。
UPDATE(can't fit as comment): Wait, there is a huge different between "moving a subset of communication technology from a library/framework to another" and "replace WCF". I personally think that WCF was designed for some kind of communication concept and it has a rather cool design, but the distributed computing is somewhat moving on to new (and simpler) solutions (look the feature-rich SOAP vs the lean e flexible REST, although many people still use REST in a RPC manner), and i think that this kind of programming patterns better fit into the MVC architecture than the WCF one. Effort was put on designing some simple way of building/consuming web services on top of WCF, but they eventually found out that it was not the right solution.
更新(不能作为评论):等等,“将通信技术的一个子集从库/框架移动到另一个”和“替换 WCF”之间存在巨大差异。我个人认为 WCF 是为某种通信概念而设计的,它的设计相当酷,但分布式计算在某种程度上正在转向新的(更简单的)解决方案(看看功能丰富的 SOAP 与精益灵活的 REST,虽然很多人仍然以 RPC 的方式使用 REST),我认为这种编程模式比 WCF 更适合 MVC 架构。努力设计一些在 WCF 之上构建/使用 Web 服务的简单方法,但他们最终发现这不是正确的解决方案。
Not to mention that many developers now use ASP.NET MVC and want to do rest web services for their web app, messing with WCF is often overkill for these kind of things, and I've experienced that on my own skin.
更不用说许多开发人员现在使用 ASP.NET MVC 并希望为他们的 web 应用程序做休息 web 服务,弄乱 WCF 对于这类事情通常是矫枉过正,我自己也经历过。
I think that the routing mechanism is awesome and the right way to go, and if you look closely, they included part of it (with different names and types, but the pattern was there) in WCF. So yeah, i think that if MS don't dismiss that part of WCF WEshould do it. To strictly answer, no, i don't think you'll ever find WebGet/WebInvoke in asp.net mvc*, it just don't fit in.
我认为路由机制很棒而且是正确的方法,如果你仔细观察,他们在 WCF 中包含了它的一部分(具有不同的名称和类型,但模式在那里)。所以是的,我认为如果 MS 不解雇 WCF 的那部分,我们应该这样做。严格回答,不,我认为您永远不会在 asp.net mvc* 中找到 WebGet/WebInvoke,它只是不适合。
Yeah self-host is probably the only bit of WCF contained in ASP.NET MVC4 right now.
是的,自托管可能是目前 ASP.NET MVC4 中唯一包含的 WCF。
回答by Ladislav Mrnka
It looks like WCF itself is somehow dying or at least becoming much less important then it was supposed to be and because of that it also has much less development effort put into its feature set. New features in WCFitself are more cosmetic.
看起来 WCF 本身正在以某种方式消亡,或者至少变得不那么重要,因此它在其功能集中投入的开发工作也少得多。WCF本身的新功能更加美观。
WCF was designed as transport / protocol independent way for inter process communication. Even the idea was independent abstraction it was mostly build on top of SOAP stack. When WCF 3.5 brought support for REST it was mostly hacked in because REST is all about transport dependency. Using transport independent API to support inter process communication which is done through directly using transport features appeared inconvenient. As result MS first released WCF Rest API Starter Kit which never reached RTM but it was preview of features which was later included in WCF 4 and finally in .NET 4.5 or WCF Web API. Because REST is transport dependent and currently used only with HTTP (even it is theoretically possible to use other transport protocol) the API was moved to .NET part which is more suitable for HTTP processing - to currently very popular ASP.NET MVC.
WCF 被设计为进程间通信的独立于传输/协议的方式。即使这个想法是独立的抽象,它也主要构建在 SOAP 堆栈之上。当 WCF 3.5 带来对 REST 的支持时,它主要被黑客入侵,因为 REST 完全是关于传输依赖的。使用与传输无关的 API 来支持通过直接使用传输特性完成的进程间通信显得不方便。因此,MS 首先发布了 WCF Rest API Starter Kit,它从未达到 RTM,但它是功能的预览,后来包含在 WCF 4 中,最终包含在 .NET 4.5 或 WCF Web API 中。因为 REST 依赖于传输并且目前仅用于 HTTP(即使理论上可以使用其他传输协议),API 被移动到更适合 HTTP 处理的 .NET 部分 - 到当前非常流行的 ASP。
回答by ScottWelker
WCF Web API is replaced by ASP.NET Web API which takes features from WCF Web API and merges them with the features from ASPNet MVC. ASP.NET Web API is a new (02/2012) framework for building and consuming HTTP services and a platform for building RESTful service.
WCF Web API 被 ASP.NET Web API 取代,ASP.NET Web API 从 WCF Web API 获取功能并将它们与来自 ASPNet MVC 的功能合并。ASP.NET Web API 是用于构建和使用 HTTP 服务的新 (02/2012) 框架和构建 RESTful 服务的平台。
Although not in the original question it seems worth noting that WCF is alive and well and its REST support remains useful when you have existing SOAP (WS-*) services you must support but want to add REST to reach more clients.
尽管不在最初的问题中,但似乎值得注意的是,WCF 仍然存在并且很好,并且当您拥有必须支持的现有 SOAP (WS-*) 服务但想要添加 REST 以覆盖更多客户端时,它的 REST 支持仍然很有用。
Reference
参考
回答by Bumble
ASP.net web api is lightweight and REST support inbuilt. It is more suitable for mobile applications.WCF is bloated with more options . It depends on the complexity of the system to select one of these.
ASP.net web api 是轻量级的,并且内置了 REST 支持。它更适合移动应用程序。WCF 臃肿,有更多的选择。选择其中之一取决于系统的复杂性。
回答by Chazaq
The following excerpt found on this MSDN pagesummarizes this dilemma well.
在这个 MSDN 页面上找到的以下摘录很好地总结了这个困境。
Use WCF to create reliable, secure web services that accessible over a variety of transports. Use ASP.NET Web API to create HTTP-based services that are accessible from a wide variety of clients. Use ASP.NET Web API if you are creating and designing new REST-style services. Although WCF provides some support for writing REST-style services, the support for REST in ASP.NET Web API is more complete and all future REST feature improvements will be made in ASP.NET Web API.If you have an existing WCF service and you want to expose additional REST endpoints, use WCF and the WebHttpBinding.
使用 WCF 创建可通过各种传输访问的可靠、安全的 Web 服务。使用 ASP.NET Web API 创建可从各种客户端访问的基于 HTTP 的服务。如果您要创建和设计新的 REST 样式服务,请使用 ASP.NET Web API。虽然 WCF 对编写 REST 风格的服务提供了一些支持,但 ASP.NET Web API 中对 REST 的支持更加完整,未来所有的 REST 功能改进都将在 ASP.NET Web API 中进行。如果您有现有的 WCF 服务并且想要公开其他 REST 端点,请使用 WCF 和 WebHttpBinding。
回答by Bhuvnesh
Here is good article on Web Service, WCF and Web API http://goo.gl/T29A5B
这是关于 Web 服务、WCF 和 Web API http://goo.gl/T29A5B 的好文章
Web Service
网络服务
- Based on SOAP and return XML Data
- Support only HTTP protocol. It support only HTTP protocol.
- Consumed by client that able to understand xml SOAP Services.
- Can host on IIS. It can be hosted only on IIS.
- Easy to Learn and understand.
- 基于 SOAP 并返回 XML 数据
- 仅支持 HTTP 协议。它仅支持 HTTP 协议。
- 由能够理解 xml SOAP 服务的客户端使用。
- 可以托管在 IIS 上。它只能托管在 IIS 上。
- 易于学习和理解。
WCF
周转基金
- Based on SOAP and return XML Data. SOAP is heavy compare then JSON and its overhead over network also.
- Enhanced version of web services support multiple protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ via configuration.
- More reliable when both client and server have .Net.
- Its implementation and configuration is complex
- Consumed by client that able to understand xml SOAP Services.
- Self-hosting, IIS and using windows services.
- 基于 SOAP 并返回 XML 数据。SOAP 与 JSON 及其网络开销相比也很重。
- Web 服务的增强版本通过配置支持多种协议,如 TCP、HTTP、HTTPS、命名管道、MSMQ。
- 当客户端和服务器都有 .Net 时更可靠。
- 它的实现和配置很复杂
- 由能够理解 xml SOAP 服务的客户端使用。
- 自托管、IIS 和使用 Windows 服务。
Web API (Web API 2.0)
Web API (Web API 2.0)
- Design specifically for building HTTP Restful Services on .Net Framework.
- Web API easily readable and handy as JSON.
- Support all features of HTTP Like URls, Request/Response, Headers, Caching and Versioning.
- Web API support many HTTP Verbs like GET, POST, PUT, DELETE etc.
- Web API is stateless.
- Web API supports MVC features (controllers, action results, routing, filter, model binders, IOC container or dependency injection)
- Web API can be self-hosted, hosted with in the application and on IIS.
- OWIN (Open Web Interface for .NET) is used for self-Hosting.
- 专为在 .Net Framework 上构建 HTTP Restful 服务而设计。
- Web API 像 JSON 一样易于阅读和方便。
- 支持 HTTP Like URls、请求/响应、标头、缓存和版本控制的所有功能。
- Web API 支持许多 HTTP 动词,如 GET、POST、PUT、DELETE 等。
- Web API 是无状态的。
- Web API 支持 MVC 特性(控制器、动作结果、路由、过滤器、模型绑定器、IOC 容器或依赖注入)
- Web API 可以是自托管的,也可以在应用程序和 IIS 上托管。
- OWIN (Open Web Interface for .NET) 用于自托管。

