Skip to content

Commit b71cdc4

Browse files
Refactor DI so that IApiVersionDescriptionProviderFactory creates IApiVersionDescriptionProvider in all paths and can be the single source of replacement
1 parent 51033a9 commit b71cdc4

File tree

2 files changed

+8
-53
lines changed

2 files changed

+8
-53
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22

3-
#pragma warning disable SA1135 // Using directives should be qualified
4-
#pragma warning disable SA1200 // Using directives should be placed correctly
5-
6-
using Asp.Versioning;
7-
using Asp.Versioning.ApiExplorer;
8-
using Microsoft.Extensions.Options;
3+
#pragma warning disable CA1812 // Avoid uninstantiated internal classes
94

105
namespace Microsoft.AspNetCore.Builder;
116

7+
using Asp.Versioning;
8+
using Asp.Versioning.ApiExplorer;
129
using Microsoft.AspNetCore.Routing;
13-
using Activator = Func<IEnumerable<IApiVersionMetadataCollationProvider>, ISunsetPolicyManager, IOptions<ApiExplorerOptions>, IApiVersionDescriptionProvider>;
10+
using Microsoft.Extensions.Options;
1411

1512
internal sealed class ApiVersionDescriptionProviderFactory : IApiVersionDescriptionProviderFactory
1613
{
1714
private readonly ISunsetPolicyManager sunsetPolicyManager;
1815
private readonly IApiVersionMetadataCollationProvider[] providers;
1916
private readonly IEndpointInspector endpointInspector;
2017
private readonly IOptions<ApiExplorerOptions> options;
21-
private readonly Activator activator;
2218

2319
public ApiVersionDescriptionProviderFactory(
24-
Activator activator,
2520
ISunsetPolicyManager sunsetPolicyManager,
2621
IEnumerable<IApiVersionMetadataCollationProvider> providers,
2722
IEndpointInspector endpointInspector,
2823
IOptions<ApiExplorerOptions> options )
2924
{
30-
this.activator = activator;
3125
this.sunsetPolicyManager = sunsetPolicyManager;
3226
this.providers = providers.ToArray();
3327
this.endpointInspector = endpointInspector;
@@ -43,6 +37,6 @@ public IApiVersionDescriptionProvider Create( EndpointDataSource endpointDataSou
4337

4438
collators.AddRange( providers );
4539

46-
return activator( collators, sunsetPolicyManager, options );
40+
return new DefaultApiVersionDescriptionProvider( collators, sunsetPolicyManager, options );
4741
}
4842
}

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/DependencyInjection/IApiVersioningBuilderExtensions.cs

+3-42
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ namespace Microsoft.Extensions.DependencyInjection;
55
using Asp.Versioning;
66
using Asp.Versioning.ApiExplorer;
77
using Microsoft.AspNetCore.Builder;
8-
using Microsoft.AspNetCore.Http;
98
using Microsoft.AspNetCore.Mvc.ApiExplorer;
109
using Microsoft.AspNetCore.Mvc.ModelBinding;
1110
using Microsoft.AspNetCore.Routing;
1211
using Microsoft.Extensions.DependencyInjection.Extensions;
1312
using Microsoft.Extensions.Options;
14-
using Microsoft.Extensions.Primitives;
1513
using 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

Comments
 (0)