Short Circuiting and Branching in .NET Core 1.1

Introduction

Short circuiting and branching in .NET Core are key concepts to make an application sequential and provide more control over the response.

The term middleware is used in a .NET Core application either to delegate a request to the next component in the pipeline or short circuit it.

Middleware has some important aspects as shown below:

It delegates the request to the next component in the pipeline.

Business logic can be written before and after the component is invoked in the pipeline.

There are three extension methods Run, Map, and Use which can be configured as request delegates.

Each request delegated can be written as a single line as an anonymous method (aka in-line middleware) or separately as a middleware component.

These components are responsible for invoking the next delegate in the pipeline or to short circuit it.

.NET Core request pipeline comprises request delegates, which call one after another as the image shown below:

Reference: MSDN

Each delegate can also decide not to pass a request to the next delegate, which is known as short circuiting. An advantage of short circuiting is, it doesn’t pass the request to the following delegate if it doesn’t meet specific criteria. For example, if a request doesn’t have a specific parameter in its URL, then it can short circuit the pipeline and returns the response directly from there.

On the other hand, branching helps to map the request based on matches and executes the specific branch (can be a method).

Map and MapWhen extensions are provided by .NET Core by default for branching the pipeline.

Kindly refer to the image below for better understanding:

Let’s practically see all this.

Prerequisites

Visual Studio 2017 Enterprise/Professional

.Net Core 1.1

Implementation

To understand the short circuiting and branching in .NET Core 1.1, refer to the figure below for a solution structure:

Open a startup.cs file and search for configure method; it comes out-of-box with .NET Core applications.

Configure:

This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Figure3.

Short-Circuiting:

To meetpurpose I’ve written code using Use extension method and set a limitation so that if a specific request contains a query string in its URL, then it should not go further in pipeline otherwise it should go till the Run method.

Output:

Hope this helps you to understand Short Circuiting and Branching in .NET Core.

Happy Coding!

Sachin Kalia

Sachin Kalia is a Technical Lead at 3Pillar Global, working out of our office in Noida, India. He is Microsoft MVP which is the most prominent recognition from Microsoft for helping the community for free in learning technologies. He has expertise in building and working with high-performance teams delivering enterprise product. Sachin is a passionate blogger and speaker and loves to dive deep into trending technologies. His interest areas are .Net, Asp.Net, WebAPi, Microservices, Cognitive Services, Artificial Intelligence and Azure(Cloud). He loves swimming and playing cricket and table tennis and love traveling with friends and family.