88using Microsoft . AspNetCore . Builder ;
99using Microsoft . AspNetCore . HttpOverrides ;
1010using Microsoft . AspNetCore . Mvc ;
11- using Microsoft . AspNetCore . Mvc . Formatters ;
1211using Microsoft . AspNetCore . Mvc . ModelBinding ;
1312using Microsoft . Extensions . Configuration ;
1413using Microsoft . Extensions . DependencyInjection ;
@@ -30,15 +29,13 @@ public class ZGWApiOptions
3029 public ZGWApiServiceSettings ApiServiceSettings { get ; set ; } = new ( ) ;
3130 public Action < MvcNewtonsoftJsonOptions > NewtonsoftJsonOptions { get ; set ; } = null ;
3231 public Action < MvcOptions > MvcOptions { get ; set ; } = null ;
33- public Action < AddSwaggerOptions > AddSwaggerOptions { get ; set ; } = null ;
3432 public Action < SwaggerGenOptions > SwaggerGenOptions { get ; set ; } = null ;
33+ public Action < AddSwaggerOptions > AddSwaggerOptions { get ; set ; } = null ;
3534}
3635
3736public class ZGWApiServiceSettings
3837{
3938 public bool RegisterSharedAudittrailHandlers = false ;
40- public bool UseVNGVersioning = true ;
41- public bool AddXmlSerializerOutputFormatter = false ;
4239 public string ApiGroupNameFormat = "'v'VVV" ;
4340}
4441
@@ -56,58 +53,17 @@ public static void AddZGWApi(
5653 configureZgwApiOptions ? . Invoke ( zgwApiOptions ) ;
5754
5855 services . AddLocalization ( ) ;
59- services . Configure < ForwardedHeadersOptions > ( options =>
60- {
61- options . ForwardedHeaders = ForwardedHeaders . XForwardedProto ;
62-
63- options . KnownNetworks . Clear ( ) ;
64- options . KnownProxies . Clear ( ) ;
65-
66- var knownNetworks = configuration . GetSection ( "ForwardedHeaders:KnownNetworks" ) . Get < string [ ] > ( ) ?? [ ] ;
67- if ( knownNetworks . Length != 0 )
68- {
69- foreach ( var network in knownNetworks )
70- {
71- var parts = network . Split ( '/' ) ;
72-
73- if ( parts . Length == 2 && IPAddress . TryParse ( parts [ 0 ] , out var ipAddress ) && int . TryParse ( parts [ 1 ] , out var prefixLength ) )
74- {
75- options . KnownNetworks . Add ( new Microsoft . AspNetCore . HttpOverrides . IPNetwork ( ipAddress , prefixLength ) ) ;
76- }
77- else
78- {
79- throw new FormatException (
80- $ "Invalid network format in 'ForwardedHeaders:KnownNetworks': '{ network } '. Expected format is '[IPAddress]/[PrefixLength]'."
81- ) ;
82- }
83- }
84- }
85-
86- var knownProxies = configuration . GetSection ( "ForwardedHeaders:KnownProxies" ) . Get < string [ ] > ( ) ?? [ ] ;
87- if ( knownProxies . Length != 0 )
88- {
89- foreach ( var proxy in knownProxies )
90- {
91- options . KnownProxies . Add ( IPAddress . Parse ( proxy ) ) ;
92- }
93- }
94-
95- bool resolverForwardedHeader = configuration . GetValue ( "Application:ResolveForwardedHost" , false ) ;
96- if ( resolverForwardedHeader )
97- {
98- options . ForwardedHeaders |= ForwardedHeaders . XForwardedHost ;
99- }
100- } ) ;
56+ services . ConfigureForwardedHeaders ( configuration ) ;
10157
10258 var callingAssembly = Assembly . GetCallingAssembly ( ) ;
103- var executingAssembly = Assembly . GetExecutingAssembly ( ) ;
10459
10560 services . AddHealthChecks ( ) ;
10661
10762 services . AddMediatR ( x =>
10863 {
10964 x . RegisterServicesFromAssemblies ( callingAssembly ) ;
11065 } ) ;
66+
11167 if ( zgwApiOptions . ApiServiceSettings . RegisterSharedAudittrailHandlers )
11268 {
11369 services . AddMediatR ( x =>
@@ -118,15 +74,7 @@ public static void AddZGWApi(
11874
11975 services . AddScoped ( typeof ( IPipelineBehavior < , > ) , typeof ( HandlerLoggingBehavior < , > ) ) ;
12076
121- services . AddAutoMapper (
122- mappingConfiguration =>
123- {
124- mappingConfiguration . ShouldMapMethod = m => false ;
125- mappingConfiguration . Internal ( ) . Mappers . Insert ( 0 , new NullableEnumMapper ( ) ) ;
126- } ,
127- callingAssembly ,
128- executingAssembly
129- ) ;
77+ services . AddAutoMapper ( callingAssembly ) ;
13078
13179 // Replace the default IApiVersionParser implementation with our own implementation which supports patch numbr (like 1.3.1)
13280 services . Replace ( ServiceDescriptor . Transient < IApiVersionParser , ZgwApiVersionParser > ( ) ) ;
@@ -150,11 +98,6 @@ public static void AddZGWApi(
15098 options . ModelBinderProviders . Insert ( 0 , new GuidBinderProvider ( ) ) ;
15199 options . ReturnHttpNotAcceptable = true ;
152100
153- if ( zgwApiOptions . ApiServiceSettings . AddXmlSerializerOutputFormatter )
154- {
155- options . OutputFormatters . Add ( new XmlDataContractSerializerOutputFormatter ( ) ) ;
156- }
157-
158101 zgwApiOptions . MvcOptions ? . Invoke ( options ) ;
159102 } )
160103 . ConfigureApiBehaviorOptions ( options =>
@@ -184,12 +127,66 @@ public static void AddZGWApi(
184127 services . AddRouting ( options => options . LowercaseUrls = true ) ;
185128
186129 var zgwVersion = ApplicationInformation . GetVersion ( ) ;
187- services . AddSwagger (
188- apiName ,
189- zgwVersion ,
190- zgwApiOptions . ApiServiceSettings . UseVNGVersioning ,
191- zgwApiOptions . SwaggerGenOptions ,
192- zgwApiOptions . AddSwaggerOptions
130+ services . AddSwagger ( apiName , zgwVersion , zgwApiOptions . SwaggerGenOptions , zgwApiOptions . AddSwaggerOptions ) ;
131+ }
132+
133+ public static void AddAutoMapper ( this IServiceCollection services , Assembly callingAssembly )
134+ {
135+ var executingAssembly = Assembly . GetExecutingAssembly ( ) ;
136+ services . AddAutoMapper (
137+ mappingConfiguration =>
138+ {
139+ mappingConfiguration . ShouldMapMethod = m => false ;
140+ mappingConfiguration . Internal ( ) . Mappers . Insert ( 0 , new NullableEnumMapper ( ) ) ;
141+ } ,
142+ callingAssembly ,
143+ executingAssembly
193144 ) ;
194145 }
146+
147+ public static void ConfigureForwardedHeaders ( this IServiceCollection services , IConfiguration configuration )
148+ {
149+ services . Configure < ForwardedHeadersOptions > ( options =>
150+ {
151+ options . ForwardedHeaders = ForwardedHeaders . XForwardedProto ;
152+
153+ options . KnownNetworks . Clear ( ) ;
154+ options . KnownProxies . Clear ( ) ;
155+
156+ var knownNetworks = configuration . GetSection ( "ForwardedHeaders:KnownNetworks" ) . Get < string [ ] > ( ) ?? [ ] ;
157+ if ( knownNetworks . Length != 0 )
158+ {
159+ foreach ( var network in knownNetworks )
160+ {
161+ var parts = network . Split ( '/' ) ;
162+
163+ if ( parts . Length == 2 && IPAddress . TryParse ( parts [ 0 ] , out var ipAddress ) && int . TryParse ( parts [ 1 ] , out var prefixLength ) )
164+ {
165+ options . KnownNetworks . Add ( new Microsoft . AspNetCore . HttpOverrides . IPNetwork ( ipAddress , prefixLength ) ) ;
166+ }
167+ else
168+ {
169+ throw new FormatException (
170+ $ "Invalid network format in 'ForwardedHeaders:KnownNetworks': '{ network } '. Expected format is '[IPAddress]/[PrefixLength]'."
171+ ) ;
172+ }
173+ }
174+ }
175+
176+ var knownProxies = configuration . GetSection ( "ForwardedHeaders:KnownProxies" ) . Get < string [ ] > ( ) ?? [ ] ;
177+ if ( knownProxies . Length != 0 )
178+ {
179+ foreach ( var proxy in knownProxies )
180+ {
181+ options . KnownProxies . Add ( IPAddress . Parse ( proxy ) ) ;
182+ }
183+ }
184+
185+ var resolverForwardedHeader = configuration . GetValue ( "Application:ResolveForwardedHost" , false ) ;
186+ if ( resolverForwardedHeader )
187+ {
188+ options . ForwardedHeaders |= ForwardedHeaders . XForwardedHost ;
189+ }
190+ } ) ;
191+ }
195192}
0 commit comments