Standardize middleware to use configureOption lambda

This commit is contained in:
John Luo 2015-12-22 18:44:18 -08:00
parent 8b71e9232f
commit 9f1af65abc
9 changed files with 210 additions and 89 deletions

View File

@ -14,33 +14,33 @@ namespace Microsoft.AspNet.Builder
/// <summary>
/// Enables the Elm logging service, which can be accessed via the <see cref="ElmPageMiddleware"/>.
/// </summary>
public static IApplicationBuilder UseElmCapture(this IApplicationBuilder builder)
public static IApplicationBuilder UseElmCapture(this IApplicationBuilder app)
{
if (builder == null)
if (app == null)
{
throw new ArgumentNullException(nameof(builder));
throw new ArgumentNullException(nameof(app));
}
// add the elm provider to the factory here so the logger can start capturing logs immediately
var factory = builder.ApplicationServices.GetRequiredService<ILoggerFactory>();
var store = builder.ApplicationServices.GetRequiredService<ElmStore>();
var options = builder.ApplicationServices.GetService<IOptions<ElmOptions>>();
var factory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
var store = app.ApplicationServices.GetRequiredService<ElmStore>();
var options = app.ApplicationServices.GetService<IOptions<ElmOptions>>();
factory.AddProvider(new ElmLoggerProvider(store, options?.Value ?? new ElmOptions()));
return builder.UseMiddleware<ElmCaptureMiddleware>();
return app.UseMiddleware<ElmCaptureMiddleware>();
}
/// <summary>
/// Enables viewing logs captured by the <see cref="ElmCaptureMiddleware"/>.
/// </summary>
public static IApplicationBuilder UseElmPage(this IApplicationBuilder builder)
public static IApplicationBuilder UseElmPage(this IApplicationBuilder app)
{
if (builder == null)
if (app == null)
{
throw new ArgumentNullException(nameof(builder));
throw new ArgumentNullException(nameof(app));
}
return builder.UseMiddleware<ElmPageMiddleware>();
return app.UseMiddleware<ElmPageMiddleware>();
}
}
}

View File

@ -20,9 +20,12 @@ namespace Microsoft.AspNet.Builder
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to register the middleware with.</param>
/// <returns>The same <see cref="IApplicationBuilder"/> instance so that multiple calls can be chained.</returns>
public static IApplicationBuilder UseDatabaseErrorPage([NotNull] this IApplicationBuilder app)
public static IApplicationBuilder UseDatabaseErrorPage(this IApplicationBuilder app)
{
Check.NotNull(app, nameof(app));
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseDatabaseErrorPage(options => options.EnableAll());
}
@ -32,19 +35,25 @@ namespace Microsoft.AspNet.Builder
/// migrations. When these exceptions occur an HTML response with details of possible actions to resolve the issue is generated.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to register the middleware with.</param>
/// <param name="optionsAction">An action to set the options for the middleware. All options are disabled by default.</param>
/// <param name="configureOptions">An action to set the options for the middleware. All options are disabled by default.</param>
/// <returns>The same <see cref="IApplicationBuilder"/> instance so that multiple calls can be chained.</returns>
public static IApplicationBuilder UseDatabaseErrorPage([NotNull] this IApplicationBuilder app, [NotNull] Action<DatabaseErrorPageOptions> optionsAction)
public static IApplicationBuilder UseDatabaseErrorPage(this IApplicationBuilder app, Action<DatabaseErrorPageOptions> configureOptions)
{
Check.NotNull(app, nameof(app));
Check.NotNull(optionsAction, nameof(optionsAction));
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new DatabaseErrorPageOptions();
optionsAction(options);
configureOptions(options);
app = app.UseMiddleware<DatabaseErrorPageMiddleware>(options);
if(options.EnableMigrationCommands)
if (options.EnableMigrationCommands)
{
app.UseMigrationsEndPoint(o => o.Path = options.MigrationsEndPointPath);
}
@ -56,9 +65,12 @@ namespace Microsoft.AspNet.Builder
/// Sets the options to display the maximum amount of information available.
/// </summary>
/// <param name="options">The options to be configured.</param>
public static void EnableAll([NotNull] this DatabaseErrorPageOptions options)
public static void EnableAll(this DatabaseErrorPageOptions options)
{
Check.NotNull(options, nameof(options));
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
options.ShowExceptionDetails = true;
options.ListMigrations = true;

View File

@ -18,26 +18,30 @@ namespace Microsoft.AspNet.Builder
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to register the middleware with.</param>
/// <returns>The same <see cref="IApplicationBuilder"/> instance so that multiple calls can be chained.</returns>
public static IApplicationBuilder UseMigrationsEndPoint([NotNull] this IApplicationBuilder app)
public static IApplicationBuilder UseMigrationsEndPoint(this IApplicationBuilder app)
{
Check.NotNull(app, "builder");
return app.UseMigrationsEndPoint(options => { });
}
/// <summary>
/// Processes requests to execute migrations operations. The middleware will listen for requests to the path configured in <paramref name="optionsAction"/>.
/// Processes requests to execute migrations operations. The middleware will listen for requests to the path configured in <paramref name="configureOptions"/>.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to register the middleware with.</param>
/// <param name="optionsAction">An action to set the options for the middleware.</param>
/// <param name="configureOptions">An action to set the options for the middleware.</param>
/// <returns>The same <see cref="IApplicationBuilder"/> instance so that multiple calls can be chained.</returns>
public static IApplicationBuilder UseMigrationsEndPoint([NotNull] this IApplicationBuilder app, [NotNull] Action<MigrationsEndPointOptions> optionsAction)
public static IApplicationBuilder UseMigrationsEndPoint(this IApplicationBuilder app, Action<MigrationsEndPointOptions> configureOptions)
{
Check.NotNull(app, "builder");
Check.NotNull(optionsAction, "optionsAction");
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new MigrationsEndPointOptions();
optionsAction(options);
configureOptions(options);
return app.UseMiddleware<MigrationsEndPointMiddleware>(options);
}

View File

@ -14,31 +14,31 @@ namespace Microsoft.AspNet.Builder
/// <summary>
/// Captures synchronous and asynchronous <see cref="Exception"/> instances from the pipeline and generates HTML error responses.
/// </summary>
/// <param name="builder">The <see cref="IApplicationBuilder"/>.</param>
/// <returns>A reference to the <paramref name="builder"/> after the operation has completed.</returns>
public static IApplicationBuilder UseDeveloperExceptionPage(this IApplicationBuilder builder)
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <returns>A reference to the <paramref name="app"/> after the operation has completed.</returns>
public static IApplicationBuilder UseDeveloperExceptionPage(this IApplicationBuilder app)
{
if (builder == null)
if (app == null)
{
throw new ArgumentNullException(nameof(builder));
throw new ArgumentNullException(nameof(app));
}
return builder.UseDeveloperExceptionPage(options => { });
return app.UseDeveloperExceptionPage(options => { });
}
/// <summary>
/// Captures synchronous and asynchronous <see cref="Exception"/> instances from the pipeline and generates HTML error responses.
/// </summary>
/// <param name="builder">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="configureOptions">A callback to configure <see cref="DeveloperExceptionPageOptions"/>.</param>
/// <returns>A reference to the <paramref name="builder"/> after the operation has completed.</returns>
/// <returns>A reference to the <paramref name="app"/> after the operation has completed.</returns>
public static IApplicationBuilder UseDeveloperExceptionPage(
this IApplicationBuilder builder,
this IApplicationBuilder app,
Action<DeveloperExceptionPageOptions> configureOptions)
{
if (builder == null)
if (app == null)
{
throw new ArgumentNullException(nameof(builder));
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
@ -48,7 +48,7 @@ namespace Microsoft.AspNet.Builder
var options = new DeveloperExceptionPageOptions();
configureOptions(options);
return builder.UseMiddleware<DeveloperExceptionPageMiddleware>(options);
return app.UseMiddleware<DeveloperExceptionPageMiddleware>(options);
}
}
}

View File

@ -18,11 +18,12 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns>
public static IApplicationBuilder UseExceptionHandler(this IApplicationBuilder app, string errorHandlingPath)
{
var options = new ExceptionHandlerOptions()
if (app == null)
{
ExceptionHandlingPath = new PathString(errorHandlingPath)
};
return app.UseMiddleware<ExceptionHandlerMiddleware>(options);
throw new ArgumentNullException(nameof(app));
}
return app.UseExceptionHandler(options => { options.ExceptionHandlingPath = new PathString(errorHandlingPath); });
}
/// <summary>
@ -34,13 +35,43 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns>
public static IApplicationBuilder UseExceptionHandler(this IApplicationBuilder app, Action<IApplicationBuilder> configure)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
var subAppBuilder = app.New();
configure(subAppBuilder);
var exceptionHandlerPipeline = subAppBuilder.Build();
var options = new ExceptionHandlerOptions()
return app.UseExceptionHandler(options => { options.ExceptionHandler = exceptionHandlerPipeline; });
}
/// <summary>
/// Adds a middleware to the pipeline that will catch exceptions, log them, and re-execute the request in an alternate pipeline.
/// The request will not be re-executed if the response has already started.
/// </summary>
/// <param name="app"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IApplicationBuilder UseExceptionHandler(this IApplicationBuilder app, Action<ExceptionHandlerOptions> configureOptions)
{
if (app == null)
{
ExceptionHandler = exceptionHandlerPipeline
};
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new ExceptionHandlerOptions();
configureOptions(options);
return app.UseMiddleware<ExceptionHandlerMiddleware>(options);
}
}

View File

@ -1,7 +1,6 @@
// 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.
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Diagnostics

View File

@ -10,33 +10,45 @@ namespace Microsoft.AspNet.Builder
{
public static class RuntimeInfoExtensions
{
public static IApplicationBuilder UseRuntimeInfoPage(this IApplicationBuilder builder)
public static IApplicationBuilder UseRuntimeInfoPage(this IApplicationBuilder app)
{
return UseRuntimeInfoPage(builder, new RuntimeInfoPageOptions());
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseRuntimeInfoPage(options => { });
}
public static IApplicationBuilder UseRuntimeInfoPage(this IApplicationBuilder builder, string path)
public static IApplicationBuilder UseRuntimeInfoPage(this IApplicationBuilder app, string path)
{
return UseRuntimeInfoPage(builder, new RuntimeInfoPageOptions() { Path = new PathString(path) });
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseRuntimeInfoPage(options => { options.Path = new PathString(path); });
}
public static IApplicationBuilder UseRuntimeInfoPage(
this IApplicationBuilder builder,
RuntimeInfoPageOptions options)
this IApplicationBuilder app,
Action<RuntimeInfoPageOptions> configureOptions)
{
if (builder == null)
if (app == null)
{
throw new ArgumentNullException(nameof(builder));
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
var libraryManager = app.ApplicationServices.GetService(typeof(ILibraryManager)) as ILibraryManager;
var runtimeEnvironment = app.ApplicationServices.GetService(typeof(IRuntimeEnvironment)) as IRuntimeEnvironment;
var options = new RuntimeInfoPageOptions();
configureOptions(options);
var libraryManager = builder.ApplicationServices.GetService(typeof(ILibraryManager)) as ILibraryManager;
var runtimeEnvironment = builder.ApplicationServices.GetService(typeof(IRuntimeEnvironment)) as IRuntimeEnvironment;
return builder.Use(next => new RuntimeInfoMiddleware(next, options, libraryManager, runtimeEnvironment).Invoke);
return app.Use(next => new RuntimeInfoMiddleware(next, options, libraryManager, runtimeEnvironment).Invoke);
}
}
}

View File

@ -17,14 +17,21 @@ namespace Microsoft.AspNet.Builder
/// between 400 and 599 that do not have a body.
/// </summary>
/// <param name="app"></param>
/// <param name="options"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, StatusCodePagesOptions options)
public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, Action<StatusCodePagesOptions> configureOptions)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new StatusCodePagesOptions();
configureOptions(options);
return app.UseMiddleware<StatusCodePagesMiddleware>(options);
}
@ -37,7 +44,12 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns>
public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app)
{
return UseStatusCodePages(app, new StatusCodePagesOptions());
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseStatusCodePages(configureOptions: options => { });
}
/// <summary>
@ -49,7 +61,16 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns>
public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, Func<StatusCodeContext, Task> handler)
{
return UseStatusCodePages(app, new StatusCodePagesOptions() { HandleAsync = handler });
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
return app.UseStatusCodePages(options => { options.HandleAsync = handler; });
}
/// <summary>
@ -62,7 +83,12 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns>
public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, string contentType, string bodyFormat)
{
return UseStatusCodePages(app, context =>
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseStatusCodePages(context =>
{
var body = string.Format(CultureInfo.InvariantCulture, bodyFormat, context.HttpContext.Response.StatusCode);
context.HttpContext.Response.ContentType = contentType;
@ -80,10 +106,15 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns>
public static IApplicationBuilder UseStatusCodePagesWithRedirects(this IApplicationBuilder app, string locationFormat)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (locationFormat.StartsWith("~"))
{
locationFormat = locationFormat.Substring(1);
return UseStatusCodePages(app, context =>
return app.UseStatusCodePages(context =>
{
var location = string.Format(CultureInfo.InvariantCulture, locationFormat, context.HttpContext.Response.StatusCode);
context.HttpContext.Response.Redirect(context.HttpContext.Request.PathBase + location);
@ -92,7 +123,7 @@ namespace Microsoft.AspNet.Builder
}
else
{
return UseStatusCodePages(app, context =>
return app.UseStatusCodePages(context =>
{
var location = string.Format(CultureInfo.InvariantCulture, locationFormat, context.HttpContext.Response.StatusCode);
context.HttpContext.Response.Redirect(location);
@ -110,10 +141,15 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns>
public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, Action<IApplicationBuilder> configuration)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
var builder = app.New();
configuration(builder);
var tangent = builder.Build();
return UseStatusCodePages(app, context => tangent(context.HttpContext));
return app.UseStatusCodePages(context => tangent(context.HttpContext));
}
/// <summary>
@ -125,7 +161,12 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns>
public static IApplicationBuilder UseStatusCodePagesWithReExecute(this IApplicationBuilder app, string pathFormat)
{
return UseStatusCodePages(app, async context =>
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseStatusCodePages(async context =>
{
var newPath = new PathString(string.Format(CultureInfo.InvariantCulture, pathFormat, context.HttpContext.Response.StatusCode));

View File

@ -16,49 +16,71 @@ namespace Microsoft.AspNet.Builder
/// <summary>
/// Adds the WelcomePageMiddleware to the pipeline with the given options.
/// </summary>
/// <param name="builder"></param>
/// <param name="options"></param>
/// <param name="app"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder builder, WelcomePageOptions options)
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder app, Action<WelcomePageOptions> configureOptions)
{
if (builder == null)
if (app == null)
{
throw new ArgumentNullException(nameof(builder));
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
return builder.Use(next => new WelcomePageMiddleware(next, options).Invoke);
var options = new WelcomePageOptions();
configureOptions(options);
return app.UseMiddleware<WelcomePageMiddleware>(options);
}
/// <summary>
/// Adds the WelcomePageMiddleware to the pipeline with the given path.
/// </summary>
/// <param name="builder"></param>
/// <param name="app"></param>
/// <param name="path"></param>
/// <returns></returns>
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder builder, PathString path)
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder app, PathString path)
{
return UseWelcomePage(builder, new WelcomePageOptions { Path = path });
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseWelcomePage(options => { options.Path = path; });
}
/// <summary>
/// Adds the WelcomePageMiddleware to the pipeline with the given path.
/// </summary>
/// <param name="builder"></param>
/// <param name="app"></param>
/// <param name="path"></param>
/// <returns></returns>
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder builder, string path)
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder app, string path)
{
return UseWelcomePage(builder, new WelcomePageOptions { Path = new PathString(path) });
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseWelcomePage(options => { options.Path = new PathString(path); });
}
/// <summary>
/// Adds the WelcomePageMiddleware to the pipeline.
/// </summary>
/// <param name="builder"></param>
/// <param name="app"></param>
/// <returns></returns>
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder builder)
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder app)
{
return UseWelcomePage(builder, new WelcomePageOptions());
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseWelcomePage(options => { });
}
}
}