From 853eab8fc6d4246bd6b64c234d2db7b3cbda4b9e Mon Sep 17 00:00:00 2001 From: Ebbe Knudsen Date: Thu, 14 Nov 2024 17:02:56 +0100 Subject: [PATCH] Update http extensions --- .../DependencyInjection/ClientExtensions.cs | 22 +++++++---- ...ocessManagerServiceBusOptionsExtensions.cs | 38 +++++++++++++++++++ ....cs => ProcessManagerServiceBusOptions.cs} | 7 ++-- 3 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 source/ProcessManager.Client/Extensions/DependencyInjection/ProcessManagerServiceBusOptionsExtensions.cs rename source/ProcessManager.Client/Extensions/Options/{ProcessManagerServiceBusClientsOptions.cs => ProcessManagerServiceBusOptions.cs} (79%) diff --git a/source/ProcessManager.Client/Extensions/DependencyInjection/ClientExtensions.cs b/source/ProcessManager.Client/Extensions/DependencyInjection/ClientExtensions.cs index cc03666d38..6b678281ee 100644 --- a/source/ProcessManager.Client/Extensions/DependencyInjection/ClientExtensions.cs +++ b/source/ProcessManager.Client/Extensions/DependencyInjection/ClientExtensions.cs @@ -18,6 +18,7 @@ using Energinet.DataHub.ProcessManager.Client.Processes.BRS_026_028.V1; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Azure; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -59,20 +60,27 @@ public static IServiceCollection AddProcessManagerHttpClients(this IServiceColle } /// - /// Register Process Manager Service Bus clients for use in applications. + /// Register Process Manager RequestCalculatedData client for use in applications. /// The application must register the by calling /// - public static IServiceCollection AddProcessManagerServiceBusClients(this IServiceCollection services) + public static IServiceCollection AddProcessManagerRequestCalculatedDataClient(this IServiceCollection services, IConfiguration configuration) { + services.AddProcessManagerServiceBusOptions(); + services.AddAzureClients( builder => { builder.AddClient( - (_, _, provider) => - provider - .GetRequiredService() - .CreateSender(nameof(ProcessManagerServiceBusClientsOptions.ProcessManagerTopic))) - .WithName(nameof(ProcessManagerServiceBusClientsOptions.ProcessManagerTopic)); + (_, _, provider) => + { + var serviceBusOptions = provider.GetRequiredProcessManagerServiceBusOptions(); + var serviceBusSender = provider + .GetRequiredService() + .CreateSender(serviceBusOptions.ProcessManagerTopic); + + return serviceBusSender; + }) + .WithName(nameof(ProcessManagerServiceBusOptions.ProcessManagerTopic)); }); services.AddScoped(); diff --git a/source/ProcessManager.Client/Extensions/DependencyInjection/ProcessManagerServiceBusOptionsExtensions.cs b/source/ProcessManager.Client/Extensions/DependencyInjection/ProcessManagerServiceBusOptionsExtensions.cs new file mode 100644 index 0000000000..284fce64b9 --- /dev/null +++ b/source/ProcessManager.Client/Extensions/DependencyInjection/ProcessManagerServiceBusOptionsExtensions.cs @@ -0,0 +1,38 @@ +// Copyright 2020 Energinet DataHub A/S +// +// Licensed under the Apache License, Version 2.0 (the "License2"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Energinet.DataHub.ProcessManager.Client.Extensions.Options; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace Energinet.DataHub.ProcessManager.Client.Extensions.DependencyInjection; + +public static class ProcessManagerServiceBusOptionsExtensions +{ + public static void AddProcessManagerServiceBusOptions(this IServiceCollection services) + { + services + .AddOptions() + .BindConfiguration(ProcessManagerServiceBusOptions.SectionName) + .ValidateDataAnnotations() + .ValidateOnStart(); + } + + public static ProcessManagerServiceBusOptions GetRequiredProcessManagerServiceBusOptions(this IServiceProvider provider) + { + var options = provider.GetRequiredService>(); + return options.Value; + } +} diff --git a/source/ProcessManager.Client/Extensions/Options/ProcessManagerServiceBusClientsOptions.cs b/source/ProcessManager.Client/Extensions/Options/ProcessManagerServiceBusOptions.cs similarity index 79% rename from source/ProcessManager.Client/Extensions/Options/ProcessManagerServiceBusClientsOptions.cs rename to source/ProcessManager.Client/Extensions/Options/ProcessManagerServiceBusOptions.cs index bec3337562..4acfb500fa 100644 --- a/source/ProcessManager.Client/Extensions/Options/ProcessManagerServiceBusClientsOptions.cs +++ b/source/ProcessManager.Client/Extensions/Options/ProcessManagerServiceBusOptions.cs @@ -13,15 +13,16 @@ // limitations under the License. using System.ComponentModel.DataAnnotations; +using Microsoft.Extensions.Configuration; namespace Energinet.DataHub.ProcessManager.Client.Extensions.Options; /// -/// Options for the configuration of Process Manager Service Bus clients using the Process Manager. +/// Options for configuration of Process Manager Service Bus clients using the Process Manager. /// -public class ProcessManagerServiceBusClientsOptions +public class ProcessManagerServiceBusOptions { - public const string SectionName = "ProcessManagerServiceBusClients"; + public const string SectionName = "ProcessManagerServiceBus"; /// /// Name of the topic which the Process Manager receives service bus messages on