WCF vs ASP.NET Web API [closed]
Asked Answered
M

11

504

I've spent a few months trying to grasp the concepts behind WCF and recently I've developed my first WCF service application.

I've struggled quite a bit to understand all the settings in the config file.
I am not convinced about the environment but it seems that you can do amazing stuff with it.

The other day I've found out that Microsoft has come out with a new thing called ASP.NET Web API.

For what I can read it's a RESTful framework, very easy to use and implement.

Now, I am trying to figure out what are the main differences between the 2 frameworks and if I should try and convert my old WCF service application with the new API.

Could someone, please, help me to understand the differences and usage of each?

Meridethmeridian answered 19/2, 2012 at 11:8 Comment(5)
+1 interesting question. maybe you'll get good responses at programmers.stackexchange.comSpoken
Which features of the "old" WCF are you using? Are you trying to build a RESTful API? Or RPC, or SOAP?Enow
@marcind: thanks for your answer. It's mostly RESTful calls. No RPC at all.Meridethmeridian
Another good answer can be found at https://mcmap.net/q/75243/-wcf-vs-asp-net-web-apiGlissade
both are one and the same thing but the old difference that one could come across would be the wcf is basically for intranet and Webapi for internet , yes definitely we can make wcf restful too! basically both has run upon the http protocol web.httpPandora
E
192

The new ASP.NET Web API is a continuation of the previous WCF Web API project (although some of the concepts have changed).

WCF was originally created to enable SOAP-based services. For simpler RESTful or RPCish services (think clients like jQuery) ASP.NET Web API should be good choice.

Enow answered 19/2, 2012 at 19:48 Comment(5)
Also: Although WCF provides some support for writing REST-style services, the support for REST in ASP.NET Web API is more complete and all future REST feature improvements will be made in ASP.NET Web API msdn.microsoft.com/en-us/library/jj823172.aspxGreenroom
Actually WCF was originally created to implement an abstraction layer between a SOAP or RPC service and the client. The point was to create a single architecture (ABC) around both these very different calls and handle the plumbing via configuration files.Penelopepeneplain
Real drawback with ASP.NET Web API is it's client tooling. Visual Studio supports integrated tools to support seamless WCF service client entities and service generation. No support in Web API. I know there is the HttpClient which is awesome, but it doesn't take care of entity generation and serialization/deserialization.Dilly
@Shimmy What about service generation using swagger?Guessrope
@Guessrope thanks for your response. Can the generated entities emit INotifyPropertyChanged client entities? How about validation?Dilly
H
253

For us, WCF is used for SOAP and Web API for REST. I wish Web API supported SOAP too. We are not using advanced features of WCF. Here is comparison from MSDN:

enter image description here

Heteroplasty answered 2/5, 2013 at 18:20 Comment(4)
And Web API supports OData which for CSOM is a Godsend.Gausman
Its amazing how MS with so much says nothing really worthy. For example, WCF supports JSON but this information is well hidden in this "comparison", while it says textually that WebApi supports JSON not once but twice.Botch
this table is meaningless. "JQuery" (scare quotes for the capital J) is a protocol and/or format?Lade
Interesting. MSDN is wrong in mentioning HTTP as a transport protocol. HTTP is an application layer protocol.Noncompliance
E
192

The new ASP.NET Web API is a continuation of the previous WCF Web API project (although some of the concepts have changed).

WCF was originally created to enable SOAP-based services. For simpler RESTful or RPCish services (think clients like jQuery) ASP.NET Web API should be good choice.

Enow answered 19/2, 2012 at 19:48 Comment(5)
Also: Although WCF provides some support for writing REST-style services, the support for REST in ASP.NET Web API is more complete and all future REST feature improvements will be made in ASP.NET Web API msdn.microsoft.com/en-us/library/jj823172.aspxGreenroom
Actually WCF was originally created to implement an abstraction layer between a SOAP or RPC service and the client. The point was to create a single architecture (ABC) around both these very different calls and handle the plumbing via configuration files.Penelopepeneplain
Real drawback with ASP.NET Web API is it's client tooling. Visual Studio supports integrated tools to support seamless WCF service client entities and service generation. No support in Web API. I know there is the HttpClient which is awesome, but it doesn't take care of entity generation and serialization/deserialization.Dilly
@Shimmy What about service generation using swagger?Guessrope
@Guessrope thanks for your response. Can the generated entities emit INotifyPropertyChanged client entities? How about validation?Dilly
C
80

ASP.net Web API is all about HTTP and REST based GET,POST,PUT,DELETE with well know ASP.net MVC style of programming and JSON returnable; web API is for all the light weight process and pure HTTP based components. For one to go ahead with WCF even for simple or simplest single web service it will bring all the extra baggage. For light weight simple service for ajax or dynamic calls always WebApi just solves the need. This neatly complements or helps in parallel to the ASP.net MVC.

Check out the podcast : Hanselminutes Podcast 264 - This is not your father's WCF - All about the WebAPI with Glenn Block by Scott Hanselman for more information.

Cthrine answered 3/4, 2012 at 20:42 Comment(0)
M
70

In the scenarios listed below you should go for WCF:

  1. If you need to send data on protocols like TCP, MSMQ or MIME
  2. If the consuming client just knows how to consume SOAP messages

WEB API is a framework for developing RESTful/HTTP services.

There are so many clients that do not understand SOAP like Browsers, HTML5, in those cases WEB APIs are a good choice.

HTTP services header specifies how to secure service, how to cache the information, type of the message body and HTTP body can specify any type of content like HTML not just XML as SOAP services.

Misstep answered 6/1, 2013 at 9:1 Comment(2)
This makes the assumption that WCF only handles SOAP messages, an incorrect assumption. You can expose REST endpoints on WCF services as well. I would modify it to say, if you are not going to use the features of WCF (see tridy's message) then Web API makes sense.Brominate
Yep WCF does Rest as well.. basically Web api is a subset of WCFs functionality which is appropriate if your doing simple CRUD style data apps.Considerate
R
45

Since using both till now, I have found many differences between WCF and Web API. Both technology stacks are suited well to different scenarios, so it is not possible to say which is better, this depends on configuration and scenario.

Properties              ASP.Net Web API                         WCF
--------------------------------------------------------------------------------------------------
End point (mainly)      Http based                              SOAP based
Service Type            Front End                               Back-end
Support                 caching, compression, versioning        No
Framework               ASP.net                                 WCF
Orientation             Resource Oriented                       Service Oriented
Transports              http                                    http, tcp, MSMQ, Named pipe
Message pattern         Request reply                           request Reply, one way, duplex
Configuration overhead  Less                                    Much
Security                lesser than WCF (web standard security) Very high (WS-I standard)
Hosting                 IIS                                     IIS, Windows Service, Self hosting
Performance             Fast                                    A bit slower than Web API
In use from             .NET 4.0                                .NET 3.5

Note: The data is not only my view, it is also collected from other official websites.

Roadster answered 22/12, 2016 at 15:37 Comment(1)
Web Service API can also be self hosted (Owin / Katana) as well as in a Windows serviceCarvelbuilt
V
35

WCF will give you so much of out the box, it's not even comparable to anything. Unless you want to do on your own implementation of (to name a few) authentication, authorization, encryption, queuing, throttling, reliable messaging, logging, sessions and so on. WCF is not [only] web services; WCF is a development platform for SOA.

Valorievalorization answered 2/7, 2014 at 10:42 Comment(5)
If I'm not mistaken, I think that WEB API also provides most of the functions you listed.Sumatra
No Web api does not provide these things or provides very simple versions.Considerate
Well what is it - does it provide them or not?Diazo
For authentication and authorization, check asp.net/web-api/overview/security/…. tl;dr: It supports it definitely in IIS. For encryption, you'll probably need to use SSL, ASP.NET naturally handles queuing (but that's straight up based on worker threads available vs incoming requests). Sessions exist (but I'd never recommend using Sessions directly). Logging is easy enough to set up (through ActionFilters or the such). An alternative to reliable messaging is using SignalR (although not exactly).Arin
"Not comparable to anything"?? Unlikely.Grams
H
21

Why I'm answering:

I took huge amount of time to understand the difference between these two technologies. I'll put all those points here that I think "If I had these points at the time when I was wondering around in search of this answer, then I have decided very earlier in selecting my required technology."

Source of Information:

Microsoft® Visual Studio® 2015 Unleashed

ISBN-13: 978-0-672-33736-9 ISBN-10: 0-672-33736-3

Why ASP.NET Web API and 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.

Let's define purpose of 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.

Let's now define purpose of 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.

Example of WCF:

Consider the example:

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.

This is how WCF serves different clients

Conclusion:

i) When to choose Web API:

There is no denying that REST-based HTTP services like those created using ASP.NET Web API have become the standard for building web services. These services offer an easy, straightforward approach for web developers building services. Web developers understand HTTP GET and POST and thus adapt well to these types of services. Therefore, if you are writing services strictly targeted to HTTP, ASP.NET Web API is the logical choice.

ii) When to choose WCF:

The WCF technology is useful when you need to support multiple service endpoints based on different protocols and message formats. Products like Microsoft BizTalk leverage WCF for creating robust services that can be used over the Web as well via different machine-to-machine configurations.If, however, you do need to write an application that communicates over TCP/IP when connected to the local network and works over HTTP when outside the network, WCF is your answer.

Be Warned:

Web developers often view WCF as more difficult and complex to develop against. Therefore, if you do not foresee the need for multiprotocol services, you would likely stick with ASP.NET Web API.

Hitherward answered 20/3, 2017 at 22:37 Comment(1)
Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question and flag for undeletion.Mccomb
H
12

There is a comparison on MSDN about this

WCF and ASP.NET Web API

For me, the choice was about Who the clients are, and where are they located?

Within the company Network and .NET based clients : Use WCF with TCP binding (Fast communication than HTTP)

Outside the company Network, and use diverse technologies like PHP, Python etc: Use Web API with REST

Hellenic answered 8/2, 2017 at 1:31 Comment(0)
B
10

Business speaking, WebApi lacks of a WSDL, so the developers should document all manually. And if, for example, the WebApi operation returns a list of objects then, the client should creates the objects manually, i.e. WebAPI is really prone to errors of definitions.

The pro of Webapi is its more lightweight than WCF.

Botch answered 4/3, 2016 at 12:33 Comment(1)
WCF == WS-*, webapi == RESTMckeehan
C
7

Regarding the statement "WebApi lacks of WSDL" there are several ways to generate Rest client. One popular approach is Swagger UI / (Swashbukkle Nuget). This gives a rich interface to understand the REST end point's input and output schema and online tool to test the end points.

JSON LD (Json Linked Documents) is another emerging standard which will further improve the JSON based REST developer experience by exposing the JSON schema with better semantics.

Crossrefer answered 13/9, 2016 at 6:34 Comment(0)
G
1

With wcf we can configure and expose the same service support for multiple endpoints like tcp, http.if you want your service to be only http based then it will be better to go with web API. Web API has very less configuration when compared to wcf and is bit faster than wcf. Wcf also supports restful services. If you have limitation of .Net framework 3.5 then your option is wcf.

Glycoside answered 18/6, 2017 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.