Friday, June 23, 2023

Understanding the Use, Run, and Map Functions for Middleware in .NET Core

use,run and map in .net core

Introduction:

Middleware plays a vital role in handling and processing HTTP requests within a .NET Core application's request pipeline. It enables developers to customize and extend the application's behavior. In this post, we will delve into three crucial functions used for configuring middleware: Use, Run, and Map.



  1. Use:

    The Use function is extensively used when configuring middleware in .NET Core. It allows the addition of middleware components to the request pipeline. This function accepts a delegate or a middleware class as a parameter. The delegate or middleware class is responsible for processing an HTTP request and generating an appropriate response.

    Consider the following example that demonstrates the Use function in adding custom middleware:

    public void Configure(IApplicationBuilder app)
    {
        app.Use(async (context, next) =>
        {
            // Perform some logic before the request reaches the next middleware
            await next.Invoke();
            // Perform some logic after the request has been processed by subsequent middleware
        });
        // Add more middleware components using the Use function if necessary
    }
    

Wednesday, June 21, 2023

Middleware in .NET Core: A Developer's Guide

middleware .net core

Introduction:

Middleware plays a vital role in web development, and having a clear understanding of its concept and implementation is crucial for .NET Core developers. Middleware acts as a bridge between incoming requests and outgoing responses in an application, enabling developers to customize and extend the request-processing pipeline. In this article, we will explore the world of middleware in .NET Core, discussing its significance, usage, and providing practical examples.


Understanding Middleware:

In the context of .NET Core, middleware refers to a software component or a set of components that are executed sequentially to process HTTP requests and responses. It forms a chain of components that intercept requests, perform specific actions, and pass control to the next component in the pipeline. Middleware empowers developers to add, remove, or modify behavior at various stages of request processing without altering the core application code.

Middleware in .NET Core:

In .NET Core, the request pipeline is constructed using middleware components. It comprises a series of middleware components that receive an incoming HTTP request and pass it along until a response is generated. Each middleware component can inspect, modify, or terminate the request pipeline.

Middleware components in .NET Core are represented by classes that implement either the IMiddleware interface or the RequestDelegate delegate. The IMiddleware interface provides a convenient way to encapsulate middleware logic, while the RequestDelegate delegate offers finer control over middleware behavior.

Saturday, May 6, 2023

Default values for lambda expressions - C#12

In C#12, you can now define default values for parameters on lambda expressions. The syntax and rules are the same as adding default values for arguments to any method or local function.For example:

var addWithDefault = (int addTo = 2) => addTo + 1;
addWithDefault(); // 3
addWithDefault(5); // 6

Prior to C# 12 you needed to use a local function or the unwieldy DefaultParameterValue from the System.Runtime.InteropServices namespace to provide a default value for lambda expression parameters.

Using directives for additional types - C#12

C# 12 extends using directive support to any type.Now you can use the using alias directive to alias any type, not just named types. That means you can create semantic aliases for tuple types, array types, pointer types, or other unsafe types. Below are few examples :

using Measurement = (string, int);
using PathOfPoints = int[];
using DatabaseInt = int?;

You can now alias almost any type. You can alias nullable value types, although you cannot alias nullable reference types.

Friday, May 5, 2023

Primary Constructor - C# 12

Primary constructor allows you to add parameters to calss declaration itself and use these values in class body. Promary constructor was introduced for records in C#9. C#12 extends it to all classes and structs.

c sharp primary constructor
^ Scroll to Top