From 450d3aae18c29358706ffc1531780bcb7ef839f3 Mon Sep 17 00:00:00 2001 From: Mauro Servienti Date: Thu, 14 Dec 2023 19:04:10 +0100 Subject: [PATCH] Revert changes and remove the need for a configuration manager --- .../EndpointConfigurationManager.cs | 165 ------------------ src/Maddox.NServiceBus/LearningEndpoint.cs | 27 ++- .../LearningEndpointConfigurationManager.cs | 25 --- src/Maddox.NServiceBus/NServiceBusEndpoint.cs | 147 ++++++++++++++-- 4 files changed, 152 insertions(+), 212 deletions(-) delete mode 100644 src/Maddox.NServiceBus/EndpointConfigurationManager.cs delete mode 100644 src/Maddox.NServiceBus/LearningEndpointConfigurationManager.cs diff --git a/src/Maddox.NServiceBus/EndpointConfigurationManager.cs b/src/Maddox.NServiceBus/EndpointConfigurationManager.cs deleted file mode 100644 index bcf0bce..0000000 --- a/src/Maddox.NServiceBus/EndpointConfigurationManager.cs +++ /dev/null @@ -1,165 +0,0 @@ -using Microsoft.Extensions.Configuration; -using NServiceBus.Serialization; -using NServiceBus.Transport; - -namespace Maddox.NServiceBus; - -public abstract class EndpointConfigurationManager - where TTransport : TransportDefinition -{ - const string NServiceBusEndpointConfigurationSectionName = "NServiceBus:EndpointConfiguration"; - Action? transportCustomization; - Func? transportFactory; - bool _useDefaultSerializer = true; - Action>? _serializerCustomization; - EndpointConfiguration _endpointConfiguration = null!; - - static void ConfigureAuditing(EndpointConfiguration endpointConfiguration, IConfigurationSection? endpointConfigurationSection) - { - var auditSection = endpointConfigurationSection?.GetSection("Auditing"); - var enableAuditing = bool.Parse(auditSection?["Enabled"] ?? true.ToString()); - if (!enableAuditing) - { - return; - } - - var auditQueue = auditSection?["AuditQueue"] ?? "audit"; - endpointConfiguration.AuditProcessedMessagesTo(auditQueue); - } - - static void ConfigureRecoverability(EndpointConfiguration endpointConfiguration, IConfigurationSection? endpointConfigurationSection) - { - var recoverabilitySection = endpointConfigurationSection?.GetSection("Recoverability"); - - var errorQueue = recoverabilitySection?["ErrorQueue"] ?? "error"; - endpointConfiguration.SendFailedMessagesTo(errorQueue); - - var recoverabilityConfiguration = endpointConfiguration.Recoverability(); - - if (recoverabilitySection?.GetSection("Immediate") is { } immediateSection) - { - recoverabilityConfiguration.Immediate( - immediate => - { - if(immediateSection["NumberOfRetries"] is {} numberOfRetries) - { - immediate.NumberOfRetries(int.Parse(numberOfRetries)); - } - }); - } - - if(recoverabilitySection?.GetSection("Delayed") is { } delayedSection) - { - recoverabilityConfiguration.Delayed( - delayed => - { - if(delayedSection["NumberOfRetries"] is { } numberOfRetries) - { - delayed.NumberOfRetries(int.Parse(numberOfRetries)); - } - - if (delayedSection["TimeIncrease"] is {} timeIncrease) - {; - delayed.TimeIncrease(TimeSpan.Parse(timeIncrease)); - } - }); - } - } - - protected abstract TTransport CreateTransport(IConfigurationSection? transportConfigurationSection); - - protected static void ApplyCommonTransportSettings(IConfigurationSection? transportConfigurationSection, - TransportDefinition transport) - { - if (transportConfigurationSection?["TransportTransactionMode"] is { } transportTransactionMode) - { - Enum.TryParse(transportTransactionMode, ignoreCase: false, out TransportTransactionMode ttm); - transport.TransportTransactionMode = ttm; - } - } - - public void CustomizeTransport(Action transport) - { - this.transportCustomization = transport; - } - - public void OverrideTransport(Func factory) - { - this.transportFactory = factory; - } - - public SerializationExtensions ReplaceDefaultSerializer() where T : SerializationDefinition, new() - { - _useDefaultSerializer = false; - return _endpointConfiguration.UseSerialization(); - } - - public void CustomizeDefaultSerializer(Action>? serializerCustomization) - { - _serializerCustomization = serializerCustomization; - } - - void Customize(EndpointConfiguration endpointConfiguration, - IConfigurationSection? endpointConfigurationSection) - { - ConfigureAuditing(endpointConfiguration, endpointConfigurationSection); - ConfigureRecoverability(endpointConfiguration, endpointConfigurationSection); - - // create and configure the transport - var transport = transportFactory != null ? transportFactory(endpointConfigurationSection) : CreateTransport(endpointConfigurationSection?.GetSection("Transport")); - transportCustomization?.Invoke(transport); - endpointConfiguration.UseTransport(transport); - - // TODO create and configure the persistence - - if (_useDefaultSerializer) - { - var serializerConfiguration = endpointConfiguration.UseSerialization(); - _serializerCustomization?.Invoke(serializerConfiguration); - } - } - - static IConfigurationSection GetMandatoryEndpointConfigurationSection(IConfiguration configuration) - { - var endpointConfigurationSection = configuration.GetSection(NServiceBusEndpointConfigurationSectionName); - if (endpointConfigurationSection == null) - throw new Exception($"Cannot find the required '{NServiceBusEndpointConfigurationSectionName}' configuration section"); - - return endpointConfigurationSection; - } - - static string GetMandatoryEndpointName(IConfigurationSection endpointConfigurationSection) - { - return endpointConfigurationSection["EndpointName"] - ?? throw new ArgumentException( - "EndpointName cannot be null. Make sure the " + - $"{NServiceBusEndpointConfigurationSectionName}:EndpointName configuration value is set."); - } - - public EndpointConfiguration CreateEndpointConfiguration(string endpointName, IConfiguration? configuration) - { - if (string.IsNullOrWhiteSpace(endpointName)) - throw new ArgumentException("Endpoint name is required and cannot be empty", nameof(endpointName)); - - _endpointConfiguration = new EndpointConfiguration(endpointName); - - var endpointConfigurationSection = configuration?.GetSection(NServiceBusEndpointConfigurationSectionName); - Customize(_endpointConfiguration, endpointConfigurationSection); - - return _endpointConfiguration; - } - - public EndpointConfiguration CreateEndpointConfiguration(IConfiguration configuration) - { - if (configuration == null) throw new ArgumentNullException(nameof(configuration)); - - var endpointConfigurationSection = GetMandatoryEndpointConfigurationSection(configuration); - var endpointName = GetMandatoryEndpointName(endpointConfigurationSection); - - _endpointConfiguration = new EndpointConfiguration(endpointName); - - Customize(_endpointConfiguration, endpointConfigurationSection); - - return _endpointConfiguration; - } -} \ No newline at end of file diff --git a/src/Maddox.NServiceBus/LearningEndpoint.cs b/src/Maddox.NServiceBus/LearningEndpoint.cs index 90121cc..741c335 100644 --- a/src/Maddox.NServiceBus/LearningEndpoint.cs +++ b/src/Maddox.NServiceBus/LearningEndpoint.cs @@ -2,13 +2,34 @@ namespace Maddox.NServiceBus; -public class LearningEndpoint : NServiceBusEndpoint +public class LearningEndpoint : NServiceBusEndpoint { - public LearningEndpoint(IConfiguration configuration) : base(configuration) + public LearningEndpoint(IConfiguration configuration) + : base(configuration) { } - public LearningEndpoint(string endpointName, IConfiguration? configuration = null) : base(endpointName, configuration) + public LearningEndpoint(string endpointName, IConfiguration? configuration = null) + : base(endpointName, configuration) { } + + protected override LearningTransport CreateTransport(IConfigurationSection? transportConfigurationSection) + { + LearningTransport transport = new(); + + ApplyCommonTransportSettings(transportConfigurationSection, transport); + + if (transportConfigurationSection?["StorageDirectory"] is { } storageDirectory) + { + transport.StorageDirectory = storageDirectory; + } + + if (transportConfigurationSection?["RestrictPayloadSize"] is { } restrictPayloadSize) + { + transport.RestrictPayloadSize = bool.Parse(restrictPayloadSize); + } + + return transport; + } } \ No newline at end of file diff --git a/src/Maddox.NServiceBus/LearningEndpointConfigurationManager.cs b/src/Maddox.NServiceBus/LearningEndpointConfigurationManager.cs deleted file mode 100644 index 914d7bf..0000000 --- a/src/Maddox.NServiceBus/LearningEndpointConfigurationManager.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.Extensions.Configuration; - -namespace Maddox.NServiceBus; - -public class LearningEndpointConfigurationManager : EndpointConfigurationManager -{ - protected override LearningTransport CreateTransport(IConfigurationSection? transportConfigurationSection) - { - LearningTransport transport = new (); - - ApplyCommonTransportSettings(transportConfigurationSection, transport); - - if (transportConfigurationSection?["StorageDirectory"] is { } storageDirectory) - { - transport.StorageDirectory = storageDirectory; - } - - if (transportConfigurationSection?["RestrictPayloadSize"] is { } restrictPayloadSize) - { - transport.RestrictPayloadSize = bool.Parse(restrictPayloadSize); - } - - return transport; - } -} \ No newline at end of file diff --git a/src/Maddox.NServiceBus/NServiceBusEndpoint.cs b/src/Maddox.NServiceBus/NServiceBusEndpoint.cs index 0cba38a..a9f83c8 100644 --- a/src/Maddox.NServiceBus/NServiceBusEndpoint.cs +++ b/src/Maddox.NServiceBus/NServiceBusEndpoint.cs @@ -5,68 +5,177 @@ namespace Maddox.NServiceBus; -public abstract class NServiceBusEndpoint - where TTransport : TransportDefinition - where TEndpointConfigurationManager : EndpointConfigurationManager, new() +public abstract class NServiceBusEndpoint where TTransport : TransportDefinition { - readonly EndpointConfiguration _endpointConfiguration; - protected TEndpointConfigurationManager ConfigurationManager { get; } + const string NServiceBusEndpointConfigurationSectionName = "NServiceBus:EndpointConfiguration"; + readonly IConfiguration? _configuration; + protected EndpointConfiguration EndpointConfiguration{ get; } + protected IConfigurationSection? EndpointConfigurationSection { get; } + + Action>? _serializerCustomization; + bool _useDefaultSerializer = true; + + protected TTransport Transport { get; private set; } = null!; + Action? _transportCustomization; + Func? _transportFactory; protected NServiceBusEndpoint(IConfiguration configuration) + : this(GetEndpointNameFromConfigurationOrThrow(configuration), configuration) { - ConfigurationManager = new TEndpointConfigurationManager(); - _endpointConfiguration = ConfigurationManager.CreateEndpointConfiguration(configuration); + } protected NServiceBusEndpoint(string endpointName, IConfiguration? configuration = null) { if (endpointName == null) throw new ArgumentNullException(nameof(endpointName)); + + _configuration = configuration; + EndpointConfiguration = new EndpointConfiguration(endpointName); + EndpointConfigurationSection = configuration?.GetSection(NServiceBusEndpointConfigurationSectionName); + } + + protected abstract TTransport CreateTransport(IConfigurationSection? transportConfigurationSection); - ConfigurationManager = new TEndpointConfigurationManager(); - _endpointConfiguration = ConfigurationManager.CreateEndpointConfiguration(endpointName, configuration); + protected static void ApplyCommonTransportSettings(IConfigurationSection? transportConfigurationSection, + TransportDefinition transport) + { + if (transportConfigurationSection?["TransportTransactionMode"] is { } transportTransactionMode) + { + Enum.TryParse(transportTransactionMode, ignoreCase: false, out TransportTransactionMode transportTransportTransactionMode); + transport.TransportTransactionMode = transportTransportTransactionMode; + } } - public static implicit operator EndpointConfiguration(NServiceBusEndpoint endpoint) + static void ConfigureAuditing(EndpointConfiguration endpointConfiguration, IConfigurationSection? endpointConfigurationSection) + { + var auditSection = endpointConfigurationSection?.GetSection("Auditing"); + var enableAuditing = bool.Parse(auditSection?["Enabled"] ?? true.ToString()); + if (!enableAuditing) + { + return; + } + + var auditQueue = auditSection?["AuditQueue"] ?? "audit"; + endpointConfiguration.AuditProcessedMessagesTo(auditQueue); + } + + static void ConfigureRecoverability(EndpointConfiguration endpointConfiguration, IConfigurationSection? endpointConfigurationSection) { - return endpoint._endpointConfiguration; + var recoverabilitySection = endpointConfigurationSection?.GetSection("Recoverability"); + + var errorQueue = recoverabilitySection?["ErrorQueue"] ?? "error"; + endpointConfiguration.SendFailedMessagesTo(errorQueue); + + var recoverabilityConfiguration = endpointConfiguration.Recoverability(); + + if (recoverabilitySection?.GetSection("Immediate") is { } immediateSection) + { + recoverabilityConfiguration.Immediate( + immediate => + { + if(immediateSection["NumberOfRetries"] is {} numberOfRetries) + { + immediate.NumberOfRetries(int.Parse(numberOfRetries)); + } + }); + } + + if(recoverabilitySection?.GetSection("Delayed") is { } delayedSection) + { + recoverabilityConfiguration.Delayed( + delayed => + { + if(delayedSection["NumberOfRetries"] is { } numberOfRetries) + { + delayed.NumberOfRetries(int.Parse(numberOfRetries)); + } + + if (delayedSection["TimeIncrease"] is {} timeIncrease) + {; + delayed.TimeIncrease(TimeSpan.Parse(timeIncrease)); + } + }); + } + } + + protected static string GetEndpointNameFromConfigurationOrThrow(IConfiguration configuration) + { + if (configuration == null) + { + throw new ArgumentNullException(nameof(configuration)); + } + + return configuration.GetSection(NServiceBusEndpointConfigurationSectionName)["EndpointName"] + ?? throw new ArgumentException( + "EndpointName cannot be null. Make sure the " + + "NServiceBus:EndpointConfiguration:EndpointName configuration section is set."); + } + + protected virtual void FinalizeConfiguration() + { + ConfigureAuditing(EndpointConfiguration, EndpointConfigurationSection); + ConfigureRecoverability(EndpointConfiguration, EndpointConfigurationSection); + + if (_useDefaultSerializer) + { + var serializerConfiguration = EndpointConfiguration.UseSerialization(); + _serializerCustomization?.Invoke(serializerConfiguration); + } + + var transportConfigurationSection = EndpointConfigurationSection?.GetSection("Transport"); + Transport = _transportFactory != null ? _transportFactory(_configuration) : CreateTransport(transportConfigurationSection); + + _transportCustomization?.Invoke(Transport); + EndpointConfiguration.UseTransport(Transport); + + // TODO create and configure the persistence + } + + public static implicit operator EndpointConfiguration(NServiceBusEndpoint endpoint) + { + endpoint.FinalizeConfiguration(); + return endpoint.EndpointConfiguration; } public PersistenceExtensions UsePersistence() where T : PersistenceDefinition { - return _endpointConfiguration.UsePersistence(); + return EndpointConfiguration.UsePersistence(); } public PersistenceExtensions UsePersistence() where T : PersistenceDefinition where S : StorageType { - return _endpointConfiguration.UsePersistence(); + return EndpointConfiguration.UsePersistence(); } public SerializationExtensions ReplaceDefaultSerializer() where T : SerializationDefinition, new() { - return ConfigurationManager.ReplaceDefaultSerializer(); + _useDefaultSerializer = false; + return EndpointConfiguration.UseSerialization(); } - public void CustomizeDefaultSerializer(Action>? serializerCustomization) + public void CustomizeDefaultSerializer(Action> serializerCustomization) { - ConfigurationManager.CustomizeDefaultSerializer(serializerCustomization); + _serializerCustomization = serializerCustomization; } public void CustomizeTransport(Action transportCustomization) { - ConfigurationManager.CustomizeTransport(transportCustomization); + _transportCustomization = transportCustomization; } public void OverrideTransport(Func transportFactory) { - ConfigurationManager.OverrideTransport(transportFactory); + _transportFactory = transportFactory; } public async Task Start() { - var endpointInstance = await Endpoint.Start(_endpointConfiguration).ConfigureAwait(false); + FinalizeConfiguration(); + + var endpointInstance = await Endpoint.Start(EndpointConfiguration).ConfigureAwait(false); return endpointInstance; } } \ No newline at end of file