From 698a9a20fc0bfe4545223bafa5e6479f159358b8 Mon Sep 17 00:00:00 2001 From: Tomasz Maruszak Date: Fri, 20 Sep 2024 23:45:56 +0200 Subject: [PATCH] Tweaks after #307 Signed-off-by: Tomasz Maruszak --- src/Host.Plugin.Properties.xml | 2 +- .../Builders/MessageBusBuilder.cs | 17 ++++++++++++----- .../Settings/MessageBusSettings.cs | 6 +++--- .../SlimMessageBus.Host.Configuration.csproj | 2 +- .../ServiceCollectionExtensions.cs | 16 +++++----------- src/SlimMessageBus.Host/MessageBusBase.cs | 5 ++--- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Host.Plugin.Properties.xml b/src/Host.Plugin.Properties.xml index 52871511..2578f949 100644 --- a/src/Host.Plugin.Properties.xml +++ b/src/Host.Plugin.Properties.xml @@ -4,7 +4,7 @@ - 2.5.3 + 2.5.4-rc1 \ No newline at end of file diff --git a/src/SlimMessageBus.Host.Configuration/Builders/MessageBusBuilder.cs b/src/SlimMessageBus.Host.Configuration/Builders/MessageBusBuilder.cs index 30bb93bd..c99a469f 100644 --- a/src/SlimMessageBus.Host.Configuration/Builders/MessageBusBuilder.cs +++ b/src/SlimMessageBus.Host.Configuration/Builders/MessageBusBuilder.cs @@ -72,7 +72,7 @@ public MessageBusBuilder Produce(Action> builder) /// public MessageBusBuilder Produce(Type messageType, Action> builder) { - if (builder == null) throw new ArgumentNullException(nameof(builder)); + if (builder == null) throw new ArgumentNullException(nameof(builder)); var item = new ProducerSettings(); builder(new ProducerBuilder(item, messageType)); @@ -109,7 +109,7 @@ public MessageBusBuilder Consume(Action> bui /// public MessageBusBuilder Consume(Type messageType, Action> builder) { - if (builder == null) throw new ArgumentNullException(nameof(builder)); + if (builder == null) throw new ArgumentNullException(nameof(builder)); builder(new ConsumerBuilder(Settings, messageType)); return this; @@ -314,11 +314,18 @@ public MessageBusBuilder AutoStartConsumersEnabled(bool enabled) Settings.AutoStartConsumers = enabled; return this; } - + + /// + /// Adds a child bus with the given name . + /// If the child bus with the given name already exists, it will be reused and only the will be invoked to configure it. + /// + /// + /// + /// + /// public MessageBusBuilder AddChildBus(string busName, Action builderAction) { if (busName is null) throw new ArgumentNullException(nameof(busName)); - if (builderAction is null) throw new ArgumentNullException(nameof(builderAction)); if (!Children.TryGetValue(busName, out var child)) { @@ -334,7 +341,7 @@ public MessageBusBuilder AddChildBus(string busName, Action b child.MergeFrom(Settings); } - builderAction.Invoke(child); + builderAction?.Invoke(child); return this; } diff --git a/src/SlimMessageBus.Host.Configuration/Settings/MessageBusSettings.cs b/src/SlimMessageBus.Host.Configuration/Settings/MessageBusSettings.cs index e9a68bf7..2387868e 100644 --- a/src/SlimMessageBus.Host.Configuration/Settings/MessageBusSettings.cs +++ b/src/SlimMessageBus.Host.Configuration/Settings/MessageBusSettings.cs @@ -41,9 +41,9 @@ public IServiceProvider ServiceProvider public MessageBusSettings(MessageBusSettings parent = null) { - _children = new List(); - Producers = new List(); - Consumers = new List(); + _children = []; + Producers = []; + Consumers = []; SerializerType = typeof(IMessageSerializer); AutoStartConsumers = true; diff --git a/src/SlimMessageBus.Host.Configuration/SlimMessageBus.Host.Configuration.csproj b/src/SlimMessageBus.Host.Configuration/SlimMessageBus.Host.Configuration.csproj index 58ba9273..920b1441 100644 --- a/src/SlimMessageBus.Host.Configuration/SlimMessageBus.Host.Configuration.csproj +++ b/src/SlimMessageBus.Host.Configuration/SlimMessageBus.Host.Configuration.csproj @@ -6,7 +6,7 @@ Core configuration interfaces of SlimMessageBus SlimMessageBus SlimMessageBus.Host - 2.5.1 + 2.5.2-rc1 diff --git a/src/SlimMessageBus.Host/DependencyResolver/ServiceCollectionExtensions.cs b/src/SlimMessageBus.Host/DependencyResolver/ServiceCollectionExtensions.cs index 20d645e4..d8fbaac4 100644 --- a/src/SlimMessageBus.Host/DependencyResolver/ServiceCollectionExtensions.cs +++ b/src/SlimMessageBus.Host/DependencyResolver/ServiceCollectionExtensions.cs @@ -26,9 +26,9 @@ public static IServiceCollection AddSlimMessageBus(this IServiceCollection servi configure(mbb); // Execute post config actions for the master bus and its children - foreach (var action in mbb.PostConfigurationActions.Concat(mbb.Children.Values.SelectMany(x => x.PostConfigurationActions))) + foreach (var postConfigure in mbb.PostConfigurationActions.Concat(mbb.Children.Values.SelectMany(x => x.PostConfigurationActions))) { - action(services); + postConfigure(services); } } } @@ -55,6 +55,9 @@ public static IServiceCollection AddSlimMessageBus(this IServiceCollection servi } // MessageBusSettings + services.TryAddTransient(svp => svp.GetRequiredService().Settings); + + // IMasterMessageBus - Single master bus that holds the defined consumers and message processing pipelines services.TryAddSingleton(svp => { var mbb = svp.GetRequiredService(); @@ -66,15 +69,6 @@ public static IServiceCollection AddSlimMessageBus(this IServiceCollection servi postProcessor.Run(mbb.Settings); } - return mbb.Settings; - }); - - // IMasterMessageBus - Single master bus that holds the defined consumers and message processing pipelines - services.TryAddSingleton(svp => - { - var mbb = svp.GetRequiredService(); - var messageBusSettings = svp.GetRequiredService(); - // Set the MessageBus.Current var currentBusProvider = svp.GetRequiredService(); MessageBus.SetProvider(currentBusProvider.GetCurrent); diff --git a/src/SlimMessageBus.Host/MessageBusBase.cs b/src/SlimMessageBus.Host/MessageBusBase.cs index 385bf0eb..d0782433 100644 --- a/src/SlimMessageBus.Host/MessageBusBase.cs +++ b/src/SlimMessageBus.Host/MessageBusBase.cs @@ -84,11 +84,10 @@ public virtual IMessageSerializer Serializer protected MessageBusBase(MessageBusSettings settings) { - if (settings is null) throw new ArgumentNullException(nameof(settings)); + Settings = settings ?? throw new ArgumentNullException(nameof(settings)); + if (settings.ServiceProvider is null) throw new ConfigurationMessageBusException($"The bus {Name} has no {nameof(settings.ServiceProvider)} configured"); - Settings = settings; - // Try to resolve from DI. If not available, suppress logging by using the NullLoggerFactory LoggerFactory = settings.ServiceProvider.GetService() ?? NullLoggerFactory.Instance;