Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Web Services with Windows Azure (new)

You're reading from  Building Web Services with Windows Azure (new)

Product type Book
Published in May 2015
Publisher
ISBN-13 9781784398378
Pages 322 pages
Edition 1st Edition
Languages

Table of Contents (17) Chapters

Building Web Services with Microsoft Azure
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Introduction
1. Getting Started with the ASP.NET Web API 2. Extending the ASP.NET Web API 3. API Management 4. Developing a Web API for Mobile Apps 5. Connecting Applications with Microsoft Azure Service Bus 6. Creating Hybrid Services 7. Data Services in the Cloud – an Overview of ADO.NET and Entity Framework 8. Data Services in the Cloud – Microsoft Azure Storage 9. Data Services in the Cloud – NoSQL in Microsoft Azure Index

Design principles behind the ASP.NET Web API


Now that we understand the intent behind ASP.NET Web API, let's discuss some design principles on which ASP.NET Web API is based:

  • Distributed by design: ASP.NET Web API considers HTTP as a first-class citizen and has inherent support for REST. It enables a framework for creating distributed services that leverage HTTP features such as stateless, caching, and compression.

  • Russian Doll Model: This is also called the Matryoshka doll model, which refers to a set of wooden dolls of decreasing sizes placed one inside the other. Architecturally, it denotes a recognizable relationship of nested objects (object within an object). The ASP.NET Web API messaging handlers leverage this principle to define the request-response pipeline. Each handler in the pipeline is responsible for processing the incoming request and then delegating the request to another handler. When a request reaches a handler, it may opt to process it, validate it, and then delegate the request to another handler or break the chain by sending a response. Of course, this is not true for the last handler in the chain since it will not delegate but rather work to get the response back up the chain. The response will follow the same logic but in the reverse direction and each handler will get the response before sending it back to the caller. We will discuss message handlers in detail in the next section. The follow figure describes the Russian Doll model being employed by ASP.NET Web API framework.

  • Asynchronous to the core: The ASP.NET Web API leverages the .NET Task-based Asynchronous Pattern (TAP) model to the core. TAP is based on the Task and Task<TResult> types in the System.Threading.Tasks namespace, which are used to represent arbitrary asynchronous operations. TAP provides a new pattern to work with both CPU-intensive and non-CPU-intensive asynchronous invocations. It simplifies the overall programming model for asynchronous operations in .NET 4.5 through the async and await model. Let's look at an example:

    async Task<int> GetContentLengthAsync(string uri)
    {
        int contentLength;
        using (var client = new HttpClient())
        {
            var content = await client.GetStringAsync(uri);
            contentLength = content.Length;
        }
     
        return contentLength;
    }

In the preceding example:

  • We create an asynchronous method GetContentLengthAsync that accepts a web URI and returns the length of the response.

  • The return type for this method is Task<TResult>, the return type can also be Task or void.

  • An async method typically includes at least one await call, which will mark a path where the code cannot continue unless an asynchronous operation is completed. It does not block the thread though. Instead, the method is suspended and the control returns to the caller.

  • The method execution continues after the response is returned from the asynchronous call.

    Note

    A TAP-based pattern simplifies writing asynchronous programming code and improves overall responsiveness of the system.

  • ASP.NET incorporates dependency injection at various levels in the core types of the system. The ASP.NET Web API exposes a set of interfaces such as IDependencyResolver (http://www.asp.net/web-api/overview/advanced/dependency-injection), which enables developers to modify almost everything in the message pipeline by injecting their custom instances. Another good side effect of Dependency Injection in ASP.NET Web API is that we can easily create unit tests for services built on the framework thus improving testability.

  • ASP.NET Web API is intended to be an open framework, and it leverages the ASP.NET infrastructure for enabling multiple hosting options. We can host a Web API from a simple console application to a powerful server such as IIS. We will cover the different options in Chapter 2, Extending the ASP.NET Web API.

You have been reading a chapter from
Building Web Services with Windows Azure (new)
Published in: May 2015 Publisher: ISBN-13: 9781784398378
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}