[ACCEPTED]-WCF vs ASP.NET Web API-asp.net-web-api
For us, WCF is used for SOAP and Web API 3 for REST. I wish Web API supported SOAP 2 too. We are not using advanced features 1 of WCF. Here is comparison from MSDN:
WCF was 4 originally created to enable SOAP-based 3 services. For simpler RESTful or RPCish 2 services (think clients like jQuery) ASP.NET 1 Web API should be good choice.
ASP.net Web API is all about HTTP and REST 12 based GET,POST,PUT,DELETE with well know 11 ASP.net MVC style of programming and JSON 10 returnable; web API is for all the light 9 weight process and pure HTTP based components. For 8 one to go ahead with WCF even for simple 7 or simplest single web service it will bring 6 all the extra baggage. For light weight 5 simple service for ajax or dynamic calls 4 always WebApi just solves the need. This 3 neatly complements or helps in parallel 2 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 1 Hanselman for more information.
In the scenarios listed below you should 10 go for WCF:
- If you need to send data on protocols like TCP, MSMQ or MIME
- If the consuming client just knows how to consume SOAP messages
WEB API is a framework for 9 developing RESTful/HTTP services.
There are 8 so many clients that do not understand SOAP 7 like Browsers, HTML5, in those cases WEB 6 APIs are a good choice.
HTTP services header 5 specifies how to secure service, how to 4 cache the information, type of the message 3 body and HTTP body can specify any type 2 of content like HTML not just XML as SOAP 1 services.
Since using both till now, I have found 7 many differences between WCF and Web API. Both 6 technology stacks are suited well to different 5 scenarios, so it is not possible to say 4 which is better, this depends on configuration 3 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 2 my view, it is also collected from other 1 official websites.
WCF will give you so much of out the box, it's 6 not even comparable to anything. Unless 5 you want to do on your own implementation 4 of (to name a few) authentication, authorization, encryption, queuing, throttling, reliable 3 messaging, logging, sessions and so on. WCF 2 is not [only] web services; WCF is a development 1 platform for SOA.
Why I'm answering:
I took huge amount of time to understand 100 the difference between these two technologies. I'll 99 put all those points here that I think "If 98 I had these points at the time when I was 97 wondering around in search of this answer, then 96 I have decided very earlier in selecting 95 my required technology."
Source of Information:
Microsoft® Visual 94 Studio® 2015 Unleashed
ISBN-13: 978-0-672-33736-9 93 ISBN-10: 0-672-33736-3
Why ASP.NET Web API and WCF:
Before comparing the 92 technologies of ASP.NET Web API and WCF, it 91 is important to understand there are actually 90 two styles/standards for creating web services: REST 89 (Representational State Transfer) and SOAP/WSDL. The 88 SOAP/WSDL was the original standard on which 87 web services were built. However, it was 86 difficult to use and had bulky message formats 85 (like XML) that degraded performance. REST-based 84 services quickly became the alternative. They 83 are easier to write because they leverage 82 the basic constructs of HTTP (GET, POST, PUT, DELETE) and 81 typically use smaller message formats (like 80 JSON). As a result, REST-based HTTP services 79 are now the standard for writing services 78 that strictly target the Web.
Let's define purpose of ASP.NET Web API
ASP.NET Web 77 API is Microsoft’s technology for developing 76 REST-based HTTP web services. (It long ago 75 replaced Microsoft’s ASMX, which was based 74 on SOAP/WSDL.) The Web API makes it easy 73 to write robust services based on HTTP protocols 72 that all browsers and native devices understand. This 71 enables you to create services to support 70 your application and call them from other 69 web applications, tablets, mobile phones, PCs, and 68 gaming consoles. The majority of applications 67 written today to leverage the ever present 66 Web connection use HTTP services in some 65 way.
Let's now define purpose of WCF:
Communicating across the Internet is 64 not always the most efficient means. For 63 example, if both the client and the service 62 exist on the same technology (or even the 61 same machine), they can often negotiate 60 a more efficient means to communicate (such 59 as TCP/IP). Service developers found themselves 58 making the same choices they were trying 57 to avoid. They now would have to choose 56 between creating efficient internal services 55 and being able to have the broad access 54 found over the Internet. And, if they had 53 to support both, they might have to create 52 multiple versions of their service or at 51 least separate proxies for accessing their 50 service. This is the problem Microsoft solved with WCF.
With WCF, you can create your 49 service without concern for boundaries. You 48 can then let WCF worry about running your 47 service in the most efficient way, depending 46 on the calling client. To manage this task, WCF 45 uses the concept of endpoints. Your service 44 might have multiple endpoints (configured 43 at design time or after deployment). Each 42 endpoint indicates how the service might 41 support a calling client: over the Web, via 40 remoting, through Microsoft Message Queuing 39 (MSMQ), and more. WCF enables you to focus 38 on creating your service functionality. It 37 worries about how to most efficiently speak 36 with calling clients. In this way, a single 35 WCF service can efficiently support many 34 different client types.
Example of WCF:
Consider the example:
The 33 customer data is shared among the applications. Each 32 application might be written on a different 31 platform, and it might exist in a different 30 location. You can extract the customer interface 29 into a WCF service that provides common 28 access to shared customer data. This centralizes 27 the data, reduces duplication, eliminates 26 synchronization, and simplifies management. In 25 addition, by using WCF, you can configure 24 the service endpoints to work in the way 23 that makes sense to the calling client. Figure 22 shows the example from before with centralized 21 access of customer data in a WCF service.
i) When to choose Web API:
There 20 is no denying that REST-based HTTP services 19 like those created using ASP.NET Web API 18 have become the standard for building web 17 services. These services offer an easy, straightforward 16 approach for web developers building services. Web 15 developers understand HTTP GET and POST 14 and thus adapt well to these types of services. Therefore, if 13 you are writing services strictly targeted to HTTP, ASP.NET 12 Web API is the logical choice.
ii) When to choose WCF:
The WCF technology 11 is useful when you need to support multiple 10 service endpoints based on different protocols 9 and message formats. Products like Microsoft 8 BizTalk leverage WCF for creating robust 7 services that can be used over the Web as 6 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.
Web 5 developers often view WCF as more difficult 4 and complex to develop against. Therefore, if 3 you do not foresee the need for multiprotocol 2 services, you would likely stick with ASP.NET 1 Web API.
There is a comparison on MSDN about this
For 4 me, the choice was about Who the clients 3 are, and where are they located?
Within the company Network and .NET based clients : Use WCF 2 with TCP binding (Fast communication than 1 HTTP)
Outside the company Network, and use diverse technologies like PHP, Python etc: Use Web API with REST
Business speaking, WebApi lacks of a WSDL, so 7 the developers should document all manually. And 6 if, for example, the WebApi operation returns 5 a list of objects then, the client should 4 creates the objects manually, i.e. WebAPI 3 is really prone to errors of definitions.
The 2 pro of Webapi is its more lightweight than 1 WCF.
Regarding the statement "WebApi lacks of 10 WSDL" there are several ways to generate 9 Rest client. One popular approach is Swagger 8 UI / (Swashbukkle Nuget). This gives a 7 rich interface to understand the REST end 6 point's input and output schema and online 5 tool to test the end points.
JSON LD (Json 4 Linked Documents) is another emerging standard 3 which will further improve the JSON based 2 REST developer experience by exposing the 1 JSON schema with better semantics.
With wcf we can configure and expose the 8 same service support for multiple endpoints 7 like tcp, http.if you want your service 6 to be only http based then it will be better 5 to go with web API. Web API has very less 4 configuration when compared to wcf and is 3 bit faster than wcf. Wcf also supports restful 2 services. If you have limitation of .Net 1 framework 3.5 then your option is wcf.
More Related questions