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

为了理解配置文件中的所有设置,我花了不少功夫。 我不相信环境,但似乎你可以用它做惊人的事情。

前几天我发现微软推出了一个叫ASP的新东西。NET Web API。

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

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

有人能帮我了解一下这两种语言的区别和用法吗?


当前回答

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

最初创建WCF是为了启用基于soap的服务。对于更简单的RESTful或rpish服务(考虑jQuery这样的客户端)。NET Web API应该是一个很好的选择。

其他回答

WCF会给你很多开箱即用的东西,它甚至是任何东西都无法比拟的。除非您想自己实现(举几个例子)身份验证、授权、加密、排队、节流、可靠消息传递、日志记录、会话等等。WCF不仅仅是web服务;WCF是面向SOA的开发平台。

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

注:数据不只是个人观点,也有其他官方网站的数据。

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

最初创建WCF是为了启用基于soap的服务。对于更简单的RESTful或rpish服务(考虑jQuery这样的客户端)。NET Web API应该是一个很好的选择。

我为什么要回答:

我花了大量的时间来理解这两种技术之间的区别。我把所有这些点都写在这里,我认为“如果我在四处寻找答案的时候就有了这些点,那么我就会更早地决定选择我需要的技术。”

资料来源:

Microsoft® Visual Studio® 2015 Unleashed

Isbn-13: 978-0-672-33736-9 isbn-10: 0-672-33736-3

为什么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

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.

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

WCF示例:

请看下面的例子:

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.

结论:

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。

在下面列出的场景中,你应该使用WCF:

如果您需要在TCP、MSMQ或MIME等协议上发送数据 如果消费客户端只知道如何消费SOAP消息

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

有很多客户端不理解SOAP,比如浏览器,HTML5,在这些情况下,WEB api是一个很好的选择。

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