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

Optional Parameters in C# — Writing Flexible and Clean Methods

Hello, .NET developers! 👋 How often have you created multiple method overloads just to handle slightly different cases? Maybe one method accepts two parameters, another three, and one more adds a flag for debugging? That’s a lot of code duplication for something that can be solved beautifully with optional parameters . Optional parameters in C# let you define default values for method arguments. When a caller doesn’t pass a value, the compiler automatically substitutes the default. This feature helps keep your APIs simple, readable, and maintainable. 🎥 Explore more on YouTube : DotNet Full Stack Dev Understanding Optional Parameters Optional parameters are defined by assigning default values in the method signature. When calling the method, you can omit those parameters if you’re okay with the defaults. Example public class Logger { public void Log(string message, string level = "INFO", bool writeToFile = false) ...

.NET 10: Your Ultimate Guide to the Coolest New Features (with Real-World Goodies!)

 Hey .NET warriors! 🤓 Are you ready to explore the latest and greatest features that .NET 10 and C# 14 bring to the table? Whether you're a seasoned developer or just starting out, this guide will show you how .NET 10 makes your apps faster, safer, and more productive — with real-world examples to boot! So grab your coffee ☕️ and let’s dive into the awesome . 💪 1️⃣ JIT Compiler Superpowers — Lightning-Fast Apps .NET 10 is all about speed . The Just-In-Time (JIT) compiler has been turbocharged with: Stack Allocation for Small Arrays 🗂️ Think fewer heap allocations, less garbage collection, and blazing-fast performance . Better Code Layout 🔥 Hot code paths are now smarter, meaning faster method calls and fewer CPU cache misses. 💡 Why you care: Your APIs, desktop apps, and services now respond quicker — giving users a snappy experience . 2️⃣ Say Hello to C# 14 — More Power in Your Syntax .NET 10 ships with C# 14 , and it’s packed with developer goodies: Field-Bac...

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...