Jez
Jez

Reputation: 30073

How can I get a list of registered middleware in ASP.NET Core?

In ASP.NET Core, you can register new middleware into the request processing pipeline during the Configure method of the startup class you're using for your web host builder by using app.UseMiddleware(...). During debugging, how do I get a list of registered middleware providers, though? I can't see any way to actually view the middleware that's been registered for the app.

Upvotes: 29

Views: 6668

Answers (5)

Kenneth Carter
Kenneth Carter

Reputation: 101

the extension answer is overly complicated for a public property exposed by IApplicationBuilder.

this code revised is a simpler approach:

public static bool IsMiddlewareAdded<T>(this IApplicationBuilder app) => app.IsMiddlewareAdded(typeof(T).Name);

public static bool IsMiddlewareAdded(this IApplicationBuilder app, string typeName)
{
    if (app.Properties.TryGetValue("__MiddlewareDescriptions", out var value) &&
        value is IList<string> descriptions)
    {
        return descriptions.Any(x => x.EndsWith(typeName));
    }
    return false;
}

Upvotes: 0

hawky
hawky

Reputation: 162

Here's a useful extension method that works on net8 (possibly others), using the middleware type to see if it's been added:

public static bool IsMiddlewareAdded<T>(this WebApplication app)
{
    PropertyInfo? propInfo = app.GetType().GetProperty("Properties", BindingFlags.Instance | BindingFlags.NonPublic);

    if (propInfo is null)
    {
        return false;
    }

    var properties = propInfo.GetValue(app) as IDictionary<string, object?>;

    return properties is not null &&
        properties.TryGetValue("__MiddlewareDescriptions", out var value) &&
        value is IList<string> descriptions &&
        descriptions.Any(x => x.Contains(typeof(T).Name));
}

Call like this:

bool isAdded = app.IsMiddlewareAdded<HttpsRedirectionMiddleware>();

Upvotes: 0

Ken Netherland
Ken Netherland

Reputation: 151

    public static List<object> GetMiddleware(this IApplicationBuilder app)
    {
        var list = new List<object>();
        var field = app.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "_components");
        var components = field.GetValue(app) as List<Func<RequestDelegate, RequestDelegate>>;

        components.ForEach(c =>
        {
            var middlewareField = c.Target.GetType().GetRuntimeFields().SingleOrDefault(pi => pi.Name == "_middleware");

            if (middlewareField != null)
            {
                var middlewareValue = middlewareField.GetValue(c.Target);

                list.Add(middlewareValue);
            }
        });

        return list;
    }

Upvotes: 1

shifatullah
shifatullah

Reputation: 798

Though question is to get middleware list during debugging, just in case someone needs, following reflection based code can be used to get list of registered middleware in request pipeline programmatically:

private void CollectListOfMiddleware(IApplicationBuilder app)
{
    _listOfMiddleware = new StringBuilder();
    FieldInfo _componentsField = app.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "_components");
    List<Func<RequestDelegate, RequestDelegate>> _componentsValue =
        _componentsField.GetValue(app) as List<Func<RequestDelegate, RequestDelegate>>;
    _componentsValue.ForEach(x =>
    {
        FieldInfo middlewareField = x.Target.GetType().GetRuntimeFields().Single(pi => pi.Name == "middleware");
        object middlewareValue = middlewareField.GetValue(x.Target);
        _listOfMiddleware.Append($"{middlewareValue.ToString()}\n");
    }
    );
}

Upvotes: 13

Jez
Jez

Reputation: 30073

From another question that someone's pointed out is very similar to this one:

The list of middleware is not publically available, for some reason. In debug mode, though, it is available by examining the IApplicationBuilder app variable during execution of the Configure method, specifically the _components non-public member. This non-public member is an IList<Func<RequestDelegate, RequestDelegate>>, containing a list of entries representing the middlewares that have been registered so far.

Upvotes: 24

Related Questions