Table of Contents

Class MiddlewareBuilderFactory

Namespace
Cuemon.AspNetCore.Builder
Assembly
Cuemon.AspNetCore.dll

Provides support for creating, using and configuring Middleware or ConfigurableMiddleware<TOptions> implementations.

public static class MiddlewareBuilderFactory
Inheritance
MiddlewareBuilderFactory

Examples

The following example demonstrates how to register a custom middleware in the application pipeline using MiddlewareBuilderFactory.

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

namespace MyApp.Examples;

public class TimingMiddleware : Middleware
{
    public TimingMiddleware(RequestDelegate next) : base(next) { }

    public override async Task InvokeAsync(HttpContext context)
    {
        var sw = Stopwatch.StartNew();
        await Next(context);
        sw.Stop();
        Console.WriteLine($"{context.Request.Path} took {sw.ElapsedMilliseconds} ms");
    }
}

public static class MiddlewareBuilderFactoryExample
{
    public static void Demonstrate()
    {
        var builder = WebApplication.CreateBuilder().Build();

        MiddlewareBuilderFactory.UseMiddleware<TimingMiddleware>(builder);
    }
}

Methods

UseConfigurableMiddleware<TMiddleware, TOptions>(IApplicationBuilder, Action<TOptions>)

Adds a configurable middleware type to the application request pipeline.

public static IApplicationBuilder UseConfigurableMiddleware<TMiddleware, TOptions>(IApplicationBuilder builder, Action<TOptions> setup = null) where TMiddleware : ConfigurableMiddlewareCore<TOptions> where TOptions : class, IParameterObject, new()

Parameters

builder IApplicationBuilder

The IApplicationBuilder instance.

setup Action<TOptions>

The Action<T> which need to be configured.

Returns

IApplicationBuilder

The IApplicationBuilder instance.

Type Parameters

TMiddleware

The type of the configurable middleware.

TOptions

The type of the delegate setup.

UseMiddleware<TMiddleware>(IApplicationBuilder)

Adds a middleware type to the application request pipeline.

public static IApplicationBuilder UseMiddleware<TMiddleware>(IApplicationBuilder builder) where TMiddleware : MiddlewareCore

Parameters

builder IApplicationBuilder

The IApplicationBuilder instance.

Returns

IApplicationBuilder

The IApplicationBuilder instance.

Type Parameters

TMiddleware

The type of the middleware.