Skip to main content

Middleware in .NET Core with an Item API

 



Middleware in .NET Core is a crucial concept that plays a significant role in handling HTTP requests and responses within the application pipeline. It acts as a series of filters or components that process incoming requests and outgoing responses. In this blog, we'll dive into the concept of middleware, its role in a .NET Core application, and demonstrate how to create custom middleware using an Item API as an example.

What is Middleware?

Middleware is software that is assembled into an application pipeline to handle requests and responses. Each piece of middleware can perform operations before or after the next piece of middleware in the pipeline. This pipeline is set up in the Startup.cs file of a .NET Core application.

Common Uses of Middleware

Middleware can be used for a variety of tasks, such as:

  • Request Logging: Logging details about incoming requests.
  • Authentication: Verifying user credentials.
  • Error Handling: Catching and handling exceptions.
  • Routing: Determining the endpoint that should handle a request.
  • Response Compression: Compressing the response data.

Setting Up Middleware in .NET Core

In a .NET Core application, middleware is configured in the Startup.cs file. Let's start by understanding the basic setup.

1. Configuring Middleware in Startup.cs

The Configure method in the Startup.cs file is where middleware is set up. The order in which middleware is added is important because it determines the flow of request processing.

Here's a basic example:

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

In this example, UseRouting, UseAuthorization, and UseEndpoints are all middleware components. The pipeline processes requests in the order these components are added.

Creating Custom Middleware

Custom middleware can be created to handle specific requirements. Let's create a simple custom middleware that logs request details for our Item API.

2. Creating the Custom Middleware Class

First, let's create a class that will represent our custom middleware.

using Microsoft.AspNetCore.Http;
using System.Diagnostics;
using System.Threading.Tasks;

public class RequestLoggingMiddleware
{
    private readonly RequestDelegate _next;

    public RequestLoggingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        // Log the request path
        Debug.WriteLine($"Incoming Request: {context.Request.Method} {context.Request.Path}");

        // Call the next middleware in the pipeline
        await _next(context);

        // Log the response status code
        Debug.WriteLine($"Outgoing Response: {context.Response.StatusCode}");
    }
}

In this RequestLoggingMiddleware class:

  • The RequestDelegate is a delegate that represents the next middleware in the pipeline.
  • The InvokeAsync method is where we define our middleware logic. It logs the request method and path, then calls the next middleware in the pipeline, and finally logs the response status code.

3. Registering the Middleware

To use the custom middleware in our application, we need to register it in the Startup.cs file.

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        // Use custom request logging middleware
        app.UseMiddleware<RequestLoggingMiddleware>();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

Here, app.UseMiddleware<RequestLoggingMiddleware>(); registers our custom middleware in the pipeline. The order of registration is important because it determines when the middleware will execute in relation to other middleware components.

4. Testing the Middleware with Item API

Let's implement a basic ItemsController to see the middleware in action:

[Route("api/[controller]")]
[ApiController]
public class ItemsController : ControllerBase
{
    private static List<Item> Items = new List<Item>
    {
        new Item { Id = 1, Name = "Item1", Description = "First item" },
        new Item { Id = 2, Name = "Item2", Description = "Second item" },
    };

    [HttpGet]
    public ActionResult<IEnumerable<Item>> GetItems()
    {
        return Ok(Items);
    }

    [HttpGet("{id}")]
    public ActionResult<Item> GetItem(int id)
    {
        var item = Items.FirstOrDefault(i => i.Id == id);
        if (item == null)
        {
            return NotFound();
        }
        return Ok(item);
    }

    // Other CRUD actions...
}

When you run the application and make a request to the ItemsController, the custom middleware will log the request and response details to the debug output.

Conclusion

Middleware is a powerful feature in .NET Core that allows you to intercept and manipulate HTTP requests and responses as they pass through the application pipeline. By understanding and utilizing middleware, you can create more modular, maintainable, and testable applications.

In this blog, we created a custom request logging middleware and integrated it into an Item API. This example highlights how middleware can be used to add cross-cutting concerns, such as logging, to your application in a clean and reusable manner. Whether you're handling errors, managing authentication, or simply logging requests, middleware provides a flexible and powerful way to build robust applications in .NET Core.

Comments

Popular posts from this blog

Implementing and Integrating RabbitMQ in .NET Core Application: Shopping Cart and Order API

RabbitMQ is a robust message broker that enables communication between services in a decoupled, reliable manner. In this guide, we’ll implement RabbitMQ in a .NET Core application to connect two microservices: Shopping Cart API (Producer) and Order API (Consumer). 1. Prerequisites Install RabbitMQ locally or on a server. Default Management UI: http://localhost:15672 Default Credentials: guest/guest Install the RabbitMQ.Client package for .NET: dotnet add package RabbitMQ.Client 2. Architecture Overview Shopping Cart API (Producer): Sends a message when a user places an order. RabbitMQ : Acts as the broker to hold the message. Order API (Consumer): Receives the message and processes the order. 3. RabbitMQ Producer: Shopping Cart API Step 1: Install RabbitMQ.Client Ensure the RabbitMQ client library is installed: dotnet add package RabbitMQ.Client Step 2: Create the Producer Service Add a RabbitMQProducer class to send messages. RabbitMQProducer.cs : using RabbitMQ.Client; usin...

How Does My .NET Core Application Build Once and Run Everywhere?

One of the most powerful features of .NET Core is its cross-platform nature. Unlike the traditional .NET Framework, which was limited to Windows, .NET Core allows you to build your application once and run it on Windows , Linux , or macOS . This makes it an excellent choice for modern, scalable, and portable applications. In this blog, we’ll explore how .NET Core achieves this, the underlying architecture, and how you can leverage it to make your applications truly cross-platform. Key Features of .NET Core for Cross-Platform Development Platform Independence : .NET Core Runtime is available for multiple platforms (Windows, Linux, macOS). Applications can run seamlessly without platform-specific adjustments. Build Once, Run Anywhere : Compile your code once and deploy it on any OS with minimal effort. Self-Contained Deployment : .NET Core apps can include the runtime in the deployment package, making them independent of the host system's installed runtime. Standardized Libraries ...

Clean Architecture: What It Is and How It Differs from Microservices

In the tech world, buzzwords like   Clean Architecture   and   Microservices   often dominate discussions about building scalable, maintainable applications. But what exactly is Clean Architecture? How does it compare to Microservices? And most importantly, is it more efficient? Let’s break it all down, from understanding the core principles of Clean Architecture to comparing it with Microservices. By the end of this blog, you’ll know when to use each and why Clean Architecture might just be the silent hero your projects need. What is Clean Architecture? Clean Architecture  is a design paradigm introduced by Robert C. Martin (Uncle Bob) in his book  Clean Architecture: A Craftsman’s Guide to Software Structure and Design . It’s an evolution of layered architecture, focusing on organizing code in a way that makes it  flexible ,  testable , and  easy to maintain . Core Principles of Clean Architecture Dependency Inversion : High-level modules s...