33
44using System ;
55using System . Linq ;
6- using System . Reflection ;
76using Microsoft . Extensions . Configuration ;
87using Microsoft . Extensions . DependencyInjection ;
98using Microsoft . Extensions . DependencyInjection . Extensions ;
@@ -166,11 +165,12 @@ public static IImageSharpBuilder InsertProvider<TProvider>(this IImageSharpBuild
166165 where TProvider : class , IImageProvider
167166 {
168167 var descriptors = builder . Services . Where ( x => x . ServiceType == typeof ( IImageProvider ) ) . ToList ( ) ;
169- builder . ClearProviders ( ) ;
170-
168+ descriptors . RemoveAll ( x => x . GetImplementationType ( ) == typeof ( TProvider ) ) ;
171169 descriptors . Insert ( index , ServiceDescriptor . Singleton < IImageProvider , TProvider > ( ) ) ;
172170
171+ builder . ClearProviders ( ) ;
173172 builder . Services . TryAddEnumerable ( descriptors ) ;
173+
174174 return builder ;
175175 }
176176
@@ -186,11 +186,12 @@ public static IImageSharpBuilder InsertProvider<TProvider>(this IImageSharpBuild
186186 where TProvider : class , IImageProvider
187187 {
188188 var descriptors = builder . Services . Where ( x => x . ServiceType == typeof ( IImageProvider ) ) . ToList ( ) ;
189- builder . ClearProviders ( ) ;
190-
189+ descriptors . RemoveAll ( x => x . GetImplementationType ( ) == typeof ( TProvider ) ) ;
191190 descriptors . Insert ( index , ServiceDescriptor . Singleton < IImageProvider > ( implementationFactory ) ) ;
192191
192+ builder . ClearProviders ( ) ;
193193 builder . Services . TryAddEnumerable ( descriptors ) ;
194+
194195 return builder ;
195196 }
196197
@@ -203,11 +204,7 @@ public static IImageSharpBuilder InsertProvider<TProvider>(this IImageSharpBuild
203204 public static IImageSharpBuilder RemoveProvider < TProvider > ( this IImageSharpBuilder builder )
204205 where TProvider : class , IImageProvider
205206 {
206- ServiceDescriptor descriptor = builder . Services . FirstOrDefault ( x =>
207- x . ServiceType == typeof ( IImageProvider )
208- && ( x . ImplementationType == typeof ( TProvider )
209- || ( x . ImplementationFactory ? . GetMethodInfo ( ) . ReturnType == typeof ( TProvider ) ) ) ) ;
210-
207+ ServiceDescriptor descriptor = builder . Services . FirstOrDefault ( x => x . ServiceType == typeof ( IImageProvider ) && x . GetImplementationType ( ) == typeof ( TProvider ) ) ;
211208 if ( descriptor != null )
212209 {
213210 builder . Services . Remove ( descriptor ) ;
@@ -264,12 +261,7 @@ public static IImageSharpBuilder AddProcessor<TProcessor>(this IImageSharpBuilde
264261 public static IImageSharpBuilder RemoveProcessor < TProcessor > ( this IImageSharpBuilder builder )
265262 where TProcessor : class , IImageWebProcessor
266263 {
267- ServiceDescriptor descriptor = builder . Services . FirstOrDefault ( x =>
268- x . ServiceType == typeof ( IImageWebProcessor )
269- && x . Lifetime == ServiceLifetime . Singleton
270- && ( x . ImplementationType == typeof ( TProcessor )
271- || ( x . ImplementationFactory ? . GetMethodInfo ( ) . ReturnType == typeof ( TProcessor ) ) ) ) ;
272-
264+ ServiceDescriptor descriptor = builder . Services . FirstOrDefault ( x => x . ServiceType == typeof ( IImageWebProcessor ) && x . GetImplementationType ( ) == typeof ( TProcessor ) ) ;
273265 if ( descriptor != null )
274266 {
275267 builder . Services . Remove ( descriptor ) ;
@@ -326,12 +318,7 @@ public static IImageSharpBuilder AddConverter<TConverter>(this IImageSharpBuilde
326318 public static IImageSharpBuilder RemoveConverter < TConverter > ( this IImageSharpBuilder builder )
327319 where TConverter : class , ICommandConverter
328320 {
329- ServiceDescriptor descriptor = builder . Services . FirstOrDefault ( x =>
330- x . ServiceType == typeof ( ICommandConverter )
331- && x . Lifetime == ServiceLifetime . Singleton
332- && ( x . ImplementationType == typeof ( TConverter )
333- || ( x . ImplementationFactory ? . GetMethodInfo ( ) . ReturnType == typeof ( TConverter ) ) ) ) ;
334-
321+ ServiceDescriptor descriptor = builder . Services . FirstOrDefault ( x => x . ServiceType == typeof ( ICommandConverter ) && x . GetImplementationType ( ) == typeof ( TConverter ) ) ;
335322 if ( descriptor != null )
336323 {
337324 builder . Services . Remove ( descriptor ) ;
@@ -379,5 +366,10 @@ public static IImageSharpBuilder Configure<TOptions>(this IImageSharpBuilder bui
379366 builder . Services . Configure ( configureOptions ) ;
380367 return builder ;
381368 }
369+
370+ private static Type GetImplementationType ( this ServiceDescriptor descriptor )
371+ => descriptor . ImplementationType
372+ ?? descriptor . ImplementationInstance ? . GetType ( )
373+ ?? descriptor . ImplementationFactory ? . GetType ( ) . GenericTypeArguments [ 1 ] ;
382374 }
383375}
0 commit comments