From f1beaec400c2b8393b99fe534fa466b3bcba153d Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Sat, 2 Apr 2022 16:00:38 +0200 Subject: [PATCH 1/6] Remove no-op ImageSharpConfiguration and setup action --- .../ImageSharpConfiguration.cs | 19 -------------- .../ServiceCollectionExtensions.cs | 26 +++++++++---------- 2 files changed, 12 insertions(+), 33 deletions(-) delete mode 100644 src/ImageSharp.Web/DependencyInjection/ImageSharpConfiguration.cs diff --git a/src/ImageSharp.Web/DependencyInjection/ImageSharpConfiguration.cs b/src/ImageSharp.Web/DependencyInjection/ImageSharpConfiguration.cs deleted file mode 100644 index 185a8c99..00000000 --- a/src/ImageSharp.Web/DependencyInjection/ImageSharpConfiguration.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using Microsoft.Extensions.Options; -using SixLabors.ImageSharp.Web.Middleware; - -namespace SixLabors.ImageSharp.Web.DependencyInjection -{ - /// - /// Provides default configuration settings to be consumed by the middleware. - /// - public class ImageSharpConfiguration : IConfigureOptions - { - /// - public void Configure(ImageSharpMiddlewareOptions options) - { - } - } -} diff --git a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs index afa8950a..71f05c54 100644 --- a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs @@ -3,8 +3,6 @@ using System; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Options; using SixLabors.ImageSharp.Web.Caching; using SixLabors.ImageSharp.Web.Commands; using SixLabors.ImageSharp.Web.Commands.Converters; @@ -26,7 +24,15 @@ public static class ServiceCollectionExtensions /// The to add services to. /// An that can be used to further configure the ImageSharp services. public static IImageSharpBuilder AddImageSharp(this IServiceCollection services) - => AddImageSharp(services, _ => { }); + { + Guard.NotNull(services, nameof(services)); + + IImageSharpBuilder builder = new ImageSharpBuilder(services); + + AddDefaultServices(builder); + + return builder; + } /// /// Adds ImageSharp services to the specified with the given options. @@ -41,21 +47,13 @@ public static IImageSharpBuilder AddImageSharp( Guard.NotNull(services, nameof(services)); Guard.NotNull(setupAction, nameof(setupAction)); - services.TryAddTransient, ImageSharpConfiguration>(); - - IImageSharpBuilder builder = new ImageSharpBuilder(services); - - AddDefaultServices(builder, setupAction); + services.Configure(setupAction); - return builder; + return AddImageSharp(services); } - private static void AddDefaultServices( - IImageSharpBuilder builder, - Action setupAction) + private static void AddDefaultServices(IImageSharpBuilder builder) { - builder.Services.Configure(setupAction); - builder.Services.AddSingleton(); builder.Services.AddSingleton>(); From f1b8e91d0152c2202a9a4e7933f277eae8d45821 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Sun, 3 Apr 2022 00:00:09 +0200 Subject: [PATCH 2/6] Separate required and default services --- samples/ImageSharp.Web.Sample/Startup.cs | 93 ++++++------------- .../DependencyInjection/ImageSharpBuilder.cs | 3 +- .../ServiceCollectionExtensions.cs | 50 ++++------ 3 files changed, 48 insertions(+), 98 deletions(-) diff --git a/samples/ImageSharp.Web.Sample/Startup.cs b/samples/ImageSharp.Web.Sample/Startup.cs index 02db25df..018ce82b 100644 --- a/samples/ImageSharp.Web.Sample/Startup.cs +++ b/samples/ImageSharp.Web.Sample/Startup.cs @@ -37,71 +37,32 @@ public class Startup /// /// The collection of service desscriptors. public void ConfigureServices(IServiceCollection services) - { - services.AddImageSharp() - .SetRequestParser() - .Configure(options => - { - options.CacheRootPath = null; - options.CacheFolder = "is-cache"; - options.CacheFolderDepth = 8; - }) - .SetCache() - .SetCacheKey() - .SetCacheHash() - .Configure(options => - { - options.ProviderRootPath = null; - }) - .AddProvider() - .AddProcessor() - .AddProcessor() - .AddProcessor() - .AddProcessor(); - - // Add the default service and options. - // - // services.AddImageSharp(); - - // Or add the default service and custom options. - // - // this.ConfigureDefaultServicesAndCustomOptions(services); - - // Or we can fine-grain control adding the default options and configure all other services. - // - // this.ConfigureCustomServicesAndDefaultOptions(services); - - // Or we can fine-grain control adding custom options and configure all other services - // There are also factory methods for each builder that will allow building from configuration files. - // - // this.ConfigureCustomServicesAndCustomOptions(services); - } + => services.AddImageSharp(); // Add the default service and options + // Or add the default service and custom options private void ConfigureDefaultServicesAndCustomOptions(IServiceCollection services) - { - services.AddImageSharp(options => + => services.AddImageSharp(options => { options.Configuration = Configuration.Default; options.BrowserMaxAge = TimeSpan.FromDays(7); options.CacheMaxAge = TimeSpan.FromDays(365); - options.CacheHashLength = 8; + options.CacheHashLength = 12; options.OnParseCommandsAsync = _ => Task.CompletedTask; options.OnBeforeSaveAsync = _ => Task.CompletedTask; options.OnProcessedAsync = _ => Task.CompletedTask; options.OnPrepareResponseAsync = _ => Task.CompletedTask; }); - } + // Or we can fine-grain control adding the default options and configure all other services private void ConfigureCustomServicesAndDefaultOptions(IServiceCollection services) - { - services.AddImageSharp() - .RemoveProcessor() - .RemoveProcessor(); - } + => services.AddImageSharp() + .RemoveProcessor() + .RemoveProcessor(); + // Or we can fine-grain control adding custom options and configure all other services + // There are also factory methods for each builder that will allow building from configuration files private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services) - { - services.AddImageSharp(options => + => services.AddImageSharp(options => { options.Configuration = Configuration.Default; options.BrowserMaxAge = TimeSpan.FromDays(7); @@ -112,22 +73,22 @@ private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services options.OnProcessedAsync = _ => Task.CompletedTask; options.OnPrepareResponseAsync = _ => Task.CompletedTask; }) - .SetRequestParser() - .Configure(options => - { - options.CacheFolder = "different-cache"; - }) - .SetCache() - .SetCacheKey() - .SetCacheHash() - .ClearProviders() - .AddProvider() - .ClearProcessors() - .AddProcessor() - .AddProcessor() - .AddProcessor() - .AddProcessor(); - } + .SetRequestParser() + .Configure(options => + { + options.CacheFolder = "different-cache"; + options.CacheFolderDepth = 6; + }) + .SetCache() + .SetCacheKey() + .SetCacheHash() + .ClearProviders() + .AddProvider() + .ClearProcessors() + .AddProcessor() + .AddProcessor() + .AddProcessor() + .AddProcessor(); /// /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs b/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs index 6bebd32a..cb8fd822 100644 --- a/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs +++ b/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System; using Microsoft.Extensions.DependencyInjection; namespace SixLabors.ImageSharp.Web.DependencyInjection @@ -14,7 +15,7 @@ internal class ImageSharpBuilder : IImageSharpBuilder /// Initializes a new instance of the class. /// /// The to add services to. - public ImageSharpBuilder(IServiceCollection services) => this.Services = services; + public ImageSharpBuilder(IServiceCollection services) => this.Services = services ?? throw new ArgumentNullException(nameof(services)); /// public IServiceCollection Services { get; } diff --git a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs index 71f05c54..e8192007 100644 --- a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs @@ -24,38 +24,21 @@ public static class ServiceCollectionExtensions /// The to add services to. /// An that can be used to further configure the ImageSharp services. public static IImageSharpBuilder AddImageSharp(this IServiceCollection services) - { - Guard.NotNull(services, nameof(services)); - - IImageSharpBuilder builder = new ImageSharpBuilder(services); - - AddDefaultServices(builder); - - return builder; - } + => new ImageSharpBuilder(services).AddRequiredServices().AddDefaultServices(); /// /// Adds ImageSharp services to the specified with the given options. /// /// The to add services to. - /// An to configure the provided . + /// An to configure the provided . /// An that can be used to further configure the ImageSharp services. - public static IImageSharpBuilder AddImageSharp( - this IServiceCollection services, - Action setupAction) - { - Guard.NotNull(services, nameof(services)); - Guard.NotNull(setupAction, nameof(setupAction)); - - services.Configure(setupAction); + public static IImageSharpBuilder AddImageSharp(this IServiceCollection services, Action configureOptions) + => services.AddImageSharp().Configure(configureOptions); - return AddImageSharp(services); - } - - private static void AddDefaultServices(IImageSharpBuilder builder) + private static IImageSharpBuilder AddRequiredServices(this IImageSharpBuilder builder) { builder.Services.AddSingleton(); - + builder.Services.AddSingleton(); builder.Services.AddSingleton>(); builder.SetRequestParser(); @@ -66,13 +49,6 @@ private static void AddDefaultServices(IImageSharpBuilder builder) builder.SetCacheHash(); - builder.AddProvider(); - - builder.AddProcessor() - .AddProcessor() - .AddProcessor() - .AddProcessor(); - builder.AddConverter>(); builder.AddConverter>(); builder.AddConverter>(); @@ -119,7 +95,19 @@ private static void AddDefaultServices(IImageSharpBuilder builder) builder.AddConverter(); builder.AddConverter(); - builder.Services.AddSingleton(); + return builder; + } + + private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder builder) + { + builder.AddProvider(); + + builder.AddProcessor() + .AddProcessor() + .AddProcessor() + .AddProcessor(); + + return builder; } } } From 2a375e7a3229a13a00cc6cfc48d1795e44640f88 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Mon, 4 Apr 2022 23:51:32 +0200 Subject: [PATCH 3/6] Apply review comments/cleanup --- .../DependencyInjection/ImageSharpBuilder.cs | 3 +-- .../ServiceCollectionExtensions.cs | 23 ++++++++----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs b/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs index cb8fd822..6bebd32a 100644 --- a/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs +++ b/src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. -using System; using Microsoft.Extensions.DependencyInjection; namespace SixLabors.ImageSharp.Web.DependencyInjection @@ -15,7 +14,7 @@ internal class ImageSharpBuilder : IImageSharpBuilder /// Initializes a new instance of the class. /// /// The to add services to. - public ImageSharpBuilder(IServiceCollection services) => this.Services = services ?? throw new ArgumentNullException(nameof(services)); + public ImageSharpBuilder(IServiceCollection services) => this.Services = services; /// public IServiceCollection Services { get; } diff --git a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs index e8192007..41e09aca 100644 --- a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs @@ -24,7 +24,7 @@ public static class ServiceCollectionExtensions /// The to add services to. /// An that can be used to further configure the ImageSharp services. public static IImageSharpBuilder AddImageSharp(this IServiceCollection services) - => new ImageSharpBuilder(services).AddRequiredServices().AddDefaultServices(); + => new ImageSharpBuilder(services).AddDefaultServices(); /// /// Adds ImageSharp services to the specified with the given options. @@ -35,20 +35,15 @@ public static IImageSharpBuilder AddImageSharp(this IServiceCollection services) public static IImageSharpBuilder AddImageSharp(this IServiceCollection services, Action configureOptions) => services.AddImageSharp().Configure(configureOptions); - private static IImageSharpBuilder AddRequiredServices(this IImageSharpBuilder builder) + private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder builder) { builder.Services.AddSingleton(); - builder.Services.AddSingleton(); builder.Services.AddSingleton>(); + // Command parsing + builder.Services.AddSingleton(); builder.SetRequestParser(); - builder.SetCache(); - - builder.SetCacheKey(); - - builder.SetCacheHash(); - builder.AddConverter>(); builder.AddConverter>(); builder.AddConverter>(); @@ -95,13 +90,15 @@ private static IImageSharpBuilder AddRequiredServices(this IImageSharpBuilder bu builder.AddConverter(); builder.AddConverter(); - return builder; - } + // Cache + builder.SetCache(); + builder.SetCacheKey(); + builder.SetCacheHash(); - private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder builder) - { + // Providers builder.AddProvider(); + // Processors builder.AddProcessor() .AddProcessor() .AddProcessor() From fbb6cef1eb6affc8ad470aea252c8f459e886536 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Wed, 6 Apr 2022 11:16:33 +0200 Subject: [PATCH 4/6] Explicitly call AddOptions --- .../DependencyInjection/ServiceCollectionExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs index 41e09aca..4d7fc1cb 100644 --- a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs @@ -37,6 +37,7 @@ public static IImageSharpBuilder AddImageSharp(this IServiceCollection services, private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder builder) { + builder.Services.AddOptions(); builder.Services.AddSingleton(); builder.Services.AddSingleton>(); From 42e3c7389a84c99bd85b39628edc6e93ee85fe7c Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Wed, 6 Apr 2022 11:27:02 +0200 Subject: [PATCH 5/6] Document all default configurations --- samples/ImageSharp.Web.Sample/Startup.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/samples/ImageSharp.Web.Sample/Startup.cs b/samples/ImageSharp.Web.Sample/Startup.cs index 018ce82b..af42c833 100644 --- a/samples/ImageSharp.Web.Sample/Startup.cs +++ b/samples/ImageSharp.Web.Sample/Startup.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.IO; using SixLabors.ImageSharp.Web.Caching; using SixLabors.ImageSharp.Web.Commands; using SixLabors.ImageSharp.Web.DependencyInjection; @@ -37,13 +38,15 @@ public class Startup /// /// The collection of service desscriptors. public void ConfigureServices(IServiceCollection services) - => services.AddImageSharp(); // Add the default service and options + => services.AddImageSharp(); // Add the default services and options - // Or add the default service and custom options + // Or add the default services and custom options private void ConfigureDefaultServicesAndCustomOptions(IServiceCollection services) => services.AddImageSharp(options => { options.Configuration = Configuration.Default; + options.MemoryStreamManager = new RecyclableMemoryStreamManager(); + options.UseInvariantParsingCulture = true; options.BrowserMaxAge = TimeSpan.FromDays(7); options.CacheMaxAge = TimeSpan.FromDays(365); options.CacheHashLength = 12; @@ -65,25 +68,32 @@ private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services => services.AddImageSharp(options => { options.Configuration = Configuration.Default; + options.MemoryStreamManager = new RecyclableMemoryStreamManager(); + options.UseInvariantParsingCulture = true; options.BrowserMaxAge = TimeSpan.FromDays(7); options.CacheMaxAge = TimeSpan.FromDays(365); - options.CacheHashLength = 8; + options.CacheHashLength = 12; options.OnParseCommandsAsync = _ => Task.CompletedTask; options.OnBeforeSaveAsync = _ => Task.CompletedTask; options.OnProcessedAsync = _ => Task.CompletedTask; options.OnPrepareResponseAsync = _ => Task.CompletedTask; }) .SetRequestParser() + .SetCache() .Configure(options => { - options.CacheFolder = "different-cache"; - options.CacheFolderDepth = 6; + options.CacheFolder = "is-cache"; + options.CacheFolderDepth = 8; }) - .SetCache() .SetCacheKey() .SetCacheHash() .ClearProviders() .AddProvider() + .Configure(options => + { + options.ProviderRootPath = null; + options.ProcessingBehavior = ProcessingBehavior.CommandOnly; + }) .ClearProcessors() .AddProcessor() .AddProcessor() From 2d5e0d425f1710722fa1f22062db4f00f799e41f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 22 Apr 2022 21:55:39 +1000 Subject: [PATCH 6/6] Revert unnecessary changes --- samples/ImageSharp.Web.Sample/Startup.cs | 105 +++++++++++------- .../ServiceCollectionExtensions.cs | 60 ++++++---- 2 files changed, 104 insertions(+), 61 deletions(-) diff --git a/samples/ImageSharp.Web.Sample/Startup.cs b/samples/ImageSharp.Web.Sample/Startup.cs index af42c833..02db25df 100644 --- a/samples/ImageSharp.Web.Sample/Startup.cs +++ b/samples/ImageSharp.Web.Sample/Startup.cs @@ -8,7 +8,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.IO; using SixLabors.ImageSharp.Web.Caching; using SixLabors.ImageSharp.Web.Commands; using SixLabors.ImageSharp.Web.DependencyInjection; @@ -38,67 +37,97 @@ public class Startup /// /// The collection of service desscriptors. public void ConfigureServices(IServiceCollection services) - => services.AddImageSharp(); // Add the default services and options + { + services.AddImageSharp() + .SetRequestParser() + .Configure(options => + { + options.CacheRootPath = null; + options.CacheFolder = "is-cache"; + options.CacheFolderDepth = 8; + }) + .SetCache() + .SetCacheKey() + .SetCacheHash() + .Configure(options => + { + options.ProviderRootPath = null; + }) + .AddProvider() + .AddProcessor() + .AddProcessor() + .AddProcessor() + .AddProcessor(); + + // Add the default service and options. + // + // services.AddImageSharp(); + + // Or add the default service and custom options. + // + // this.ConfigureDefaultServicesAndCustomOptions(services); + + // Or we can fine-grain control adding the default options and configure all other services. + // + // this.ConfigureCustomServicesAndDefaultOptions(services); + + // Or we can fine-grain control adding custom options and configure all other services + // There are also factory methods for each builder that will allow building from configuration files. + // + // this.ConfigureCustomServicesAndCustomOptions(services); + } - // Or add the default services and custom options private void ConfigureDefaultServicesAndCustomOptions(IServiceCollection services) - => services.AddImageSharp(options => + { + services.AddImageSharp(options => { options.Configuration = Configuration.Default; - options.MemoryStreamManager = new RecyclableMemoryStreamManager(); - options.UseInvariantParsingCulture = true; options.BrowserMaxAge = TimeSpan.FromDays(7); options.CacheMaxAge = TimeSpan.FromDays(365); - options.CacheHashLength = 12; + options.CacheHashLength = 8; options.OnParseCommandsAsync = _ => Task.CompletedTask; options.OnBeforeSaveAsync = _ => Task.CompletedTask; options.OnProcessedAsync = _ => Task.CompletedTask; options.OnPrepareResponseAsync = _ => Task.CompletedTask; }); + } - // Or we can fine-grain control adding the default options and configure all other services private void ConfigureCustomServicesAndDefaultOptions(IServiceCollection services) - => services.AddImageSharp() - .RemoveProcessor() - .RemoveProcessor(); + { + services.AddImageSharp() + .RemoveProcessor() + .RemoveProcessor(); + } - // Or we can fine-grain control adding custom options and configure all other services - // There are also factory methods for each builder that will allow building from configuration files private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services) - => services.AddImageSharp(options => + { + services.AddImageSharp(options => { options.Configuration = Configuration.Default; - options.MemoryStreamManager = new RecyclableMemoryStreamManager(); - options.UseInvariantParsingCulture = true; options.BrowserMaxAge = TimeSpan.FromDays(7); options.CacheMaxAge = TimeSpan.FromDays(365); - options.CacheHashLength = 12; + options.CacheHashLength = 8; options.OnParseCommandsAsync = _ => Task.CompletedTask; options.OnBeforeSaveAsync = _ => Task.CompletedTask; options.OnProcessedAsync = _ => Task.CompletedTask; options.OnPrepareResponseAsync = _ => Task.CompletedTask; }) - .SetRequestParser() - .SetCache() - .Configure(options => - { - options.CacheFolder = "is-cache"; - options.CacheFolderDepth = 8; - }) - .SetCacheKey() - .SetCacheHash() - .ClearProviders() - .AddProvider() - .Configure(options => - { - options.ProviderRootPath = null; - options.ProcessingBehavior = ProcessingBehavior.CommandOnly; - }) - .ClearProcessors() - .AddProcessor() - .AddProcessor() - .AddProcessor() - .AddProcessor(); + .SetRequestParser() + .Configure(options => + { + options.CacheFolder = "different-cache"; + }) + .SetCache() + .SetCacheKey() + .SetCacheHash() + .ClearProviders() + .AddProvider() + .ClearProcessors() + .AddProcessor() + .AddProcessor() + .AddProcessor() + .AddProcessor(); + } /// /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs index 4d7fc1cb..9d1c2488 100644 --- a/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs @@ -3,6 +3,8 @@ using System; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Options; using SixLabors.ImageSharp.Web.Caching; using SixLabors.ImageSharp.Web.Commands; using SixLabors.ImageSharp.Web.Commands.Converters; @@ -24,27 +26,53 @@ public static class ServiceCollectionExtensions /// The to add services to. /// An that can be used to further configure the ImageSharp services. public static IImageSharpBuilder AddImageSharp(this IServiceCollection services) - => new ImageSharpBuilder(services).AddDefaultServices(); + => AddImageSharp(services, _ => { }); /// /// Adds ImageSharp services to the specified with the given options. /// /// The to add services to. - /// An to configure the provided . + /// An to configure the provided . /// An that can be used to further configure the ImageSharp services. - public static IImageSharpBuilder AddImageSharp(this IServiceCollection services, Action configureOptions) - => services.AddImageSharp().Configure(configureOptions); + public static IImageSharpBuilder AddImageSharp( + this IServiceCollection services, + Action setupAction) + { + Guard.NotNull(services, nameof(services)); + Guard.NotNull(setupAction, nameof(setupAction)); + + IImageSharpBuilder builder = new ImageSharpBuilder(services); + + AddDefaultServices(builder, setupAction); - private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder builder) + return builder; + } + + private static void AddDefaultServices( + IImageSharpBuilder builder, + Action setupAction) { - builder.Services.AddOptions(); + builder.Services.Configure(setupAction); + builder.Services.AddSingleton(); + builder.Services.AddSingleton>(); - // Command parsing - builder.Services.AddSingleton(); builder.SetRequestParser(); + builder.SetCache(); + + builder.SetCacheKey(); + + builder.SetCacheHash(); + + builder.AddProvider(); + + builder.AddProcessor() + .AddProcessor() + .AddProcessor() + .AddProcessor(); + builder.AddConverter>(); builder.AddConverter>(); builder.AddConverter>(); @@ -91,21 +119,7 @@ private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder bui builder.AddConverter(); builder.AddConverter(); - // Cache - builder.SetCache(); - builder.SetCacheKey(); - builder.SetCacheHash(); - - // Providers - builder.AddProvider(); - - // Processors - builder.AddProcessor() - .AddProcessor() - .AddProcessor() - .AddProcessor(); - - return builder; + builder.Services.AddSingleton(); } } }