diff --git a/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilderExtensions.cs b/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilderExtensions.cs index f5b178aa..15e89b34 100644 --- a/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilderExtensions.cs +++ b/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilderExtensions.cs @@ -155,6 +155,45 @@ public static IImageSharpBuilder AddProvider(this IImageSharpBuilder return builder; } + /// + /// Inserts the given at the give index into to the provider collection within the service collection. + /// + /// The type of class implementing to add. + /// The core builder. + /// The zero-based index at which the provider should be inserted. + /// The . + public static IImageSharpBuilder InsertProvider(this IImageSharpBuilder builder, int index) + where TProvider : class, IImageProvider + { + var descriptors = builder.Services.Where(x => x.ServiceType == typeof(IImageProvider)).ToList(); + builder.ClearProviders(); + + descriptors.Insert(index, ServiceDescriptor.Singleton()); + + builder.Services.TryAddEnumerable(descriptors); + return builder; + } + + /// + /// Inserts the given at the give index into the provider collection within the service collection. + /// + /// The type of class implementing to add. + /// The core builder. + /// The zero-based index at which the provider should be inserted. + /// The factory method for returning a . + /// The . + public static IImageSharpBuilder InsertProvider(this IImageSharpBuilder builder, int index, Func implementationFactory) + where TProvider : class, IImageProvider + { + var descriptors = builder.Services.Where(x => x.ServiceType == typeof(IImageProvider)).ToList(); + builder.ClearProviders(); + + descriptors.Insert(index, ServiceDescriptor.Singleton(implementationFactory)); + + builder.Services.TryAddEnumerable(descriptors); + return builder; + } + /// /// Removes the given from the provider collection within the service collection. /// @@ -166,7 +205,6 @@ public static IImageSharpBuilder RemoveProvider(this IImageSharpBuild { ServiceDescriptor descriptor = builder.Services.FirstOrDefault(x => x.ServiceType == typeof(IImageProvider) - && x.Lifetime == ServiceLifetime.Singleton && (x.ImplementationType == typeof(TProvider) || (x.ImplementationFactory?.GetMethodInfo().ReturnType == typeof(TProvider)))); diff --git a/tests/ImageSharp.Web.Tests/DependencyInjection/ServiceRegistrationExtensionsTests.cs b/tests/ImageSharp.Web.Tests/DependencyInjection/ServiceRegistrationExtensionsTests.cs index 33dcaf6a..32bf429d 100644 --- a/tests/ImageSharp.Web.Tests/DependencyInjection/ServiceRegistrationExtensionsTests.cs +++ b/tests/ImageSharp.Web.Tests/DependencyInjection/ServiceRegistrationExtensionsTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System.Collections.Generic; +using System.Linq; using System.Reflection; using Microsoft.Extensions.DependencyInjection; using SixLabors.ImageSharp.Web.Caching; @@ -40,6 +42,8 @@ private static bool IsServiceImplementationFactory(Se (descriptor.ImplementationFactory?.GetMethodInfo().ReturnType == typeof(TImplementation) || descriptor.ImplementationFactory?.Invoke(null)?.GetType() == typeof(TImplementation)); // OK to invoke the factory in tests + private static IReadOnlyList GetCollection(IServiceCollection serviceDescriptors) => serviceDescriptors.Where(x => x.ServiceType == typeof(T)).ToList(); + [Fact] public void DefaultServicesAreRegistered() { @@ -174,6 +178,24 @@ public void CanAddRemoveImageProviders() Assert.DoesNotContain(services, IsService); } + [Fact] + public void CanInsertRemoveImageProviders() + { + var services = new ServiceCollection(); + IImageSharpBuilder builder = services.AddImageSharp(); + + builder.InsertProvider(0); + Assert.Single(services, IsService); + Assert.Single(services, IsServiceImplementationType); + + IReadOnlyList providers = GetCollection(services); + Assert.Equal(2, providers.Count); + Assert.True(IsService(providers[0])); + + builder.RemoveProvider(); + Assert.DoesNotContain(services, IsService); + } + [Fact] public void CanAddRemoveFactoryImageProviders() { @@ -188,6 +210,25 @@ public void CanAddRemoveFactoryImageProviders() Assert.DoesNotContain(services, IsService); } + [Fact] + public void CanInsertRemoveFactoryImageProviders() + { + var services = new ServiceCollection(); + IImageSharpBuilder builder = services.AddImageSharp(); + + builder.InsertProvider(0, _ => new MockImageProvider()); + Assert.Single(services, IsService); + Assert.Single(services, IsServiceImplementationFactory); + + IReadOnlyList providers = GetCollection(services); + Assert.Equal(2, providers.Count); + Assert.True(IsService(providers[0])); + Assert.True(IsServiceImplementationFactory(providers[0])); + + builder.RemoveProvider(); + Assert.DoesNotContain(services, IsService); + } + [Fact] public void CanAddRemoveAllImageProviders() {