@@ -9,7 +9,6 @@ namespace Microsoft.Extensions.DependencyInjection;
99using Microsoft . AspNetCore . Http ;
1010using Microsoft . AspNetCore . Http . Features ;
1111using Microsoft . AspNetCore . Mvc . ApplicationModels ;
12- using Microsoft . AspNetCore . Mvc . ApplicationParts ;
1312using Microsoft . AspNetCore . OData ;
1413using Microsoft . AspNetCore . OData . Routing . Template ;
1514using Microsoft . AspNetCore . Routing ;
@@ -69,8 +68,7 @@ private static void AddServices( IServiceCollection services )
6968 services . TryRemoveODataService ( typeof ( IApplicationModelProvider ) , ODataRoutingApplicationModelProviderType ) ;
7069
7170 var partManager = services . GetOrCreateApplicationPartManager ( ) ;
72-
73- ConfigureDefaultFeatureProviders ( partManager ) ;
71+ var configured = partManager . ConfigureDefaultFeatureProviders ( ) ;
7472
7573 services . AddHttpContextAccessor ( ) ;
7674 services . TryAddSingleton < VersionedODataOptions > ( ) ;
@@ -87,24 +85,11 @@ private static void AddServices( IServiceCollection services )
8785 services . TryAddEnumerable ( Singleton < MatcherPolicy , DefaultMetadataMatcherPolicy > ( ) ) ;
8886 services . TryAddEnumerable ( Transient < IApplicationModelProvider , ODataApplicationModelProvider > ( ) ) ;
8987 services . TryAddEnumerable ( Transient < IApplicationModelProvider , ODataMultiModelApplicationModelProvider > ( ) ) ;
90- services . AddModelConfigurationsAsServices ( partManager ) ;
91- }
92-
93- private static T GetService < T > ( this IServiceCollection services ) =>
94- ( T ) services . LastOrDefault ( d => d . ServiceType == typeof ( T ) ) ? . ImplementationInstance ! ;
95-
96- private static ApplicationPartManager GetOrCreateApplicationPartManager ( this IServiceCollection services )
97- {
98- var partManager = services . GetService < ApplicationPartManager > ( ) ;
9988
100- if ( partManager == null )
89+ if ( configured )
10190 {
102- partManager = new ApplicationPartManager ( ) ;
103- services . TryAddSingleton ( partManager ) ;
91+ services . AddModelConfigurationsAsServices ( partManager ) ;
10492 }
105-
106- partManager . ApplicationParts . Add ( new AssemblyPart ( typeof ( ODataApiVersioningOptions ) . Assembly ) ) ;
107- return partManager ;
10893 }
10994
11095 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
@@ -151,27 +136,6 @@ private static void TryReplaceODataService(
151136 }
152137 }
153138
154- private static void AddModelConfigurationsAsServices ( this IServiceCollection services , ApplicationPartManager partManager )
155- {
156- var feature = new ModelConfigurationFeature ( ) ;
157- var modelConfigurationType = typeof ( IModelConfiguration ) ;
158-
159- partManager . PopulateFeature ( feature ) ;
160-
161- foreach ( var modelConfiguration in feature . ModelConfigurations )
162- {
163- services . TryAddEnumerable ( Transient ( modelConfigurationType , modelConfiguration ) ) ;
164- }
165- }
166-
167- private static void ConfigureDefaultFeatureProviders ( ApplicationPartManager partManager )
168- {
169- if ( ! partManager . FeatureProviders . OfType < ModelConfigurationFeatureProvider > ( ) . Any ( ) )
170- {
171- partManager . FeatureProviders . Add ( new ModelConfigurationFeatureProvider ( ) ) ;
172- }
173- }
174-
175139 private static object CreateInstance ( this IServiceProvider services , ServiceDescriptor descriptor )
176140 {
177141 if ( descriptor . ImplementationInstance != null )
0 commit comments