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

时间:2022-04-09 19:18:30

I have read many articles in the web so far, about the differences between WCF and 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. But I am confused what will work best for my project and why? Here is my brief description of the project.

到目前为止,我已经阅读了很多关于WCF和ASP.Net Web API之间差异的文章。不幸的是,我无法清楚地知道什么能满足我的目的。我读过的大多数文章都强调了两个Web服务的设计观点。但我很困惑什么对我的项目最有效,为什么?这是我对该项目的简要描述。

I need to create a communication channel between two servers (both are written in 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). What I believe WCF is very will surely do this, but can I do the same with 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). But in my case I will need to expose service points that will

我需要在两台服务器之间创建一个通信通道(两者都是用C#编写的)。服务器将使用消息(某种类型的命令)进行通信。消息有时只能是确认,有时消息可能包含进行某些计算的指令。例如,一条消息可以绘制内容,或发送短信等。消息不一定涉及任何数据库事务。但是这些消息有时可以将大文本文件作为有效载荷发送(最大约1-5 MB)。我相信WCF肯定会这样做,但我可以用ASP.net web API做同样的事情。因为到目前为止我已经看到了ASP.Net web api的所有示例:它们适用于操纵某种DB存储(GET,PUT,DELETE)的RESTful服务。但在我的情况下,我将需要公开服务点

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实现此目的的任何直接示例。

4 个解决方案

#1


9  

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.
  2. 如果要创建应支持特殊方案(如单向消息传递,消息队列,双工通信等)的服务,请选择WCF。

  3. 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.
  4. 如果要创建可在可用时使用快速传输通道的服务(例如TCP,命名管道,甚至是UDP(在WCF 4.5中)),请选择WCF,并且还希望在所有其他传输通道不可用时支持HTTP。

  5. 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).
  6. 如果要通过HTTP创建可以使用HTTP的完整功能(如URI,请求/响应头,缓存,版本控制,各种内容格式)的面向资源的服务,请选择Web API。

  7. Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.
  8. 如果要将服务公开给广泛的客户,包括浏览器,手机,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. Where as With WCF REST, service creation is bit difficult (requires clear understanding of configurations).
  • 它非常简单,使用Web API创建服务。与使用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...

我希望这能让你明白选择什么......

#2


1  

First things first, RESTful is a stateless and uniform interface norm that can be applied to web services. It doesn't have to be automatically and only plain old CRUD service backed by a DB.

首先,RESTful是一种无状态和统一的接口规范,可以应用于Web服务。它不必是自动的,只有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. And yes you can use websockets with ASP.NET WebApi there's plenty of tutorial out there, even for the newer ASP.NET Core.

对于基于消息的API,特别是如果它是双向的和基于事件的,您可以使用websockets并考虑使用REST API来公开统一的无状态Web界面来创建它们。是的,你可以使用带有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. 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.

我很难推荐使用SOAP的WCF,因为考虑到现在的Web标准,它几乎不可移植。例如,如果想要使用浏览器客户端而不是其他ASP.NET服务,那么您将不得不做额外的代码客户端来处理支持。

You can use WCF websockets, providing the almost all the advantages of WCF SOAP.

您可以使用WCF websockets,提供WCF SOAP的几乎所有优点。

tl;dr :

  • 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没有什么不同

#3


0  

WebAPI is not just good for RESTful webservices. 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不仅适用于RESTful Web服务。您可以轻松地将请求发送到WebAPI控制器并按照您的需要进行处理:计算,发送消息,与CRM交互,与数据库或其他任何内容交互。

WCF was created to manage SOAP based webservices and brings extra complexity. It handles TCP, Mime...

创建WCF是为了管理基于SOAP的Web服务并带来额外的复杂性。它处理TCP,Mime ......

If you just need to handle HTTP requests, the simplest way is to go with WebAPI.

如果您只需要处理HTTP请求,最简单的方法是使用WebAPI。

#4


0  

I would suggest Web API if it is for RESTful services as WCF was never made to serve as Restful services although you can serve as one where as Web API was made particularly for this.

我建议使用Web API,如果它是用于RESTful服务的,因为WCF从未被用作Restful服务,尽管你可以作为Web API特别为此服务。

#1


9  

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.
  2. 如果要创建应支持特殊方案(如单向消息传递,消息队列,双工通信等)的服务,请选择WCF。

  3. 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.
  4. 如果要创建可在可用时使用快速传输通道的服务(例如TCP,命名管道,甚至是UDP(在WCF 4.5中)),请选择WCF,并且还希望在所有其他传输通道不可用时支持HTTP。

  5. 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).
  6. 如果要通过HTTP创建可以使用HTTP的完整功能(如URI,请求/响应头,缓存,版本控制,各种内容格式)的面向资源的服务,请选择Web API。

  7. Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.
  8. 如果要将服务公开给广泛的客户,包括浏览器,手机,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. Where as With WCF REST, service creation is bit difficult (requires clear understanding of configurations).
  • 它非常简单,使用Web API创建服务。与使用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...

我希望这能让你明白选择什么......

#2


1  

First things first, RESTful is a stateless and uniform interface norm that can be applied to web services. It doesn't have to be automatically and only plain old CRUD service backed by a DB.

首先,RESTful是一种无状态和统一的接口规范,可以应用于Web服务。它不必是自动的,只有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. And yes you can use websockets with ASP.NET WebApi there's plenty of tutorial out there, even for the newer ASP.NET Core.

对于基于消息的API,特别是如果它是双向的和基于事件的,您可以使用websockets并考虑使用REST API来公开统一的无状态Web界面来创建它们。是的,你可以使用带有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. 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.

我很难推荐使用SOAP的WCF,因为考虑到现在的Web标准,它几乎不可移植。例如,如果想要使用浏览器客户端而不是其他ASP.NET服务,那么您将不得不做额外的代码客户端来处理支持。

You can use WCF websockets, providing the almost all the advantages of WCF SOAP.

您可以使用WCF websockets,提供WCF SOAP的几乎所有优点。

tl;dr :

  • 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没有什么不同

#3


0  

WebAPI is not just good for RESTful webservices. 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不仅适用于RESTful Web服务。您可以轻松地将请求发送到WebAPI控制器并按照您的需要进行处理:计算,发送消息,与CRM交互,与数据库或其他任何内容交互。

WCF was created to manage SOAP based webservices and brings extra complexity. It handles TCP, Mime...

创建WCF是为了管理基于SOAP的Web服务并带来额外的复杂性。它处理TCP,Mime ......

If you just need to handle HTTP requests, the simplest way is to go with WebAPI.

如果您只需要处理HTTP请求,最简单的方法是使用WebAPI。

#4


0  

I would suggest Web API if it is for RESTful services as WCF was never made to serve as Restful services although you can serve as one where as Web API was made particularly for this.

我建议使用Web API,如果它是用于RESTful服务的,因为WCF从未被用作Restful服务,尽管你可以作为Web API特别为此服务。