Add Extensions method for options (#23602)

This commit is contained in:
Kahbazi 2020-07-06 23:24:44 +04:30 committed by GitHub
parent bbb695fe16
commit 5a375a7c22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 191 additions and 2 deletions

View File

@ -105,3 +105,11 @@ namespace Microsoft.AspNetCore.Diagnostics
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
}
}
namespace Microsoft.Extensions.DependencyInjection
{
public static partial class ExceptionHandlerServiceCollectionExtensions
{
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddExceptionHandler(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.ExceptionHandlerOptions> configureOptions) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddExceptionHandler<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.ExceptionHandlerOptions, TService> configureOptions) where TService : class { throw null; }
}
}

View File

@ -0,0 +1,55 @@
// 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 System;
using Microsoft.AspNetCore.Builder;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for the exception handler middleware.
/// </summary>
public static class ExceptionHandlerServiceCollectionExtensions
{
/// <summary>
/// Adds services and options for the exception handler middleware.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
/// <param name="configureOptions">A delegate to configure the <see cref="ExceptionHandlerOptions"/>.</param>
/// <returns></returns>
public static IServiceCollection AddExceptionHandler(this IServiceCollection services, Action<ExceptionHandlerOptions> configureOptions)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
return services.Configure(configureOptions);
}
/// <summary>
/// Adds services and options for the exception handler middleware.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
/// <param name="configureOptions">A delegate to configure the <see cref="ExceptionHandlerOptions"/>.</param>
/// <returns></returns>
public static IServiceCollection AddExceptionHandler<TService>(this IServiceCollection services, Action<ExceptionHandlerOptions, TService> configureOptions) where TService : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
services.AddOptions<ExceptionHandlerOptions>().Configure(configureOptions);
return services;
}
}
}

View File

@ -248,7 +248,7 @@
<value>Environment:</value>
</data>
<data name="ExceptionHandlerOptions_NotConfiguredCorrectly" xml:space="preserve">
<value>An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'. Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.Configure&lt;ExceptionHandlerOptions&gt;(options =&gt; { ... });'.</value>
<value>An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'. Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.AddExceptionHandler(options =&gt; { ... });'.</value>
</data>
<data name="ErrorPageHtml_NoRouteValues" xml:space="preserve">
<value>No route values.</value>

View File

@ -408,7 +408,7 @@ namespace Microsoft.AspNetCore.Diagnostics
// Assert
Assert.Equal("An error occurred when configuring the exception handler middleware. " +
"Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'. " +
"Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.Configure<ExceptionHandlerOptions>(options => { ... });'.",
"Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.AddExceptionHandler(options => { ... });'.",
exception.Message);
}
}

View File

@ -108,3 +108,11 @@ namespace Microsoft.AspNetCore.Localization
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
}
}
namespace Microsoft.Extensions.DependencyInjection
{
public static partial class RequestLocalizationServiceCollectionExtensions
{
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddRequestLocalization(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.RequestLocalizationOptions> configureOptions) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddRequestLocalization<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.RequestLocalizationOptions, TService> configureOptions) where TService : class { throw null; }
}
}

View File

@ -0,0 +1,55 @@
// 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 System;
using Microsoft.AspNetCore.Builder;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for the request localization middleware.
/// </summary>
public static class RequestLocalizationServiceCollectionExtensions
{
/// <summary>
/// Adds services and options for the request localization middleware.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
/// <param name="configureOptions">A delegate to configure the <see cref="RequestLocalizationOptions"/>.</param>
/// <returns></returns>
public static IServiceCollection AddRequestLocalization(this IServiceCollection services, Action<RequestLocalizationOptions> configureOptions)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
return services.Configure(configureOptions);
}
/// <summary>
/// Adds services and options for the request localization middleware.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
/// <param name="configureOptions">A delegate to configure the <see cref="RequestLocalizationOptions"/>.</param>
/// <returns></returns>
public static IServiceCollection AddRequestLocalization<TService>(this IServiceCollection services, Action<RequestLocalizationOptions, TService> configureOptions) where TService : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
services.AddOptions<RequestLocalizationOptions>().Configure(configureOptions);
return services;
}
}
}

View File

@ -56,3 +56,11 @@ namespace Microsoft.AspNetCore.CookiePolicy
Always = 1,
}
}
namespace Microsoft.Extensions.DependencyInjection
{
public static partial class CookiePolicyServiceCollectionExtensions
{
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddCookiePolicy(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.CookiePolicyOptions> configureOptions) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddCookiePolicy<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.CookiePolicyOptions, TService> configureOptions) where TService : class { throw null; }
}
}

View File

@ -0,0 +1,55 @@
// 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 System;
using Microsoft.AspNetCore.Builder;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for the cookie policy middleware.
/// </summary>
public static class CookiePolicyServiceCollectionExtensions
{
/// <summary>
/// Adds services and options for the cookie policy middleware.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
/// <param name="configureOptions">A delegate to configure the <see cref="CookiePolicyOptions"/>.</param>
/// <returns></returns>
public static IServiceCollection AddCookiePolicy(this IServiceCollection services, Action<CookiePolicyOptions> configureOptions)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
return services.Configure(configureOptions);
}
/// <summary>
/// Adds services and options for the cookie policy middleware.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
/// <param name="configureOptions">A delegate to configure the <see cref="CookiePolicyOptions"/>.</param>
/// <returns></returns>
public static IServiceCollection AddCookiePolicy<TService>(this IServiceCollection services, Action<CookiePolicyOptions, TService> configureOptions) where TService : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
services.AddOptions<CookiePolicyOptions>().Configure(configureOptions);
return services;
}
}
}