简体   繁体   中英

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. 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.

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

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.

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.
  • Secondly, if you are creating internet service which is going to use external resource , then go with 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.

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 :

  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. 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.
  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).
  4. Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.

Why to choose Web API

  • Web API doesn't have tedious and extensive configuration like WCF REST service.
  • 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 is only based on HTTP and HTTPS and easy to define, expose and consume in a REST-full way.
  • Web API is light weight architecture and good for devices which have limited bandwidth like smart phones.

My Opinion:

  • Simplest way to do so - Web API (Since you dont have any examples for this)
  • Hardest Way is (Configurations) - WCF (Better go with WCF, since you have examples)

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. It doesn't have to be automatically and only plain old CRUD service backed by a DB.

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.

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.

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.

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.

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

tl;dr :

  • You can mix RESTful and Websockets, it can actually be better than going full REST or full 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
  • Message API between services is no different than a message API between a service and a browser

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM