@@ -13,50 +13,62 @@ namespace TinyHelpers.AspNetCore.Extensions;
1313
1414public static class ServiceCollectionExtensions
1515{
16- public static T ? ConfigureAndGet < T > ( this IServiceCollection services , IConfiguration configuration , string sectionName ) where T : class
16+ extension ( IServiceCollection services )
1717 {
18- var section = configuration . GetSection ( sectionName ) ;
19- var settings = section . Get < T > ( ) ;
20- services . Configure < T > ( section ) ;
21-
22- return settings ;
23- }
18+ public T ? ConfigureAndGet < T > ( IConfiguration configuration , string sectionName ) where T : class
19+ {
20+ var section = configuration . GetSection ( sectionName ) ;
21+ var settings = section . Get < T > ( ) ;
22+ services . Configure < T > ( section ) ;
2423
25- public static IServiceCollection AddRequestLocalization ( this IServiceCollection services , params string [ ] cultures )
26- => services . AddRequestLocalization ( cultures , null ) ;
24+ return settings ;
25+ }
2726
28- public static IServiceCollection AddRequestLocalization ( this IServiceCollection services , IEnumerable < string > cultures , Action < IList < IRequestCultureProvider > > ? providersConfiguration )
29- {
30- var supportedCultures = cultures . Select ( c => new CultureInfo ( c ) ) . ToList ( ) ;
27+ public IServiceCollection AddRequestLocalization ( params string [ ] cultures )
28+ => services . AddRequestLocalization ( cultures , null ) ;
3129
32- services . Configure < RequestLocalizationOptions > ( options =>
30+ public IServiceCollection AddRequestLocalization ( IEnumerable < string > cultures , Action < IList < IRequestCultureProvider > > ? providersConfiguration )
3331 {
34- options . SupportedCultures = supportedCultures ;
35- options . SupportedUICultures = supportedCultures ;
36- options . DefaultRequestCulture = new RequestCulture ( supportedCultures . First ( ) ) ;
37- providersConfiguration ? . Invoke ( options . RequestCultureProviders ) ;
38- } ) ;
32+ var supportedCultures = cultures . Select ( c => new CultureInfo ( c ) ) . ToList ( ) ;
33+
34+ services . Configure < RequestLocalizationOptions > ( options =>
35+ {
36+ options . SupportedCultures = supportedCultures ;
37+ options . SupportedUICultures = supportedCultures ;
38+ options . DefaultRequestCulture = new RequestCulture ( supportedCultures . First ( ) ) ;
39+ providersConfiguration ? . Invoke ( options . RequestCultureProviders ) ;
40+ } ) ;
41+
42+ return services ;
43+ }
44+
45+ public IServiceCollection Replace < TService , TImplementation > ( ServiceLifetime lifetime = ServiceLifetime . Scoped )
46+ where TService : class
47+ where TImplementation : class , TService
48+ {
49+ services . Replace ( new ServiceDescriptor ( serviceType : typeof ( TService ) , implementationType : typeof ( TImplementation ) , lifetime ) ) ;
3950
40- return services ;
41- }
51+ return services ;
52+ }
4253
43- public static IServiceCollection Replace < TService , TImplementation > ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped )
44- where TService : class
45- where TImplementation : class , TService
46- {
47- services . Replace ( new ServiceDescriptor ( serviceType : typeof ( TService ) , implementationType : typeof ( TImplementation ) , lifetime ) ) ;
54+ public IServiceCollection AddDefaultProblemDetails ( )
55+ {
56+ services . AddProblemDetails ( options =>
57+ {
58+ options . CustomizeProblemDetails = context => context . UseDefaults ( ) ;
59+ } ) ;
4860
49- return services ;
50- }
61+ return services ;
62+ }
5163
52- public static IServiceCollection AddDefaultProblemDetails ( this IServiceCollection services )
53- {
54- services . AddProblemDetails ( options =>
64+ public IServiceCollection AddDefaultExceptionHandler ( )
5565 {
56- options . CustomizeProblemDetails = context => context . UseDefaults ( ) ;
57- } ) ;
66+ // Ensures that the ProblemDetails service is registered.
67+ services . AddProblemDetails ( ) ;
5868
59- return services ;
69+ services . AddExceptionHandler < DefaultExceptionHandler > ( ) ;
70+ return services ;
71+ }
6072 }
6173
6274 public static void UseDefaults ( this ProblemDetailsContext context )
@@ -70,13 +82,4 @@ public static void UseDefaults(this ProblemDetailsContext context)
7082
7183 context . ProblemDetails . Extensions . TryAdd ( "traceId" , Activity . Current ? . Id ?? context . HttpContext . TraceIdentifier ) ;
7284 }
73-
74- public static IServiceCollection AddDefaultExceptionHandler ( this IServiceCollection services )
75- {
76- // Ensures that the ProblemDetails service is registered.
77- services . AddProblemDetails ( ) ;
78-
79- services . AddExceptionHandler < DefaultExceptionHandler > ( ) ;
80- return services ;
81- }
8285}
0 commit comments