我花了几个月的时间试图掌握WCF背后的概念,最近我开发了我的第一个WCF服务应用程序。
为了理解配置文件中的所有设置,我花了不少功夫。 我不相信环境,但似乎你可以用它做惊人的事情。
前几天我发现微软推出了一个叫ASP的新东西。NET Web API。
据我所知,这是一个RESTful框架,非常容易使用和实现。
现在,我试图弄清楚这两个框架之间的主要区别是什么,如果我应该尝试用新的API转换我的旧WCF服务应用程序。
有人能帮我了解一下这两种语言的区别和用法吗?
我花了几个月的时间试图掌握WCF背后的概念,最近我开发了我的第一个WCF服务应用程序。
为了理解配置文件中的所有设置,我花了不少功夫。 我不相信环境,但似乎你可以用它做惊人的事情。
前几天我发现微软推出了一个叫ASP的新东西。NET Web API。
据我所知,这是一个RESTful框架,非常容易使用和实现。
现在,我试图弄清楚这两个框架之间的主要区别是什么,如果我应该尝试用新的API转换我的旧WCF服务应用程序。
有人能帮我了解一下这两种语言的区别和用法吗?
当前回答
ASP.net Web API是基于HTTP和REST的GET,POST,PUT,DELETE,熟悉ASP.net MVC风格的编程和JSON可返回;web API适用于所有轻量级进程和纯基于HTTP的组件。对于使用WCF的人来说,即使是简单或最简单的单个web服务,它也会带来所有额外的负担。对于ajax或动态调用的轻量级简单服务,WebApi总是能解决这一需求。这是对ASP.net MVC的补充或帮助。
查看播客:Hanselminutes播客264 -这不是你父亲的WCF -由Scott Hanselman与Glenn Block一起介绍WebAPI以获取更多信息。
其他回答
从业务角度讲,WebApi缺少WSDL,因此开发人员应该手动编写文档。如果,例如,WebApi操作返回一个对象列表,那么客户端应该手动创建对象,也就是说,WebApi确实容易出现定义错误。
Webapi的优点是它比WCF更轻量级。
关于“WebApi缺少WSDL”这句话,有几种方法来生成Rest客户端。一个流行的方法是Swagger UI / (swashbuckkle Nuget)。这提供了一个丰富的接口来理解REST端点的输入和输出模式,以及用于测试端点的在线工具。
JSON LD (JSON链接文档)是另一个新兴的标准,它将通过更好的语义公开JSON模式来进一步改善基于JSON的REST开发体验。
对我们来说,WCF用于SOAP, Web API用于REST。我希望Web API也支持SOAP。我们没有使用WCF的高级特性。以下是来自MSDN的对比:
WCF会给你很多开箱即用的东西,它甚至是任何东西都无法比拟的。除非您想自己实现(举几个例子)身份验证、授权、加密、排队、节流、可靠消息传递、日志记录、会话等等。WCF不仅仅是web服务;WCF是面向SOA的开发平台。
我为什么要回答:
我花了大量的时间来理解这两种技术之间的区别。我把所有这些点都写在这里,我认为“如果我在四处寻找答案的时候就有了这些点,那么我就会更早地决定选择我需要的技术。”
资料来源:
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。