@@ -5,13 +5,11 @@ namespace Microsoft.Extensions.DependencyInjection;
55using Asp . Versioning ;
66using Asp . Versioning . ApiExplorer ;
77using Microsoft . AspNetCore . Builder ;
8- using Microsoft . AspNetCore . Http ;
98using Microsoft . AspNetCore . Mvc . ApiExplorer ;
109using Microsoft . AspNetCore . Mvc . ModelBinding ;
1110using Microsoft . AspNetCore . Routing ;
1211using Microsoft . Extensions . DependencyInjection . Extensions ;
1312using Microsoft . Extensions . Options ;
14- using Microsoft . Extensions . Primitives ;
1513using static ServiceDescriptor ;
1614
1715/// <summary>
@@ -54,54 +52,17 @@ private static void AddApiExplorerServices( IApiVersioningBuilder builder )
5452
5553 services . AddMvcCore ( ) . AddApiExplorer ( ) ;
5654 services . TryAddSingleton < IOptionsFactory < ApiExplorerOptions > , ApiExplorerOptionsFactory < ApiExplorerOptions > > ( ) ;
57- services . TryAddTransient ( ResolveApiVersionDescriptionProviderFactory ) ;
58- services . TryAddSingleton ( ResolveApiVersionDescriptionProvider ) ;
55+ services . TryAddTransient < IApiVersionDescriptionProviderFactory , ApiVersionDescriptionProviderFactory > ( ) ;
56+ services . TryAddSingleton ( static sp => sp . GetRequiredService < IApiVersionDescriptionProviderFactory > ( ) . Create ( ) ) ;
5957
6058 // use internal constructor until ASP.NET Core fixes their bug
6159 // BUG: https://github.com/dotnet/aspnetcore/issues/41773
6260 services . TryAddEnumerable (
6361 Transient < IApiDescriptionProvider , VersionedApiDescriptionProvider > (
64- sp => new (
62+ static sp => new (
6563 sp . GetRequiredService < ISunsetPolicyManager > ( ) ,
6664 sp . GetRequiredService < IModelMetadataProvider > ( ) ,
6765 sp . GetRequiredService < IInlineConstraintResolver > ( ) ,
6866 sp . GetRequiredService < IOptions < ApiExplorerOptions > > ( ) ) ) ) ;
6967 }
70-
71- private static IApiVersionDescriptionProviderFactory ResolveApiVersionDescriptionProviderFactory ( IServiceProvider serviceProvider )
72- {
73- var sunsetPolicyManager = serviceProvider . GetRequiredService < ISunsetPolicyManager > ( ) ;
74- var providers = serviceProvider . GetServices < IApiVersionMetadataCollationProvider > ( ) ;
75- var inspector = serviceProvider . GetRequiredService < IEndpointInspector > ( ) ;
76- var options = serviceProvider . GetRequiredService < IOptions < ApiExplorerOptions > > ( ) ;
77-
78- return new ApiVersionDescriptionProviderFactory (
79- NewDefaultProvider ,
80- sunsetPolicyManager ,
81- providers ,
82- inspector ,
83- options ) ;
84-
85- static DefaultApiVersionDescriptionProvider NewDefaultProvider (
86- IEnumerable < IApiVersionMetadataCollationProvider > providers ,
87- ISunsetPolicyManager sunsetPolicyManager ,
88- IOptions < ApiExplorerOptions > apiExplorerOptions ) =>
89- new ( providers , sunsetPolicyManager , apiExplorerOptions ) ;
90- }
91-
92- private static IApiVersionDescriptionProvider ResolveApiVersionDescriptionProvider ( IServiceProvider serviceProvider )
93- {
94- var factory = serviceProvider . GetRequiredService < IApiVersionDescriptionProviderFactory > ( ) ;
95- var endpointDataSource = new EmptyEndpointDataSource ( ) ;
96- return factory . Create ( endpointDataSource ) ;
97- }
98-
99- private sealed class EmptyEndpointDataSource : EndpointDataSource
100- {
101- public override IReadOnlyList < Endpoint > Endpoints => Array . Empty < Endpoint > ( ) ;
102-
103- public override IChangeToken GetChangeToken ( ) => new CancellationChangeToken ( CancellationToken . None ) ;
104-
105- public override IReadOnlyList < Endpoint > GetGroupedEndpoints ( RouteGroupContext context ) => Array . Empty < Endpoint > ( ) ;
106- }
10768}
0 commit comments