@@ -4,7 +4,9 @@ namespace Microsoft.Extensions.DependencyInjection;
4
4
5
5
using Asp . Versioning ;
6
6
using Asp . Versioning . ApiExplorer ;
7
+ using Microsoft . AspNetCore . Builder ;
7
8
using Microsoft . AspNetCore . Mvc . ApiExplorer ;
9
+ using Microsoft . AspNetCore . Mvc . Infrastructure ;
8
10
using Microsoft . AspNetCore . Mvc . ModelBinding ;
9
11
using Microsoft . AspNetCore . Routing ;
10
12
using Microsoft . Extensions . DependencyInjection . Extensions ;
@@ -61,7 +63,8 @@ private static void AddApiExplorerServices( IServiceCollection services )
61
63
62
64
services . AddMvcCore ( ) . AddApiExplorer ( ) ;
63
65
services . TryAddSingleton < IOptionsFactory < ApiExplorerOptions > , ApiExplorerOptionsFactory < ApiExplorerOptions > > ( ) ;
64
- services . TryAddSingleton < IApiVersionDescriptionProvider , DefaultApiVersionDescriptionProvider > ( ) ;
66
+ services . TryAddTransient ( ResolveApiVersionDescriptionProviderFactory ) ;
67
+ services . TryAddSingleton ( ResolveApiVersionDescriptionProvider ) ;
65
68
66
69
// use internal constructor until ASP.NET Core fixes their bug
67
70
// BUG: https://github.com/dotnet/aspnetcore/issues/41773
@@ -73,4 +76,50 @@ private static void AddApiExplorerServices( IServiceCollection services )
73
76
sp . GetRequiredService < IInlineConstraintResolver > ( ) ,
74
77
sp . GetRequiredService < IOptions < ApiExplorerOptions > > ( ) ) ) ) ;
75
78
}
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
+ }
76
125
}
0 commit comments