WCF 与 ASP.NET Web API

发布于 2025-01-07 17:50:52 字数 1436 浏览 0 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

紫﹏色ふ单纯 2025-01-14 17:50:52

对于我们来说,WCF 用于 SOAP,而 Web API 用于 REST。我希望 Web API 也支持 SOAP。我们没有使用 WCF 的高级功能。以下是来自 MSDN 的比较:

在此处输入图像描述

For us, WCF is used for SOAP and Web API for REST. I wish Web API supported SOAP too. We are not using advanced features of WCF. Here is comparison from MSDN:

enter image description here

少年亿悲伤 2025-01-14 17:50:52

新的 ASP.NET Web API 是之前的 WCF Web API 项目(尽管一些概念已更改) 。

WCF 最初是为了支持基于 SOAP 的服务而创建的。对于更简单的 RESTful 或 RPCish 服务(认为像 jQuery 这样的客户端),ASP.NET Web API 应该是不错的选择。

The new ASP.NET Web API is a continuation of the previous WCF Web API project (although some of the concepts have changed).

WCF was originally created to enable SOAP-based services. For simpler RESTful or RPCish services (think clients like jQuery) ASP.NET Web API should be good choice.

淤浪 2025-01-14 17:50:52

ASP.net Web API 是基于 HTTP 和 REST 的 GET、POST、PUT、DELETE,具有众所周知的 ASP.net MVC 编程风格和 JSON 可返回; Web API 适用于所有轻量级进程和基于纯 HTTP 的组件。对于继续使用 WCF 的人来说,即使是简单或最简单的单一 Web 服务,也会带来所有额外的负担。对于 ajax 或动态调用的轻量级简单服务,WebApi 始终可以满足需求。这巧妙地补充了 ASP.net MVC,或者同时对 ASP.net MVC 有所帮助。

查看播客:Hansel 分钟播客 264 - 这不是你父亲的 WCF - 有关 Glenn Block 的 WebAPI 的一切 作者:Scott Hanselman 了解更多信息。

ASP.net Web API is all about HTTP and REST based GET,POST,PUT,DELETE with well know ASP.net MVC style of programming and JSON returnable; web API is for all the light weight process and pure HTTP based components. For one to go ahead with WCF even for simple or simplest single web service it will bring all the extra baggage. For light weight simple service for ajax or dynamic calls always WebApi just solves the need. This neatly complements or helps in parallel to the ASP.net MVC.

Check out the podcast : Hanselminutes Podcast 264 - This is not your father's WCF - All about the WebAPI with Glenn Block by Scott Hanselman for more information.

云淡风轻 2025-01-14 17:50:52

在下面列出的场景中,您应该选择 WCF:

  1. 如果您需要通过 TCP、MSMQ 或 MIME 等协议发送数据
  2. 如果使用客户端只知道如何使用 SOAP 消息

WEB API 是用于开发 RESTful/HTTP 服务的框架。

有很多客户端不理解 SOAP(例如浏览器、HTML5),在这些情况下 WEB API 是一个不错的选择。

HTTP 服务标头指定如何保​​护服务、如何缓存信息、消息正文的类型,并且 HTTP 正文可以指定任何类型的内容,如 HTML,而不仅仅是 XML 作为 SOAP 服务。

In the scenarios listed below you should go for WCF:

  1. If you need to send data on protocols like TCP, MSMQ or MIME
  2. If the consuming client just knows how to consume SOAP messages

WEB API is a framework for developing RESTful/HTTP services.

There are so many clients that do not understand SOAP like Browsers, HTML5, in those cases WEB APIs are a good choice.

HTTP services header specifies how to secure service, how to cache the information, type of the message body and HTTP body can specify any type of content like HTML not just XML as SOAP services.

厌倦 2025-01-14 17:50:52

自从使用两者到现在,我发现WCF和Web API之间有很多区别。两种技术栈都适合不同的场景,所以不能说哪个更好,这取决于配置和场景。

Properties              ASP.Net Web API                         WCF
--------------------------------------------------------------------------------------------------
End point (mainly)      Http based                              SOAP based
Service Type            Front End                               Back-end
Support                 caching, compression, versioning        No
Framework               ASP.net                                 WCF
Orientation             Resource Oriented                       Service Oriented
Transports              http                                    http, tcp, MSMQ, Named pipe
Message pattern         Request reply                           request Reply, one way, duplex
Configuration overhead  Less                                    Much
Security                lesser than WCF (web standard security) Very high (WS-I standard)
Hosting                 IIS                                     IIS, Windows Service, Self hosting
Performance             Fast                                    A bit slower than Web API
In use from             .NET 4.0                                .NET 3.5

注:数据不仅是我的观点,也是从其他官方网站收集的。

Since using both till now, I have found many differences between WCF and Web API. Both technology stacks are suited well to different scenarios, so it is not possible to say which is better, this depends on configuration and scenario.

Properties              ASP.Net Web API                         WCF
--------------------------------------------------------------------------------------------------
End point (mainly)      Http based                              SOAP based
Service Type            Front End                               Back-end
Support                 caching, compression, versioning        No
Framework               ASP.net                                 WCF
Orientation             Resource Oriented                       Service Oriented
Transports              http                                    http, tcp, MSMQ, Named pipe
Message pattern         Request reply                           request Reply, one way, duplex
Configuration overhead  Less                                    Much
Security                lesser than WCF (web standard security) Very high (WS-I standard)
Hosting                 IIS                                     IIS, Windows Service, Self hosting
Performance             Fast                                    A bit slower than Web API
In use from             .NET 4.0                                .NET 3.5

Note: The data is not only my view, it is also collected from other official websites.

梦巷 2025-01-14 17:50:52

WCF 将为您提供如此多的开箱即用功能,它甚至无法与任何东西相媲美。除非您想自己实现(仅举几例)身份验证、授权、加密、排队、限制、可靠消息传递、日志记录、会话等。 WCF 不仅是 Web 服务; WCF是SOA的开发平台。

WCF will give you so much of out the box, it's not even comparable to anything. Unless you want to do on your own implementation of (to name a few) authentication, authorization, encryption, queuing, throttling, reliable messaging, logging, sessions and so on. WCF is not [only] web services; WCF is a development platform for SOA.

你的往事 2025-01-14 17:50:52

我为什么回答:

我花了很多时间来理解这两种技术之间的区别。我将把我认为的所有这些要点放在这里,“如果我在寻找这个答案时有这些要点,那么我很早就决定选择我所需的技术。”

信息来源:

Microsoft® Visual Studio® 2015 Unleashed

ISBN-13: 978-0-672-33736-9 ISBN-10: 0-672-33736-3

为什么选择 ASP.NET Web API 和 WCF:

在比较 ASP 技术之前.NET Web API 和 WCF,重要的是要了解创建 Web 服务实际上有两种样式/标准:REST(表述性状态传输)和SOAP/WSDL。 SOAP/WSDL 是构建 Web 服务的原始标准。然而,它很难使用,并且消息格式庞大(如 XML),从而降低了性能。基于 REST 的服务很快成为替代方案。它们更容易编写,因为它们利用 HTTP 的基本结构(GET、POST、PUT、DELETE)并且通常使用较小的消息格式(如 JSON)。因此,基于 REST 的 HTTP 服务现在成为编写严格针对 Web 的服务的标准。

让我们定义 ASP.NET Web API 的用途

ASP.NET Web API 是 Microsoft 用于开发基于 REST 的 HTTP Web 服务的技术。 (它很久以前就取代了基于 SOAP/WSDL 的 Microsoft ASMX。)Web API 可以轻松地基于所有浏览器和本机设备都能理解的 HTTP 协议编写强大的服务。这使您能够创建服务来支持您的应用程序并从其他 Web 应用程序、平板电脑、移动电话、PC 和游戏控制台调用它们。如今,大多数为利用现有的 Web 连接而编写的应用程序都以某种方式使用 HTTP 服务。

现在让我们定义 WCF 的目的:

通过 Internet 进行通信并不总是最有效的方式。例如,如果客户端和服务都存在于相同的技术(甚至同一台机器)上,则它们通常可以协商更有效的通信方式(例如 TCP/IP)。服务开发人员发现自己做出了他们试图避免的相同选择。他们现在必须在创建高效的内部服务和通过互联网进行广泛的访问之间做出选择。而且,如果他们必须同时支持两者,他们可能必须创建其服务的多个版本,或者至少创建单独的代理来访问其服务。 这是微软通过 WCF 解决的问题

使用 WCF,您可以创建服务而无需考虑边界。然后,您可以让 WCF 负责以最有效的方式运行您的服务,具体取决于调用客户端。为了管理此任务,WCF 使用端点的概念。您的服务可能有多个端点(在设计时或部署后配置)。每个端点指示服务如何支持调用客户端:通过 Web、通过远程处理、通过 Microsoft 消息队列 (MSMQ) 等等。 WCF 使您能够专注于创建服务功能。它担心如何最有效地与来电客户交谈。通过这种方式,单个 WCF 服务可以有效地支持许多不同的客户端类型。

WCF 示例:

考虑以下示例:

客户数据在应用程序之间共享。每个应用程序可能是在不同的平台上编写的,并且可能存在于不同的位置。您可以将客户界面提取到 WCF 服务中,该服务提供对共享客户数据的公共访问。这可以集中数据、减少重复、消除同步并简化管理。此外,通过使用 WCF,您可以将服务端点配置为以对调用客户端有意义的方式工作。该图显示了之前在 WCF 服务中集中访问客户数据的示例。

就是这样WCF 服务于不同的客户端

结论:

i) 何时选择 Web API:

不可否认的是,基于 REST 的 HTTP 服务(例如使用 ASP.NET Web API 创建的服务)已成为构建 Web 服务的标准。这些服务为 Web 开发人员构建服务提供了一种简单、直接的方法。 Web 开发人员了解 HTTP GET 和 POST,因此能够很好地适应这些类型的服务。因此,如果您正在编写严格针对 HTTP服务,那么 ASP.NET Web API 是合理的选择。

ii) 何时选择 WCF:

当您需要支持基于不同协议和消息格式的多个服务端点时,WCF 技术非常有用。 Microsoft BizTalk 等产品利用 WCF 创建强大的服务,这些服务可以在 Web 上使用,也可以通过不同的机器对机器配置使用。但是,如果您确实需要编写一个在连接时通过 TCP/IP 进行通信的应用程序到本地网络并在网络外通过 HTTP 工作,WCF 就是您的答案

请注意:

Web 开发人员通常认为 WCF 的开发更加困难和复杂。因此,如果您没有预见到对多协议服务的需求,您可能会坚持使用 ASP.NET Web API。

Why I'm answering:

I took huge amount of time to understand the difference between these two technologies. I'll put all those points here that I think "If I had these points at the time when I was wondering around in search of this answer, then I have decided very earlier in selecting my required technology."

Source of Information:

Microsoft® Visual Studio® 2015 Unleashed

ISBN-13: 978-0-672-33736-9 ISBN-10: 0-672-33736-3

Why ASP.NET Web API and WCF:

Before comparing the technologies of ASP.NET Web API and WCF, it is important to understand there are actually two styles/standards for creating web services: REST (Representational State Transfer) and SOAP/WSDL. The SOAP/WSDL was the original standard on which web services were built. However, it was difficult to use and had bulky message formats (like XML) that degraded performance. REST-based services quickly became the alternative. They are easier to write because they leverage the basic constructs of HTTP (GET, POST, PUT, DELETE) and typically use smaller message formats (like JSON). As a result, REST-based HTTP services are now the standard for writing services that strictly target the Web.

Let's define purpose of ASP.NET Web API

ASP.NET Web API is Microsoft’s technology for developing REST-based HTTP web services. (It long ago replaced Microsoft’s ASMX, which was based on SOAP/WSDL.) The Web API makes it easy to write robust services based on HTTP protocols that all browsers and native devices understand. This enables you to create services to support your application and call them from other web applications, tablets, mobile phones, PCs, and gaming consoles. The majority of applications written today to leverage the ever present Web connection use HTTP services in some way.

Let's now define purpose of WCF:

Communicating across the Internet is not always the most efficient means. For example, if both the client and the service exist on the same technology (or even the same machine), they can often negotiate a more efficient means to communicate (such as TCP/IP). Service developers found themselves making the same choices they were trying to avoid. They now would have to choose between creating efficient internal services and being able to have the broad access found over the Internet. And, if they had to support both, they might have to create multiple versions of their service or at least separate proxies for accessing their service. This is the problem Microsoft solved with WCF.

With WCF, you can create your service without concern for boundaries. You can then let WCF worry about running your service in the most efficient way, depending on the calling client. To manage this task, WCF uses the concept of endpoints. Your service might have multiple endpoints (configured at design time or after deployment). Each endpoint indicates how the service might support a calling client: over the Web, via remoting, through Microsoft Message Queuing (MSMQ), and more. WCF enables you to focus on creating your service functionality. It worries about how to most efficiently speak with calling clients. In this way, a single WCF service can efficiently support many different client types.

Example of WCF:

Consider the example:

The customer data is shared among the applications. Each application might be written on a different platform, and it might exist in a different location. You can extract the customer interface into a WCF service that provides common access to shared customer data. This centralizes the data, reduces duplication, eliminates synchronization, and simplifies management. In addition, by using WCF, you can configure the service endpoints to work in the way that makes sense to the calling client. Figure shows the example from before with centralized access of customer data in a WCF service.

This is how WCF serves different clients

Conclusion:

i) When to choose Web API:

There is no denying that REST-based HTTP services like those created using ASP.NET Web API have become the standard for building web services. These services offer an easy, straightforward approach for web developers building services. Web developers understand HTTP GET and POST and thus adapt well to these types of services. Therefore, if you are writing services strictly targeted to HTTP, ASP.NET Web API is the logical choice.

ii) When to choose WCF:

The WCF technology is useful when you need to support multiple service endpoints based on different protocols and message formats. Products like Microsoft BizTalk leverage WCF for creating robust services that can be used over the Web as well via different machine-to-machine configurations.If, however, you do need to write an application that communicates over TCP/IP when connected to the local network and works over HTTP when outside the network, WCF is your answer.

Be Warned:

Web developers often view WCF as more difficult and complex to develop against. Therefore, if you do not foresee the need for multiprotocol services, you would likely stick with ASP.NET Web API.

躲猫猫 2025-01-14 17:50:52

MSDN 上有关于

WCF 和 ASP 的比较。 NET Web API

对于我来说,选择是关于客户是谁,以及他们位于哪里?

公司内部网络和基于.NET的客户端:使用带有TCP绑定的WCF(比HTTP更快的通信)

公司网络外部,并使用PHP、Python等多种技术:将 Web API 与 REST 结合使用

There is a comparison on MSDN about this

WCF and ASP.NET Web API

For me, the choice was about Who the clients are, and where are they located?

Within the company Network and .NET based clients : Use WCF with TCP binding (Fast communication than HTTP)

Outside the company Network, and use diverse technologies like PHP, Python etc: Use Web API with REST

饮惑 2025-01-14 17:50:52

从业务角度来说,WebApi 缺乏 WSDL,因此开发人员应该手动记录所有内容。例如,如果WebApi操作返回一个对象列表,那么客户端应该手动创建对象,即WebAPI确实很容易出现定义错误。

Webapi的优点是比WCF更轻量。

Business speaking, WebApi lacks of a WSDL, so the developers should document all manually. And if, for example, the WebApi operation returns a list of objects then, the client should creates the objects manually, i.e. WebAPI is really prone to errors of definitions.

The pro of Webapi is its more lightweight than WCF.

清泪尽 2025-01-14 17:50:52

关于“WebApi 缺乏 WSDL”的说法,有多种方法可以生成 Rest 客户端。一种流行的方法是 Swagger UI / (Swashbukkle Nuget)。这提供了丰富的接口来理解 REST 端点的输入和输出架构以及测试端点的在线工具。

JSON LD(Json 链接文档)是另一个新兴标准,它将通过公开具有更好语义的 JSON 模式来进一步改善基于 JSON 的 REST 开发人员体验。

Regarding the statement "WebApi lacks of WSDL" there are several ways to generate Rest client. One popular approach is Swagger UI / (Swashbukkle Nuget). This gives a rich interface to understand the REST end point's input and output schema and online tool to test the end points.

JSON LD (Json Linked Documents) is another emerging standard which will further improve the JSON based REST developer experience by exposing the JSON schema with better semantics.

烟花肆意 2025-01-14 17:50:52

使用 wcf,我们可以为多个端点(如 tcp、http)配置和公开相同的服务支持。如果您希望您的服务仅基于 http,那么最好使用 Web API。与 wcf 相比,Web API 的配置非常少,并且速度比 wcf 快一些。 Wcf还支持restful服务。如果您有 .Net Framework 3.5 的限制,那么您的选择是 wcf。

With wcf we can configure and expose the same service support for multiple endpoints like tcp, http.if you want your service to be only http based then it will be better to go with web API. Web API has very less configuration when compared to wcf and is bit faster than wcf. Wcf also supports restful services. If you have limitation of .Net framework 3.5 then your option is wcf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文