简体   繁体   English

ASP.Net Web API与WCF,我应该在我的项目中选择哪一个

[英]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之间选择谁:

  1. Choose WCF when you want to create a service that should support special scenarios such as one way messaging, message queues, duplex communication etc. 如果要创建应支持特殊方案的服务(例如单向消息传递,消息队列,双工通信等),请选择WCF。
  2. Choose WCF when you want to create a service that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transport channels are unavailable. 如果要创建可在可用时使用快速传输通道的服务(例如TCP,命名管道,甚至UDP(在WCF 4.5中)),请选择WCF,并且还希望在所有其他传输通道不可用时支持HTTP。
  3. Choose Web API when you want to create a resource-oriented services over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats). 如果要通过HTTP创建可以使用HTTP的完整功能(如URI,请求/响应头,缓存,版本控制,各种内容格式)的面向资源的服务,请选择Web API。
  4. Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets. 如果要将服务公开给广泛的客户,包括浏览器,手机,iPhone和平板电脑,请选择Web API。

Why to choose Web API 为什么选择Web API

  • Web API doesn't have tedious and extensive configuration like WCF REST service. Web API没有像WCF REST服务那样繁琐而广泛的配置。
  • It is very simple, creating service with Web API. 它非常简单,使用Web API创建服务。 Where as With WCF REST, service creation is bit difficult (requires clear understanding of configurations). 与使用WCF REST一样,服务创建有点困难(需要清楚地了解配置)。
  • Web API is only based on HTTP and HTTPS and easy to define, expose and consume in a REST-full way. Web API仅基于HTTP和HTTPS,易于以REST完全方式定义,公开和使用。
  • Web API is light weight architecture and good for devices which have limited bandwidth like smart phones. Web API是轻量级架构,适用于智能手机等带宽有限的设备。

My Opinion: 我的意见:

  • Simplest way to do so - Web API (Since you dont have any examples for this) 最简单的方法 - Web API(因为你没有任何这方面的例子)
  • Hardest Way is (Configurations) - WCF (Better go with WCF, since you have examples) 最难的方式是(配置) - WCF(最好使用WCF,因为你有例子)

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;博士:

  • You can mix RESTful and Websockets, it can actually be better than going full REST or full Websockets 您可以混合RESTful和Websockets,它实际上可能比完整的REST或完整的Websockets更好
  • It's personal preference to use SOAP over websockets but do comes with a potential technical debt considering what you want to do afterwards 个人倾向于在websockets上使用SOAP,但考虑到你之后想要做什么,确实会带来潜在的技术债务
  • Message API between services is no different than a message API between a service and a browser 服务之间的消息API与服务和浏览器之间的消息API没有什么不同

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.

相关问题 在哪种情况下我应该选择Silverlight或ASP.NET MVC - In which situation should I choose Silverlight or ASP.NET MVC ASP.NET vs SharePoint - 哪一个更适合Web开发人员? - ASP.NET vs SharePoint - which one is better for web developers? ASP.NET Web Api作为一个解决方案中的独立项目 - ASP.NET Web Api as a standalone project in one solution 我应该为 VS 2019 选择哪个 WPF 项目模板? [等候接听] - Which WPF project template should I choose for VS 2019? [on hold] 哪些场景需要 ASP.net Web API 来使用 WCF 服务? - What are some scenarios in which you need ASP.net Web API to consume a WCF Service? WCF服务应用程序VS ASP.NET Web应用程序? - WCF Service Application VS ASP.NET Web Application? 我应该为 .NET Core 中的库项目选择哪个目标框架 - Which target framework should I choose for library project in .NET Core global.asax中的一个全局WCF ChannelFactory是否会限制我的asp.net项目? - Will one global WCF ChannelFactory in global.asax limit my asp.net project? 我是否应该通过 asp.net 核心 3.1 web ZDB974238D704A 中的所有 class 方法检测取消令牌? - Should I plumb Cancellation Tokens through all of my class methods in an asp.net core 3.1 web API? ASP.NET MVC + WF + WCF + SERVICE LAYER我如何在一个项目中使用它们? - ASP.NET MVC + WF + WCF + SERVICE LAYER how i can use thats all in one project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM