Book Image

Building Web Services with Windows Azure (new)

Book Image

Building Web Services with Windows Azure (new)

Overview of this book

Table of Contents (17 chapters)
Building Web Services with Microsoft Azure
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Introduction
5
Connecting Applications with Microsoft Azure Service Bus
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.