[英]ASP.Net Web API vs WCF, which one should I choose in my project
I have read many articles in the web so far, about the differences between WCF and ASP.Net web API. 到目前为止,我已经阅读了很多关于WCF和ASP.Net web API之间差异的文章。 Unfortunately I could not come up to a clear idea about what will serve my purpose.
不幸的是,我无法清楚地知道什么能满足我的目的。 Most of the Articles I have read highlighted on the design point of view of the two web services.
我读过的大多数文章都强调了两个Web服务的设计观点。 But I am confused what will work best for my project and why?
但我很困惑什么最适合我的项目,为什么? Here is my brief description of the project.
这是我对该项目的简要描述。
I need to create a communication channel between two servers (both are written in C#). 我需要在两个服务器之间创建一个通信通道(两者都是用C#编写的)。 The servers will communicate using messages (certain type of commands).
服务器将使用消息(某种类型的命令)进行通信。 The messages sometimes can be only acknowledgements, and sometimes the messages may contain instructions to do some computation.
消息有时只能是确认,有时消息可能包含进行某些计算的指令。 For example, one message can be draw something, or send an SMS etc. And not necessarily the messages will involve any database transactions.
例如,一条消息可以绘制某些内容,或发送短信等。消息不一定涉及任何数据库事务。 But the messages can sometimes send large text files as payload (around 1-5 MB maxm).
但是这些消息有时可以将大文本文件作为有效载荷发送(最大约1-5 MB)。 What I believe WCF is very will surely do this, but can I do the same with ASP.net web API.
我相信WCF肯定会这样做,但我可以用ASP.net web API做同样的事情。 Because so far all the example I have seen for ASP.Net web api: they are good for RESTful services that manipulate some kind of DB store (GET, PUT, DELETE).
因为到目前为止我已经看到了ASP.Net web api的所有示例:它们适用于操纵某种DB存储(GET,PUT,DELETE)的RESTful服务。 But in my case I will need to expose service points that will
但在我的情况下,我将需要公开服务点
do some kind of processing such as return the value of a computation, sending and acknowledging messages, etc.
进行某种处理,例如返回计算值,发送和确认消息等。
Not just manipulating a DB-store. 不只是操纵数据库存储。
So, what should be the best and simplest way to do so? 那么,最好和最简单的方法应该是什么? It is needed to be mentioned that I did not find any straight forward example of achieving this using ASP.Net web API.
需要提到的是,我没有找到使用ASP.Net web API实现此目的的任何直接示例。
The Question you have asked is an overly-broad or primarily opinion-based, and its hard to give an example for what you have asked. 您提出的问题是过于宽泛或主要以意见为基础,很难为您所提出的问题举例说明。
Important Points: 重点:
- Firstly, if you are going to create a service which would be used on different platforms, then go with WCF.
首先,如果您要创建将在不同平台上使用的服务,那么请使用WCF。
- Secondly, if you are creating internet service which is going to use external resource , then go with Web API.
其次,如果您要创建将使用外部资源的 Internet服务,那么请使用Web API。
- Web API is the best choice if you are going to create a service for low bandwidth devices or mobile devices to access client.HTTP request/response is also more readable as compared to SOAP because it contains header, body, etc. which makes it complex.
如果要为低带宽设备或移动设备创建服务以访问客户端,Web API是最佳选择。与SOAP相比,HTTP请求/响应也更具可读性,因为它包含标题,正文等,这使得它变得复杂。
Just take few minutes and read the below article, until you get complete understanding of few principles. 只需几分钟时间阅读以下文章,直到您完全了解一些原则。
Original Source Can be found Here , Here and Here . 原始来源可以在这里 , 这里和这里找到 。
To whom choose between WCF or WEB API : 在WCF或WEB API之间选择谁:
Why to choose Web API 为什么选择Web API
My Opinion: 我的意见:
I hope this gives you a clear idea about what to choose... 我希望这能让你明白选择什么......
First things first, RESTful is a stateless and uniform interface norm that can be applied to web services. 首先,RESTful是一种无状态和统一的接口规范,可以应用于Web服务。 It doesn't have to be automatically and only plain old CRUD service backed by a DB.
它不必是自动的,只有DB支持的普通旧CRUD服务。
In the real world, we can hardly say that all web REST API respect fully the norm, in fact they don't most of the time, especially the stateless part. 在现实世界中,我们很难说所有Web REST API都完全遵循规范,事实上它们并不是大部分时间,尤其是无状态部分。
For your message based API, especially if it's bidirectional and event based, you can use websockets and consider the REST API a way to expose an uniform, stateless web interface to create those. 对于基于消息的API,特别是如果它是双向的和基于事件的,您可以使用websockets并考虑使用REST API来公开统一的无状态Web界面来创建它们。 And yes you can use websockets with ASP.NET WebApi there's plenty of tutorial out there, even for the newer ASP.NET Core.
是的,你可以使用带有ASP.NET WebApi的websockets,那里有很多教程,即使对于较新的ASP.NET Core也是如此。
The "between services" interactions part is no different than the usual Web browser <=> Web service, you're just using C# code instead of JS for the client. “服务之间”交互部分与通常的Web浏览器<=> Web服务没有什么不同,您只是为客户端使用C#代码而不是JS。
I can hardly recommend WCF that uses SOAP since it's hardly portable considering the web standards nowadays. 我很难推荐使用SOAP的WCF,因为考虑到现在的Web标准,它几乎不可移植。 For instance if want to use a browser client instead of another ASP.NET service, well you'll have to do additional code client side to handle supports.
例如,如果想要使用浏览器客户端而不是其他ASP.NET服务,那么您将不得不做额外的代码客户端来处理支持。
You can use WCF websockets , providing the almost all the advantages of WCF SOAP. 您可以使用WCF websockets ,提供WCF SOAP的几乎所有优点。
tl;dr : tl;博士:
WebAPI is not just good for RESTful webservices. WebAPI不仅适用于RESTful Web服务。 You can easily send requests to a WebAPI controller and handle it the way you want : calculations, sending messages, interact with a CRM, interact with DB or anything else.
您可以轻松地将请求发送到WebAPI控制器并以您希望的方式处理它:计算,发送消息,与CRM交互,与数据库或其他任何内容交互。
WCF was created to manage SOAP based webservices and brings extra complexity. 创建WCF是为了管理基于SOAP的Web服务并带来额外的复杂性。 It handles TCP, Mime...
它处理TCP,Mime ......
If you just need to handle HTTP requests, the simplest way is to go with WebAPI. 如果您只需要处理HTTP请求,最简单的方法是使用WebAPI。
我建议使用Web API,如果它是用于RESTful服务的,因为WCF从未被用作Restful服务,尽管你可以作为Web API特别为此服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.