C# WCF 与 ASP.NET Web API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9348639/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:00:01  来源:igfitidea点击:

WCF vs ASP.NET Web API

c#wcfasp.net-web-api

提问by LeftyX

I've spent a few months trying to grasp the concepts behind WCF and recently I've developed my first WCF service application.

我花了几个月的时间试图掌握 WCF 背后的概念,最近我开发了我的第一个 WCF 服务应用程序。

I've struggled quite a bit to understand all the settings in the config file.
I am not convinced about the environment but it seems that you can do amazing stuff with it.

我一直在努力理解配置文件中的所有设置。
我不相信环境,但似乎你可以用它做一些了不起的事情。

The other day I've found out that Microsoft has come out with a new thing called ASP.NET Web API.

前几天我发现 Microsoft 推出了一个名为ASP.NET Web API的新东西。

For what I can read it's a RESTful framework, very easy to use and implement.

据我所知,它是一个RESTful 框架,非常易于使用和实现。

Now, I am trying to figure out what are the main differences between the 2 frameworks and if I should try and convert my oldWCF service application with the new API.

现在,我试图弄清楚这两个框架之间的主要区别是什么,以及我是否应该尝试使用新 API转换我的WCF 服务应用程序。

Could someone, please, help me to understand the differences and usage of each?

有人可以帮助我了解每个的差异和用法吗?

采纳答案by marcind

The new ASP.NET Web APIis a continuation of the previous WCF Web APIproject (although some of the concepts have changed).

新的ASP.NET Web API是先前WCF Web API项目的延续(尽管某些概念已经改变)。

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.

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

回答by Naveen Vijay

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.

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

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

查看播客:Hanselminutes Podcast 264 - 这不是你父亲的 WCF - 关于 WebAPI 和Scott Hanselman 的Glenn Block的更多信息。

回答by Kris

In the scenarios listed below you should go for WCF:

在下面列出的场景中,您应该使用 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
  1. 如果您需要通过 TCP、MSMQ 或 MIME 等协议发送数据
  2. 如果消费客户端只知道如何消费 SOAP 消息

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

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

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

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

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.

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

回答by Manish Jain

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:

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

enter image description here

在此处输入图片说明

回答by tridy

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.

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

回答by magallanes

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.

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

The pro of Webapi is its more lightweight than WCF.

Webapi 的优点是它比 WCF 更轻量级。

回答by Venkatesh Muniyandi

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.

关于“WebApi 缺少 WSDL”的说法,有几种方法可以生成 Rest 客户端。一种流行的方法是 Swagger UI / (Swashbukkle Nuget)。这提供了一个丰富的界面来了解 REST 端点的输入和输出模式以及用于测试端点的在线工具。

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.

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

回答by Rajput

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.

自从使用两者到现在,我发现 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

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

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

回答by Arshad Mohammad

There is a comparison on MSDN about this

MSDN上有一个关于这个的比较

WCF and ASP.NET Web API

WCF 和 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)

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

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

公司外部网络,并使用多种技术,如 PHP、Python 等:使用 Web API 和 REST

回答by Muhammad Waqas Dilawar

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

微软?视觉工作室?2015 释放

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

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

Why ASP.NET Web API and WCF:

为什么使用 ASP.NET Web API 和 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.

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

Let's define purpose of ASP.NET Web API

让我们定义 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.

ASP.NET Web API 是 Microsoft 用于开发基于 REST 的 HTTP Web 服务的技术。(它很久以前就取代了基于 SOAP/WSDL 的 Microsoft 的 ASMX。)Web API 使得基于所有浏览器和本机设备都能理解的 HTTP 协议编写健壮的服务变得容易。这使您能够创建服务来支持您的应用程序并从其他 Web 应用程序、平板电脑、手机、PC 和游戏机调用它们。今天编写的大多数应用程序都以某种方式使用 HTTP 服务来利用一直存在的 Web 连接。

Let's now define purpose of WCF:

现在让我们定义 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.

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

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

Example of WCF:

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.

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

This is how WCF serves different clients

这就是 WCF 服务不同客户端的方式

Conclusion:

结论:

i) When to choose Web API:

i) 何时选择 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 servicesstrictly targeted to HTTP, ASP.NET Web API is the logical choice.

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

ii) When to choose WCF:

ii) 何时选择 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.

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

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.

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