Created
February 6, 2021 09:20
-
-
Save stevetalkscode/9a7aa803812b09aa4a767b06b46de2df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) .NET Foundation. All rights reserved. | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | |
/// <summary> | |
/// Adds a middleware type to the application's request pipeline. | |
/// </summary> | |
/// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param> | |
/// <param name="middleware">The middleware type.</param> | |
/// <param name="args">The arguments to pass to the middleware type instance's constructor.</param> | |
/// <returns>The <see cref="IApplicationBuilder"/> instance.</returns> | |
public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app, [DynamicallyAccessedMembers(MiddlewareAccessibility)] Type middleware, params object?[] args) | |
{ | |
if (typeof(IMiddleware).IsAssignableFrom(middleware)) | |
{ | |
// IMiddleware doesn't support passing args directly since it's | |
// activated from the container | |
if (args.Length > 0) | |
{ | |
throw new NotSupportedException(Resources.FormatException_UseMiddlewareExplicitArgumentsNotSupported(typeof(IMiddleware))); | |
} | |
return UseMiddlewareInterface(app, middleware); | |
} | |
var applicationServices = app.ApplicationServices; | |
return app.Use(next => | |
{ | |
// contents elided for this example | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment