Skip to content

Commit 5a375a7

Browse files
authored
Add Extensions method for options (#23602)
1 parent bbb695f commit 5a375a7

8 files changed

+191
-2
lines changed

src/Middleware/Diagnostics/ref/Microsoft.AspNetCore.Diagnostics.netcoreapp.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@ public WelcomePageMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Mic
105105
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
106106
}
107107
}
108+
namespace Microsoft.Extensions.DependencyInjection
109+
{
110+
public static partial class ExceptionHandlerServiceCollectionExtensions
111+
{
112+
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddExceptionHandler(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.ExceptionHandlerOptions> configureOptions) { throw null; }
113+
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; }
114+
}
115+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Builder;
6+
7+
namespace Microsoft.Extensions.DependencyInjection
8+
{
9+
/// <summary>
10+
/// Extension methods for the exception handler middleware.
11+
/// </summary>
12+
public static class ExceptionHandlerServiceCollectionExtensions
13+
{
14+
/// <summary>
15+
/// Adds services and options for the exception handler middleware.
16+
/// </summary>
17+
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
18+
/// <param name="configureOptions">A delegate to configure the <see cref="ExceptionHandlerOptions"/>.</param>
19+
/// <returns></returns>
20+
public static IServiceCollection AddExceptionHandler(this IServiceCollection services, Action<ExceptionHandlerOptions> configureOptions)
21+
{
22+
if (services == null)
23+
{
24+
throw new ArgumentNullException(nameof(services));
25+
}
26+
if (configureOptions == null)
27+
{
28+
throw new ArgumentNullException(nameof(configureOptions));
29+
}
30+
31+
return services.Configure(configureOptions);
32+
}
33+
34+
/// <summary>
35+
/// Adds services and options for the exception handler middleware.
36+
/// </summary>
37+
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
38+
/// <param name="configureOptions">A delegate to configure the <see cref="ExceptionHandlerOptions"/>.</param>
39+
/// <returns></returns>
40+
public static IServiceCollection AddExceptionHandler<TService>(this IServiceCollection services, Action<ExceptionHandlerOptions, TService> configureOptions) where TService : class
41+
{
42+
if (services == null)
43+
{
44+
throw new ArgumentNullException(nameof(services));
45+
}
46+
if (configureOptions == null)
47+
{
48+
throw new ArgumentNullException(nameof(configureOptions));
49+
}
50+
51+
services.AddOptions<ExceptionHandlerOptions>().Configure(configureOptions);
52+
return services;
53+
}
54+
}
55+
}

src/Middleware/Diagnostics/src/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
<value>Environment:</value>
249249
</data>
250250
<data name="ExceptionHandlerOptions_NotConfiguredCorrectly" xml:space="preserve">
251-
<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>
251+
<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>
252252
</data>
253253
<data name="ErrorPageHtml_NoRouteValues" xml:space="preserve">
254254
<value>No route values.</value>

src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ public void UsingExceptionHandler_ThrowsAnException_WhenExceptionHandlingPathNot
408408
// Assert
409409
Assert.Equal("An error occurred when configuring the exception handler middleware. " +
410410
"Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'. " +
411-
"Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.Configure<ExceptionHandlerOptions>(options => { ... });'.",
411+
"Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.AddExceptionHandler(options => { ... });'.",
412412
exception.Message);
413413
}
414414
}

src/Middleware/Localization/ref/Microsoft.AspNetCore.Localization.netcoreapp.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,11 @@ public RequestLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate n
108108
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
109109
}
110110
}
111+
namespace Microsoft.Extensions.DependencyInjection
112+
{
113+
public static partial class RequestLocalizationServiceCollectionExtensions
114+
{
115+
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddRequestLocalization(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.RequestLocalizationOptions> configureOptions) { throw null; }
116+
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; }
117+
}
118+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Builder;
6+
7+
namespace Microsoft.Extensions.DependencyInjection
8+
{
9+
/// <summary>
10+
/// Extension methods for the request localization middleware.
11+
/// </summary>
12+
public static class RequestLocalizationServiceCollectionExtensions
13+
{
14+
/// <summary>
15+
/// Adds services and options for the request localization middleware.
16+
/// </summary>
17+
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
18+
/// <param name="configureOptions">A delegate to configure the <see cref="RequestLocalizationOptions"/>.</param>
19+
/// <returns></returns>
20+
public static IServiceCollection AddRequestLocalization(this IServiceCollection services, Action<RequestLocalizationOptions> configureOptions)
21+
{
22+
if (services == null)
23+
{
24+
throw new ArgumentNullException(nameof(services));
25+
}
26+
if (configureOptions == null)
27+
{
28+
throw new ArgumentNullException(nameof(configureOptions));
29+
}
30+
31+
return services.Configure(configureOptions);
32+
}
33+
34+
/// <summary>
35+
/// Adds services and options for the request localization middleware.
36+
/// </summary>
37+
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
38+
/// <param name="configureOptions">A delegate to configure the <see cref="RequestLocalizationOptions"/>.</param>
39+
/// <returns></returns>
40+
public static IServiceCollection AddRequestLocalization<TService>(this IServiceCollection services, Action<RequestLocalizationOptions, TService> configureOptions) where TService : class
41+
{
42+
if (services == null)
43+
{
44+
throw new ArgumentNullException(nameof(services));
45+
}
46+
if (configureOptions == null)
47+
{
48+
throw new ArgumentNullException(nameof(configureOptions));
49+
}
50+
51+
services.AddOptions<RequestLocalizationOptions>().Configure(configureOptions);
52+
return services;
53+
}
54+
}
55+
}

src/Security/CookiePolicy/ref/Microsoft.AspNetCore.CookiePolicy.netcoreapp.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ public enum HttpOnlyPolicy
5656
Always = 1,
5757
}
5858
}
59+
namespace Microsoft.Extensions.DependencyInjection
60+
{
61+
public static partial class CookiePolicyServiceCollectionExtensions
62+
{
63+
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddCookiePolicy(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Builder.CookiePolicyOptions> configureOptions) { throw null; }
64+
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; }
65+
}
66+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Builder;
6+
7+
namespace Microsoft.Extensions.DependencyInjection
8+
{
9+
/// <summary>
10+
/// Extension methods for the cookie policy middleware.
11+
/// </summary>
12+
public static class CookiePolicyServiceCollectionExtensions
13+
{
14+
/// <summary>
15+
/// Adds services and options for the cookie policy middleware.
16+
/// </summary>
17+
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
18+
/// <param name="configureOptions">A delegate to configure the <see cref="CookiePolicyOptions"/>.</param>
19+
/// <returns></returns>
20+
public static IServiceCollection AddCookiePolicy(this IServiceCollection services, Action<CookiePolicyOptions> configureOptions)
21+
{
22+
if (services == null)
23+
{
24+
throw new ArgumentNullException(nameof(services));
25+
}
26+
if (configureOptions == null)
27+
{
28+
throw new ArgumentNullException(nameof(configureOptions));
29+
}
30+
31+
return services.Configure(configureOptions);
32+
}
33+
34+
/// <summary>
35+
/// Adds services and options for the cookie policy middleware.
36+
/// </summary>
37+
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
38+
/// <param name="configureOptions">A delegate to configure the <see cref="CookiePolicyOptions"/>.</param>
39+
/// <returns></returns>
40+
public static IServiceCollection AddCookiePolicy<TService>(this IServiceCollection services, Action<CookiePolicyOptions, TService> configureOptions) where TService : class
41+
{
42+
if (services == null)
43+
{
44+
throw new ArgumentNullException(nameof(services));
45+
}
46+
if (configureOptions == null)
47+
{
48+
throw new ArgumentNullException(nameof(configureOptions));
49+
}
50+
51+
services.AddOptions<CookiePolicyOptions>().Configure(configureOptions);
52+
return services;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)