Page 60 - MSDN Magazine, March 2018
P. 60

Cutting EdgE DINO ESPOSITO REST and Web API in ASP.NET Core
I’ve never been a fan of ASP.NET Web API as a standalone frame- work and I can’t hardly think of a project where I used it. Not that the framework in itself is out of place or unnecessary. I just find that the business value it actually delivers is, most of the time, minimal. On the other hand, I recognize in it some clear signs of the underlying effort Microsoft is making to renew the ASP.NET runtime pipeline. Overall, I like to think of ASP.NET Web API as a proof of concept for what today has become ASP.NET Core and, specifically, the new runtime environment of ASP.NET Core.
Web API was primarily introduced as a way to make building a RESTful API easy and comfortable in ASP.NET. This article is about how to achieve the same result—building a RESTful API— in ASP.NET Core.
The Extra Costs of Web API in Classic ASP.NET
ASP.NET Web API was built around the principles sustaining the Open Web Interface for .NET (OWIN) specification, which is meant to decouple the Web server from hosted Web applications. In the .NET space, the introduction of OWIN marked a turning point, where the tight integration of IIS and ASP.NET was ques- tioned.ThattightcouplingwasfullyabandonedinASP.NETCore.
Any Web façade built using the ASP.NET Web API framework relies on a completely rewritten pipeline that uses the standard OWIN interface to dialog with the underlying host Web server. Yet, an ASP.NET Web API is not a standalone application. To be available for callers it needs a host environment that takes care of listening to some configured port, captures incoming requests and dispatches them down the Web API pipeline.
A Web API application can be hosted in a Windows service or in a custom console application that implements the appropriate OWINinterfaces.ItcanalsobehostedbyaclassicASP.NETappli- cation, whether targeting Web Forms or ASP.NET MVC. Over the
past few years, hosting Web API within a classic ASP.NET MVC application proved to be a very common scenario, yet one of the least effective in terms of raw performance and memory footprint.
As Figure 1 shows, whenever you arrange a Web API façade within an ASP.NET MVC application, three frameworks end up living side-by-side, processing every single Web API request. The hostASP.NETMVCapplicationisencapsulatedinanHTTPhan- dler living on top of system.web—the original ASP.NET runtime environment. On top of that—taking up additional memory—you have the OWIN-based pipeline of Web API.
Web API is the most high-profile example of the OWIN principles in action.
The vision of introducing a server-independent Web framework is, in this case, significantly weakened by the constraints of staying compatible with the existing ASP.NET pipeline. Therefore, the clean and REST-friendly design of Web API doesn’t unleash its full potential because of the legacy system.web assembly. From a pure performance perspective, only some edge use cases really justify the use of Web API.
Effective Use Cases for Web API
Web API is the most high-profile example of the OWIN principles in action. A Web API library runs behind a server application that captures and forwards incoming requests. This host can be a clas- sic Web application on the Microsoft stack (Web Forms, ASP.NET MVC)oritcanbeaconsoleapplicationoraWindowsservice.
In any case, it has to be an application endowed with a thin layer of code capable of dialoging with the Web API listener.
Hosting a Web API outside of the Web environment removes at the root any dependency on the system.web assembly, thus mag- ically making the request pipeline as lean and mean as desired.
This is the crucial point that led the ASP.NET Core team to build the ASP.NET Core pipeline. The ideal hosting conditions for Web API have been reworked to be the ideal hosting conditions for just about any ASP.NET Core application. This enabled a completely new pipeline devoid of dependencies on the system.web assem- bly and hostable behind an embedded HTTP server exposing a contracted interface—the IServer interface.
Web Forms
MVC Web API
OWIN
system.web
MVC model
Figure 1 Frameworks Involved in a Classic ASP.NET Web API Application
54 msdn magazine


































































































   58   59   60   61   62