From 27968f7628e0b76ea476c9e8f0558ec220c2192b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Wed, 24 Jul 2024 11:15:48 +0200 Subject: [PATCH] Use a TryAddEnumerable() call per service registration to avoid unnecessary allocations --- .../app.manifest | 3 ++- .../OpenIddictClientAspNetCoreExtensions.cs | 19 +++++++++++-------- ...penIddictClientDataProtectionExtensions.cs | 10 +++++----- .../OpenIddictClientOwinExtensions.cs | 10 +++++----- ...IddictClientSystemIntegrationExtensions.cs | 17 +++++++++-------- ...OpenIddictClientSystemNetHttpExtensions.cs | 14 ++++++++------ ...penIddictClientWebIntegrationExtensions.cs | 10 +++++----- .../OpenIddictQuartzExtensions.cs | 10 +++++----- .../OpenIddictServerAspNetCoreExtensions.cs | 16 +++++++++------- ...penIddictServerDataProtectionExtensions.cs | 10 +++++----- .../OpenIddictServerOwinExtensions.cs | 10 +++++----- ...penIddictValidationAspNetCoreExtensions.cs | 13 +++++++------ ...ddictValidationDataProtectionExtensions.cs | 10 +++++----- .../OpenIddictValidationOwinExtensions.cs | 6 ++---- ...ctValidationServerIntegrationExtensions.cs | 10 +++++----- ...IddictValidationSystemNetHttpExtensions.cs | 14 ++++++++------ 16 files changed, 96 insertions(+), 86 deletions(-) diff --git a/sandbox/OpenIddict.Sandbox.WinForms.Client/app.manifest b/sandbox/OpenIddict.Sandbox.WinForms.Client/app.manifest index 7936d941..b581b3b3 100644 --- a/sandbox/OpenIddict.Sandbox.WinForms.Client/app.manifest +++ b/sandbox/OpenIddict.Sandbox.WinForms.Client/app.manifest @@ -1,6 +1,7 @@  - + + diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs index 5f6b5370..83b3fa35 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs @@ -48,14 +48,17 @@ public static class OpenIddictClientAspNetCoreExtensions // Register the option initializer used by the OpenIddict ASP.NET Core client integration services. // Note: TryAddEnumerable() is used here to ensure the initializers are only registered once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictClientAspNetCoreConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictClientAspNetCoreConfiguration>(), - - ServiceDescriptor.Singleton, OpenIddictClientAspNetCoreConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictClientAspNetCoreConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientAspNetCoreConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictClientAspNetCoreConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientAspNetCoreConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictClientAspNetCoreConfiguration>()); return new OpenIddictClientAspNetCoreBuilder(builder.Services); } diff --git a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionExtensions.cs b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionExtensions.cs index e02df3c7..2caac624 100644 --- a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionExtensions.cs +++ b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionExtensions.cs @@ -40,11 +40,11 @@ public static class OpenIddictClientDataProtectionExtensions builder.Services.TryAddSingleton(); // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictClientDataProtectionConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictClientDataProtectionConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientDataProtectionConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictClientDataProtectionConfiguration>()); return new OpenIddictClientDataProtectionBuilder(builder.Services); } diff --git a/src/OpenIddict.Client.Owin/OpenIddictClientOwinExtensions.cs b/src/OpenIddict.Client.Owin/OpenIddictClientOwinExtensions.cs index 3e6f5e2e..d864568b 100644 --- a/src/OpenIddict.Client.Owin/OpenIddictClientOwinExtensions.cs +++ b/src/OpenIddict.Client.Owin/OpenIddictClientOwinExtensions.cs @@ -49,11 +49,11 @@ public static class OpenIddictClientOwinExtensions // Register the option initializer used by the OpenIddict OWIN client integration services. // Note: TryAddEnumerable() is used here to ensure the initializers are only registered once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictClientOwinConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictClientOwinConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientOwinConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictClientOwinConfiguration>()); return new OpenIddictClientOwinBuilder(builder.Services); } diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationExtensions.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationExtensions.cs index 63d27902..b076e380 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationExtensions.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationExtensions.cs @@ -121,16 +121,17 @@ public static class OpenIddictClientSystemIntegrationExtensions // Register the option initializer and the background service used by the OpenIddict client system integration services. // Note: TryAddEnumerable() is used here to ensure the initializers and the background service are only registered once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton(), - ServiceDescriptor.Singleton(), + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); - ServiceDescriptor.Singleton, OpenIddictClientSystemIntegrationConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictClientSystemIntegrationConfiguration>(), + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientSystemIntegrationConfiguration>()); - ServiceDescriptor.Singleton, OpenIddictClientSystemIntegrationConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictClientSystemIntegrationConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictClientSystemIntegrationConfiguration>()); return new OpenIddictClientSystemIntegrationBuilder(builder.Services); } diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpExtensions.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpExtensions.cs index ca953fd9..617b3057 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpExtensions.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpExtensions.cs @@ -43,12 +43,14 @@ public static class OpenIddictClientSystemNetHttpExtensions builder.Services.TryAddSingleton(); // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictClientSystemNetHttpConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictClientSystemNetHttpConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictClientSystemNetHttpConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientSystemNetHttpConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientSystemNetHttpConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictClientSystemNetHttpConfiguration>()); return new OpenIddictClientSystemNetHttpBuilder(builder.Services); } diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs index 783d54e2..b062de02 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs @@ -39,11 +39,11 @@ public static partial class OpenIddictClientWebIntegrationExtensions .Select(descriptor => descriptor.ServiceDescriptor)); // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictClientWebIntegrationConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictClientWebIntegrationConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientWebIntegrationConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictClientWebIntegrationConfiguration>()); // Note: the IPostConfigureOptions service responsible for populating // the client registrations MUST be registered before OpenIddictClientConfiguration to ensure diff --git a/src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs b/src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs index c42e96ab..ea0918ee 100644 --- a/src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs +++ b/src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs @@ -35,11 +35,11 @@ public static class OpenIddictQuartzExtensions builder.Services.TryAddTransient(); // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictQuartzConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictQuartzConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictQuartzConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictQuartzConfiguration>()); return new OpenIddictQuartzBuilder(builder.Services); } diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreExtensions.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreExtensions.cs index 8087137a..a95424ce 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreExtensions.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreExtensions.cs @@ -52,15 +52,17 @@ public static class OpenIddictServerAspNetCoreExtensions // Register the option initializer used by the OpenIddict ASP.NET Core server integration services. // Note: TryAddEnumerable() is used here to ensure the initializers are only registered once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictServerAspNetCoreConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictServerAspNetCoreConfiguration>(), + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictServerAspNetCoreConfiguration>()); - ServiceDescriptor.Singleton, OpenIddictServerAspNetCoreConfiguration>(), + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictServerAspNetCoreConfiguration>()); - ServiceDescriptor.Singleton, OpenIddictServerAspNetCoreConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictServerAspNetCoreConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictServerAspNetCoreConfiguration>()); return new OpenIddictServerAspNetCoreBuilder(builder.Services); } diff --git a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionExtensions.cs b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionExtensions.cs index 6c810566..1825deae 100644 --- a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionExtensions.cs +++ b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionExtensions.cs @@ -40,11 +40,11 @@ public static class OpenIddictServerDataProtectionExtensions builder.Services.TryAddSingleton(); // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictServerDataProtectionConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictServerDataProtectionConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictServerDataProtectionConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictServerDataProtectionConfiguration>()); return new OpenIddictServerDataProtectionBuilder(builder.Services); } diff --git a/src/OpenIddict.Server.Owin/OpenIddictServerOwinExtensions.cs b/src/OpenIddict.Server.Owin/OpenIddictServerOwinExtensions.cs index 226046fa..3e553a6d 100644 --- a/src/OpenIddict.Server.Owin/OpenIddictServerOwinExtensions.cs +++ b/src/OpenIddict.Server.Owin/OpenIddictServerOwinExtensions.cs @@ -54,11 +54,11 @@ public static class OpenIddictServerOwinExtensions // Register the option initializers used by the OpenIddict OWIN server integration services. // Note: TryAddEnumerable() is used here to ensure the initializers are only registered once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictServerOwinConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictServerOwinConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictServerOwinConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictServerOwinConfiguration>()); return new OpenIddictServerOwinBuilder(builder.Services); } diff --git a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreExtensions.cs b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreExtensions.cs index 343206f0..5935d3c6 100644 --- a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreExtensions.cs +++ b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreExtensions.cs @@ -45,13 +45,14 @@ public static class OpenIddictValidationAspNetCoreExtensions // Register the option initializer used by the OpenIddict ASP.NET Core validation integration services. // Note: TryAddEnumerable() is used here to ensure the initializers are only registered once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictValidationAspNetCoreConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictValidationAspNetCoreConfiguration>(), + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictValidationAspNetCoreConfiguration>()); - ServiceDescriptor.Singleton, OpenIddictValidationAspNetCoreConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictValidationAspNetCoreConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictValidationAspNetCoreConfiguration>()); return new OpenIddictValidationAspNetCoreBuilder(builder.Services); } diff --git a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionExtensions.cs b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionExtensions.cs index 50fb26a2..3f6cd870 100644 --- a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionExtensions.cs +++ b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionExtensions.cs @@ -36,11 +36,11 @@ public static class OpenIddictValidationDataProtectionExtensions builder.Services.TryAdd(OpenIddictValidationDataProtectionHandlers.DefaultHandlers.Select(descriptor => descriptor.ServiceDescriptor)); // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictValidationDataProtectionConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictValidationDataProtectionConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictValidationDataProtectionConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictValidationDataProtectionConfiguration>()); return new OpenIddictValidationDataProtectionBuilder(builder.Services); } diff --git a/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinExtensions.cs b/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinExtensions.cs index e89cd440..54ed03b1 100644 --- a/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinExtensions.cs +++ b/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinExtensions.cs @@ -46,10 +46,8 @@ public static class OpenIddictValidationOwinExtensions // Register the option initializers used by the OpenIddict OWIN validation integration services. // Note: TryAddEnumerable() is used here to ensure the initializers are only registered once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictValidationOwinConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictValidationOwinConfiguration>()); return new OpenIddictValidationOwinBuilder(builder.Services); } diff --git a/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs b/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs index 6065b4c6..99bee368 100644 --- a/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs +++ b/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs @@ -31,11 +31,11 @@ public static class OpenIddictValidationServerIntegrationExtensions } // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictValidationServerIntegrationConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictValidationServerIntegrationConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictValidationServerIntegrationConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictValidationServerIntegrationConfiguration>()); return new OpenIddictValidationServerIntegrationBuilder(builder.Services); } diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpExtensions.cs b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpExtensions.cs index cc1e2411..9a943d7f 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpExtensions.cs +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpExtensions.cs @@ -43,12 +43,14 @@ public static class OpenIddictValidationSystemNetHttpExtensions builder.Services.TryAddSingleton(); // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. - builder.Services.TryAddEnumerable( - [ - ServiceDescriptor.Singleton, OpenIddictValidationSystemNetHttpConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictValidationSystemNetHttpConfiguration>(), - ServiceDescriptor.Singleton, OpenIddictValidationSystemNetHttpConfiguration>() - ]); + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictValidationSystemNetHttpConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IConfigureOptions, OpenIddictValidationSystemNetHttpConfiguration>()); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< + IPostConfigureOptions, OpenIddictValidationSystemNetHttpConfiguration>()); return new OpenIddictValidationSystemNetHttpBuilder(builder.Services); }