@@ -4,7 +4,9 @@ namespace Microsoft.Extensions.DependencyInjection;
44
55using Asp . Versioning ;
66using Asp . Versioning . ApiExplorer ;
7+ using Microsoft . AspNetCore . Builder ;
78using Microsoft . AspNetCore . Mvc . ApiExplorer ;
9+ using Microsoft . AspNetCore . Mvc . Infrastructure ;
810using Microsoft . AspNetCore . Mvc . ModelBinding ;
911using Microsoft . AspNetCore . Routing ;
1012using Microsoft . Extensions . DependencyInjection . Extensions ;
@@ -61,7 +63,8 @@ private static void AddApiExplorerServices( IServiceCollection services )
6163
6264 services . AddMvcCore ( ) . AddApiExplorer ( ) ;
6365 services . TryAddSingleton < IOptionsFactory < ApiExplorerOptions > , ApiExplorerOptionsFactory < ApiExplorerOptions > > ( ) ;
64- services . TryAddSingleton < IApiVersionDescriptionProvider , DefaultApiVersionDescriptionProvider > ( ) ;
66+ services . TryAddTransient ( ResolveApiVersionDescriptionProviderFactory ) ;
67+ services . TryAddSingleton ( ResolveApiVersionDescriptionProvider ) ;
6568
6669 // use internal constructor until ASP.NET Core fixes their bug
6770 // BUG: https://github.com/dotnet/aspnetcore/issues/41773
@@ -73,4 +76,50 @@ private static void AddApiExplorerServices( IServiceCollection services )
7376 sp . GetRequiredService < IInlineConstraintResolver > ( ) ,
7477 sp . GetRequiredService < IOptions < ApiExplorerOptions > > ( ) ) ) ) ;
7578 }
79+
80+ private static IApiVersionDescriptionProviderFactory ResolveApiVersionDescriptionProviderFactory ( IServiceProvider serviceProvider )
81+ {
82+ var options = serviceProvider . GetRequiredService < IOptions < ApiExplorerOptions > > ( ) ;
83+ var mightUseCustomGroups = options . Value . FormatGroupName is not null ;
84+
85+ return new ApiVersionDescriptionProviderFactory ( serviceProvider , mightUseCustomGroups ? NewGroupedProvider : NewDefaultProvider ) ;
86+
87+ static IApiVersionDescriptionProvider NewDefaultProvider (
88+ EndpointDataSource endpointDataSource ,
89+ IActionDescriptorCollectionProvider actionDescriptorCollectionProvider ,
90+ ISunsetPolicyManager sunsetPolicyManager ,
91+ IOptions < ApiExplorerOptions > apiExplorerOptions ) =>
92+ new DefaultApiVersionDescriptionProvider ( endpointDataSource , actionDescriptorCollectionProvider , sunsetPolicyManager , apiExplorerOptions ) ;
93+
94+ static IApiVersionDescriptionProvider NewGroupedProvider (
95+ EndpointDataSource endpointDataSource ,
96+ IActionDescriptorCollectionProvider actionDescriptorCollectionProvider ,
97+ ISunsetPolicyManager sunsetPolicyManager ,
98+ IOptions < ApiExplorerOptions > apiExplorerOptions ) =>
99+ new GroupedApiVersionDescriptionProvider ( endpointDataSource , actionDescriptorCollectionProvider , sunsetPolicyManager , apiExplorerOptions ) ;
100+ }
101+
102+ private static IApiVersionDescriptionProvider ResolveApiVersionDescriptionProvider ( IServiceProvider serviceProvider )
103+ {
104+ var endpointDataSource = serviceProvider . GetRequiredService < EndpointDataSource > ( ) ;
105+ var actionDescriptorCollectionProvider = serviceProvider . GetRequiredService < IActionDescriptorCollectionProvider > ( ) ;
106+ var sunsetPolicyManager = serviceProvider . GetRequiredService < ISunsetPolicyManager > ( ) ;
107+ var options = serviceProvider . GetRequiredService < IOptions < ApiExplorerOptions > > ( ) ;
108+ var mightUseCustomGroups = options . Value . FormatGroupName is not null ;
109+
110+ if ( mightUseCustomGroups )
111+ {
112+ return new GroupedApiVersionDescriptionProvider (
113+ endpointDataSource ,
114+ actionDescriptorCollectionProvider ,
115+ sunsetPolicyManager ,
116+ options ) ;
117+ }
118+
119+ return new DefaultApiVersionDescriptionProvider (
120+ endpointDataSource ,
121+ actionDescriptorCollectionProvider ,
122+ sunsetPolicyManager ,
123+ options ) ;
124+ }
76125}
0 commit comments