From 43dbd26769b48a9613e333b5eb5e2577eb2bbdf3 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Wed, 29 Jan 2025 12:40:23 +0100 Subject: [PATCH] chore: remove guard.net references from eventhubs --- .../AzureEventHubsMessageContext.cs | 50 +++++--- .../Extensions/EventDataExtensions.cs | 11 -- .../EventHubsMessageHandlerExtensions.cs | 17 ++- .../IServiceCollectionExtensions.cs | 19 ++- ...tHubsMessageHandlerCollectionExtensions.cs | 14 ++- ...tHubsMessageHandlerCollectionExtensions.cs | 14 ++- ...tHubsMessageHandlerCollectionExtensions.cs | 17 +-- ...tHubsMessageHandlerCollectionExtensions.cs | 33 +++--- ...tHubsMessageHandlerCollectionExtensions.cs | 37 +++--- ...tHubsMessageHandlerCollectionExtensions.cs | 29 +++-- ...tHubsMessageHandlerCollectionExtensions.cs | 33 +++--- ...ureFunctionsInProcessMessageCorrelation.cs | 9 +- .../Extensions/FunctionContextExtensions.cs | 21 ++-- .../IFunctionsHostBuilderExtensions.cs | 22 ++-- .../EventDataBuilder.cs | 15 +-- ...ProducerClientMessageCorrelationOptions.cs | 25 ++-- .../AzureClientFactoryBuilderExtensions.cs | 44 ++++--- .../Extensions/EventDataExtensions.cs | 22 ++-- .../EventHubProducerClientExtensions.cs | 70 +++-------- .../AzureEventHubsMessagePump.cs | 29 ++--- .../AzureEventHubsMessagePumpConfig.cs | 75 +++++++++--- .../IServiceCollectionExtensions.cs | 111 ++++++++++-------- 22 files changed, 389 insertions(+), 328 deletions(-) diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/AzureEventHubsMessageContext.cs b/src/Arcus.Messaging.Abstractions.EventHubs/AzureEventHubsMessageContext.cs index 805c2bed..90359ca1 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/AzureEventHubsMessageContext.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/AzureEventHubsMessageContext.cs @@ -3,7 +3,6 @@ using System.Threading; using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Producer; -using GuardNet; namespace Arcus.Messaging.Abstractions.EventHubs { @@ -13,7 +12,7 @@ namespace Arcus.Messaging.Abstractions.EventHubs public class AzureEventHubsMessageContext : MessageContext { private AzureEventHubsMessageContext( - EventData eventData, + EventData eventData, string eventHubsName, string consumerGroup, string eventHubsNamespace, @@ -125,8 +124,10 @@ public static AzureEventHubsMessageContext CreateFrom( EventData message, EventProcessorClient eventProcessor) { - Guard.NotNull(message, nameof(message), "Requires an Azure EventHubs event data message to retrieve the information to create a messaging context for this message"); - Guard.NotNull(eventProcessor, nameof(eventProcessor), "Requires an Azure EventHubs event processor to retrieve the information to create a messaging context for the consumed event"); + if (eventProcessor is null) + { + throw new ArgumentNullException(nameof(eventProcessor)); + } return CreateFrom( message, @@ -157,11 +158,6 @@ public static AzureEventHubsMessageContext CreateFrom( string consumerGroup, string eventHubsName) { - Guard.NotNull(message, nameof(message), "Requires an Azure EventHubs event data message to retrieve the information to create a messaging context for this message"); - Guard.NotNullOrWhitespace(eventHubsNamespace, nameof(eventHubsNamespace), "Requires a non-blank Azure EventHubs fully qualified namespace to relate the messaging context to the event message"); - Guard.NotNullOrWhitespace(consumerGroup, nameof(consumerGroup), "Requires a non-blank Azure EventHubs consumer group to relate the messaging context to the event message"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name to relate the messaging context to the event message"); - return CreateFrom(message, eventHubsNamespace, consumerGroup, eventHubsName, ""); } @@ -179,9 +175,10 @@ public static AzureEventHubsMessageContext CreateFrom( EventProcessorClient eventProcessor, string jobId) { - Guard.NotNull(message, nameof(message), "Requires an Azure EventHubs event data message to retrieve the information to create a messaging context for this message"); - Guard.NotNull(eventProcessor, nameof(eventProcessor), "Requires an Azure EventHubs event processor to retrieve the information to create a messaging context for the consumed event"); - Guard.NotNullOrWhitespace(jobId, nameof(jobId), "Requires a non-blank job ID to link this message context to a message pump that processes the event message"); + if (eventProcessor is null) + { + throw new ArgumentNullException(nameof(eventProcessor)); + } return CreateFrom( message, @@ -213,11 +210,30 @@ public static AzureEventHubsMessageContext CreateFrom( string eventHubsName, string jobId) { - Guard.NotNull(message, nameof(message), "Requires an Azure EventHubs event data message to retrieve the information to create a messaging context for this message"); - Guard.NotNullOrWhitespace(eventHubsNamespace, nameof(eventHubsNamespace), "Requires a non-blank Azure EventHubs fully qualified namespace to relate the messaging context to the event message"); - Guard.NotNullOrWhitespace(consumerGroup, nameof(consumerGroup), "Requires a non-blank Azure EventHubs consumer group to relate the messaging context to the event message"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name to relate the messaging context to the event message"); - Guard.NotNullOrWhitespace(jobId, nameof(jobId), "Requires a non-blank job ID to link this message context to a message pump that processes the event message"); + if (message is null) + { + throw new ArgumentNullException(nameof(message)); + } + + if (string.IsNullOrWhiteSpace(eventHubsNamespace)) + { + throw new ArgumentException("Requires a non-blank Azure EventHubs fully qualified namespace to relate the messaging context to the event message", nameof(eventHubsNamespace)); + } + + if (string.IsNullOrWhiteSpace(consumerGroup)) + { + throw new ArgumentException("Requires a non-blank Azure EventHubs consumer group to relate the messaging context to the event message", nameof(consumerGroup)); + } + + if (string.IsNullOrWhiteSpace(eventHubsName)) + { + throw new ArgumentException("Requires a non-blank Azure EventHubs name to relate the messaging context to the event message", nameof(eventHubsName)); + } + + if (string.IsNullOrWhiteSpace(jobId)) + { + throw new ArgumentException("Requires a non-blank job ID to link this message context to a message pump that processes the event message", nameof(jobId)); + } return new AzureEventHubsMessageContext(message, eventHubsName, consumerGroup, eventHubsNamespace, jobId); } diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/EventDataExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/EventDataExtensions.cs index 21489d0b..8c49f705 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/EventDataExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/EventDataExtensions.cs @@ -1,6 +1,5 @@ using System; using Arcus.Messaging.Abstractions.EventHubs; -using GuardNet; // ReSharper disable once CheckNamespace namespace Azure.Messaging.EventHubs @@ -20,10 +19,6 @@ public static class EventDataExtensions /// Thrown when the is blank. public static AzureEventHubsMessageContext GetMessageContext(this EventData message, EventProcessorClient eventProcessor, string jobId) { - Guard.NotNull(message, nameof(message), "Requires an event data to retrieve the Azure EventHubs message context"); - Guard.NotNull(eventProcessor, nameof(eventProcessor), "Requires an Azure EventHubs event processor to retrieve the information to create a messaging context for the consumed event"); - Guard.NotNullOrWhitespace(jobId, nameof(jobId), "Requires a non-blank job ID to link this message context to a message pump that processes the event message"); - return AzureEventHubsMessageContext.CreateFrom(message, eventProcessor, jobId); } @@ -44,12 +39,6 @@ public static AzureEventHubsMessageContext GetMessageContext(this EventData mess /// public static AzureEventHubsMessageContext GetMessageContext(this EventData message, string eventHubsNamespace, string eventHubsName, string consumerGroup, string jobId) { - Guard.NotNull(message, nameof(message), "Requires an event data to retrieve the Azure EventHubs message context"); - Guard.NotNullOrWhitespace(eventHubsNamespace, nameof(eventHubsNamespace), "Requires an Azure EventHubs namespace to built-up the message context"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires an Azure EventHubs name to built-up the message context"); - Guard.NotNullOrWhitespace(consumerGroup, nameof(consumerGroup), "Requires an Azure EventHubs consumer group to built-up the message context"); - Guard.NotNullOrWhitespace(jobId, nameof(jobId), "Requires a non-blank job ID to link this message context to a message pump that processes the event message"); - return AzureEventHubsMessageContext.CreateFrom(message, eventHubsNamespace, consumerGroup, eventHubsName, jobId); } } diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/EventHubsMessageHandlerExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/EventHubsMessageHandlerExtensions.cs index 6bb6f956..b9964432 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/EventHubsMessageHandlerExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/EventHubsMessageHandlerExtensions.cs @@ -1,7 +1,6 @@ using System; using Arcus.Messaging.Abstractions.EventHubs; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -24,7 +23,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(); return handlers; @@ -45,8 +47,15 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler with dependent handlers"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } + + if (implementationFactory is null) + { + throw new ArgumentNullException(nameof(implementationFactory)); + } handlers.WithMessageHandler(implementationFactory); return handlers; diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/IServiceCollectionExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/IServiceCollectionExtensions.cs index 35fea274..4d842ce6 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/IServiceCollectionExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/IServiceCollectionExtensions.cs @@ -1,6 +1,5 @@ using System; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; @@ -22,8 +21,6 @@ public static class IServiceCollectionExtensions public static EventHubsMessageHandlerCollection AddEventHubsMessageRouting( this IServiceCollection services) { - Guard.NotNull(services, nameof(services), "Requires a set of services to register the Azure EventHubs message routing"); - return AddEventHubsMessageRouting(services, configureOptions: null); } @@ -38,8 +35,6 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessageRouting( this IServiceCollection services, Action configureOptions) { - Guard.NotNull(services, nameof(services), "Requires a set of services to register the Azure EventHubs message routing"); - return AddEventHubsMessageRouting(services, (serviceProvider, options) => { var logger = serviceProvider.GetService>(); @@ -60,9 +55,6 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessageRouting implementationFactory) where TMessageRouter : IAzureEventHubsMessageRouter { - Guard.NotNull(services, nameof(services), "Requires a set of services to register the Azure EventHubs message routing"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the Azure EventHubs message router"); - return AddEventHubsMessageRouting(services, (serviceProvider, options) => implementationFactory(serviceProvider), configureOptions: null); } @@ -81,8 +73,15 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessageRouting configureOptions) where TMessageRouter : IAzureEventHubsMessageRouter { - Guard.NotNull(services, nameof(services), "Requires a set of services to register the Azure EventHubs message routing"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the Azure EventHubs message router"); + if (services is null) + { + throw new ArgumentNullException(nameof(services)); + } + + if (implementationFactory is null) + { + throw new ArgumentNullException(nameof(implementationFactory)); + } services.TryAddSingleton(serviceProvider => { diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/Message.EventHubsMessageHandlerCollectionExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/Message.EventHubsMessageHandlerCollectionExtensions.cs index 70538fd4..c9a8aa7a 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/Message.EventHubsMessageHandlerCollectionExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/Message.EventHubsMessageHandlerCollectionExtensions.cs @@ -1,7 +1,6 @@ using System; using Arcus.Messaging.Abstractions.EventHubs; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -27,8 +26,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodyFilter); return handlers; @@ -51,9 +52,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler with dependent handlers"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodyFilter, implementationFactory); return handlers; diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.EventHubsMessageHandlerCollectionExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.EventHubsMessageHandlerCollectionExtensions.cs index 1b05d655..5b05486f 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.EventHubsMessageHandlerCollectionExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.EventHubsMessageHandlerCollectionExtensions.cs @@ -1,7 +1,6 @@ using System; using Arcus.Messaging.Abstractions.EventHubs; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -27,8 +26,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageContextFilter, nameof(messageContextFilter), "Requires a filter to restrict the message processing within a certain message context"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler( messageContextFilter, serviceProvider => ActivatorUtilities.CreateInstance(serviceProvider)); @@ -53,9 +54,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageContextFilter, nameof(messageContextFilter), "Requires a filter to restrict the message processing within a certain message context"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler with dependent handlers"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageContextFilter, implementationFactory); return handlers; diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.Message.EventHubsMessageHandlerCollectionExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.Message.EventHubsMessageHandlerCollectionExtensions.cs index ba84b86b..b0655881 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.Message.EventHubsMessageHandlerCollectionExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.Message.EventHubsMessageHandlerCollectionExtensions.cs @@ -1,7 +1,6 @@ using System; using Arcus.Messaging.Abstractions.EventHubs; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -29,8 +28,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageContextFilter, nameof(messageContextFilter), "Requires a filter to restrict the message processing within a certain message context"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageContextFilter, messageBodyFilter); return handlers; @@ -55,12 +56,12 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageContextFilter, nameof(messageContextFilter), "Requires a filter to restrict the message processing within a certain message context"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler with dependent handlers"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } - handlers.WithMessageHandler(messageContextFilter, messageBodyFilter, implementationFactory); + handlers.WithMessageHandler(messageContextFilter, messageBodyFilter, implementationFactory); return handlers; } } diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.MessageSerializer.EventHubsMessageHandlerCollectionExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.MessageSerializer.EventHubsMessageHandlerCollectionExtensions.cs index 9426e0f7..9edaf85f 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.MessageSerializer.EventHubsMessageHandlerCollectionExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.MessageSerializer.EventHubsMessageHandlerCollectionExtensions.cs @@ -1,8 +1,7 @@ using System; -using Arcus.Messaging.Abstractions.MessageHandling; using Arcus.Messaging.Abstractions.EventHubs; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; +using Arcus.Messaging.Abstractions.MessageHandling; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -30,9 +29,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageContextFilter, nameof(messageContextFilter), "Requires a filter to restrict the message processing within a certain message context"); - Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageContextFilter, messageBodySerializer); return handlers; @@ -55,9 +55,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageContextFilter, nameof(messageContextFilter), "Requires a filter to restrict the message processing within a certain message context"); - Guard.NotNull(messageBodySerializerImplementationFactory, nameof(messageBodySerializerImplementationFactory), "Requires a function to create an custom message body serializer instance to deserialize incoming message for the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageContextFilter, messageBodySerializerImplementationFactory); return handlers; @@ -82,10 +83,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageContextFilter, nameof(messageContextFilter), "Requires a filter to restrict the message processing within a certain message context"); - Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageContextFilter, messageBodySerializer, implementationFactory); return handlers; @@ -110,10 +111,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageContextFilter, nameof(messageContextFilter), "Requires a filter to restrict the message processing within a certain message context"); - Guard.NotNull(messageBodySerializerImplementationFactory, nameof(messageBodySerializerImplementationFactory), "Requires a function to create an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageHandlerImplementationFactory, nameof(messageHandlerImplementationFactory), "Requires a function to create the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageContextFilter, messageBodySerializerImplementationFactory, messageHandlerImplementationFactory); return handlers; diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.MessageSerializer.Message.EventHubsMessageHandlerCollectionExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.MessageSerializer.Message.EventHubsMessageHandlerCollectionExtensions.cs index 095206fc..6c58f5f5 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.MessageSerializer.Message.EventHubsMessageHandlerCollectionExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageContext.MessageSerializer.Message.EventHubsMessageHandlerCollectionExtensions.cs @@ -1,8 +1,7 @@ using System; -using Arcus.Messaging.Abstractions.MessageHandling; using Arcus.Messaging.Abstractions.EventHubs; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; +using Arcus.Messaging.Abstractions.MessageHandling; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -32,9 +31,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageContextFilter, messageBodySerializer, messageBodyFilter); return handlers; @@ -59,9 +59,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializerImplementationFactory, nameof(messageBodySerializerImplementationFactory), "Requires a function to create an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageContextFilter, messageBodySerializerImplementationFactory, messageBodyFilter); return handlers; @@ -88,12 +89,12 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } - handlers.WithMessageHandler(messageContextFilter, messageBodySerializer, messageBodyFilter, implementationFactory); + handlers.WithMessageHandler(messageContextFilter, messageBodySerializer, messageBodyFilter, implementationFactory); return handlers; } @@ -118,12 +119,12 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializerImplementationFactory, nameof(messageBodySerializerImplementationFactory), "Requires a function to create an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); - Guard.NotNull(messageHandlerImplementationFactory, nameof(messageHandlerImplementationFactory), "Requires a function to create the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } - handlers.WithMessageHandler(messageContextFilter, messageBodySerializerImplementationFactory, messageBodyFilter, messageHandlerImplementationFactory); + handlers.WithMessageHandler(messageContextFilter, messageBodySerializerImplementationFactory, messageBodyFilter, messageHandlerImplementationFactory); return handlers; } } diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageSerializer.EventHubsMessageHandlerCollectionExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageSerializer.EventHubsMessageHandlerCollectionExtensions.cs index 3eb44ebe..3b24addf 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageSerializer.EventHubsMessageHandlerCollectionExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageSerializer.EventHubsMessageHandlerCollectionExtensions.cs @@ -1,8 +1,7 @@ using System; -using Arcus.Messaging.Abstractions.MessageHandling; using Arcus.Messaging.Abstractions.EventHubs; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; +using Arcus.Messaging.Abstractions.MessageHandling; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -28,8 +27,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodySerializer); return handlers; @@ -50,8 +51,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializerImplementationFactory, nameof(messageBodySerializerImplementationFactory), "Requires a function to create the custom message body serializer instance to deserialize incoming message for the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodySerializerImplementationFactory); return handlers; @@ -74,9 +77,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodySerializer, implementationFactory); return handlers; @@ -99,9 +103,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializerImplementationFactory, nameof(messageBodySerializerImplementationFactory), "Requires a function to create the custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageHandlerImplementationFactory, nameof(messageHandlerImplementationFactory), "Requires a function to create the message handler"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodySerializerImplementationFactory, messageHandlerImplementationFactory); return handlers; diff --git a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageSerializer.Message.EventHubsMessageHandlerCollectionExtensions.cs b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageSerializer.Message.EventHubsMessageHandlerCollectionExtensions.cs index 82e7bcab..9da8ceb3 100644 --- a/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageSerializer.Message.EventHubsMessageHandlerCollectionExtensions.cs +++ b/src/Arcus.Messaging.Abstractions.EventHubs/Extensions/MessageSerializer.Message.EventHubsMessageHandlerCollectionExtensions.cs @@ -1,8 +1,7 @@ using System; -using Arcus.Messaging.Abstractions.MessageHandling; using Arcus.Messaging.Abstractions.EventHubs; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; -using GuardNet; +using Arcus.Messaging.Abstractions.MessageHandling; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -30,9 +29,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodySerializer, messageBodyFilter); return handlers; @@ -57,10 +57,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler with dependent handlers"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodySerializer, messageBodyFilter, implementationFactory); return handlers; @@ -83,9 +83,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializerImplementationFactory, nameof(messageBodySerializerImplementationFactory), "Requires a function to create the custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodySerializerImplementationFactory, messageBodyFilter); return handlers; @@ -110,10 +111,10 @@ public static EventHubsMessageHandlerCollection WithEventHubsMessageHandler where TMessage : class { - Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); - Guard.NotNull(messageBodySerializerImplementationFactory, nameof(messageBodySerializerImplementationFactory), "Requires a function to create the custom message body serializer instance to deserialize incoming message for the message handler"); - Guard.NotNull(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); - Guard.NotNull(messageHandlerImplementationFactory, nameof(messageHandlerImplementationFactory), "Requires a function to create the message handler with dependent handlers"); + if (handlers is null) + { + throw new ArgumentNullException(nameof(handlers)); + } handlers.WithMessageHandler(messageBodySerializerImplementationFactory, messageBodyFilter, messageHandlerImplementationFactory); return handlers; diff --git a/src/Arcus.Messaging.AzureFunctions.EventHubs/AzureFunctionsInProcessMessageCorrelation.cs b/src/Arcus.Messaging.AzureFunctions.EventHubs/AzureFunctionsInProcessMessageCorrelation.cs index fafb9cee..3e567c41 100644 --- a/src/Arcus.Messaging.AzureFunctions.EventHubs/AzureFunctionsInProcessMessageCorrelation.cs +++ b/src/Arcus.Messaging.AzureFunctions.EventHubs/AzureFunctionsInProcessMessageCorrelation.cs @@ -1,7 +1,6 @@ using System; using Arcus.Messaging.Abstractions; using Azure.Messaging.EventHubs; -using GuardNet; using Microsoft.ApplicationInsights; namespace Arcus.Messaging.AzureFunctions.EventHubs @@ -20,8 +19,7 @@ public class AzureFunctionsInProcessMessageCorrelation /// Thrown when the is null. public AzureFunctionsInProcessMessageCorrelation(TelemetryClient client) { - Guard.NotNull(client, nameof(client), "Requires a Microsoft telemetry client to automatically track outgoing built-in Microsoft dependencies"); - _telemetryClient = client; + _telemetryClient = client ?? throw new ArgumentNullException(nameof(client)); } /// @@ -32,7 +30,10 @@ public AzureFunctionsInProcessMessageCorrelation(TelemetryClient client) /// Thrown when the is null. public MessageCorrelationResult CorrelateMessage(EventData message) { - Guard.NotNull(message, nameof(message), "Requires an incoming Azure EventHubs message to W3C correlate the message"); + if (message is null) + { + throw new ArgumentNullException(nameof(message)); + } (string transactionId, string operationParentId) = message.Properties.GetTraceParent(); return MessageCorrelationResult.Create(_telemetryClient, transactionId, operationParentId); diff --git a/src/Arcus.Messaging.AzureFunctions.EventHubs/Extensions/FunctionContextExtensions.cs b/src/Arcus.Messaging.AzureFunctions.EventHubs/Extensions/FunctionContextExtensions.cs index 8f09f3d4..2d35c1ee 100644 --- a/src/Arcus.Messaging.AzureFunctions.EventHubs/Extensions/FunctionContextExtensions.cs +++ b/src/Arcus.Messaging.AzureFunctions.EventHubs/Extensions/FunctionContextExtensions.cs @@ -4,7 +4,6 @@ using System.Text.Json; using Arcus.Messaging.Abstractions; using Arcus.Messaging.Abstractions.MessageHandling; -using GuardNet; using Microsoft.ApplicationInsights; using Microsoft.Extensions.DependencyInjection; @@ -30,9 +29,6 @@ public static MessageCorrelationResult GetCorrelationInfo( this FunctionContext context, Dictionary applicationProperties) { - Guard.NotNull(context, nameof(context), "Requires a function context to retrieve the message correlation from"); - Guard.NotNull(applicationProperties, nameof(applicationProperties), "Requires a series of application properties associated with the received event"); - return GetCorrelationInfo(context, applicationProperties, MessageCorrelationFormat.W3C); } @@ -52,8 +48,15 @@ public static MessageCorrelationResult GetCorrelationInfo( Dictionary applicationProperties, MessageCorrelationFormat correlationFormat) { - Guard.NotNull(context, nameof(context), "Requires a function context to retrieve the message correlation from"); - Guard.NotNull(applicationProperties, nameof(applicationProperties), "Requires a series of application properties associated with the received event"); + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (applicationProperties is null) + { + throw new ArgumentNullException(nameof(applicationProperties)); + } if (correlationFormat is MessageCorrelationFormat.W3C) { @@ -70,7 +73,7 @@ public static MessageCorrelationResult GetCorrelationInfo( } if (correlationFormat is MessageCorrelationFormat.Hierarchical) - { + { string transactionId = DetermineTransactionId(applicationProperties, PropertyNames.TransactionId); string operationId = DetermineOperationId(applicationProperties); string operationParentId = GetOptionalUserProperty(applicationProperties, PropertyNames.OperationParentId); @@ -87,9 +90,9 @@ private static (string transactionId, string operationParentId) DetermineTracePa { IDictionary castProperties = applicationProperties.ToDictionary( - item => item.Key, + item => item.Key, item => (object) item.Value.GetString()); - + return castProperties.GetTraceParent(); } diff --git a/src/Arcus.Messaging.AzureFunctions.EventHubs/Extensions/IFunctionsHostBuilderExtensions.cs b/src/Arcus.Messaging.AzureFunctions.EventHubs/Extensions/IFunctionsHostBuilderExtensions.cs index 69363607..f848bb19 100644 --- a/src/Arcus.Messaging.AzureFunctions.EventHubs/Extensions/IFunctionsHostBuilderExtensions.cs +++ b/src/Arcus.Messaging.AzureFunctions.EventHubs/Extensions/IFunctionsHostBuilderExtensions.cs @@ -1,7 +1,6 @@ using System; using Arcus.Messaging.Abstractions.EventHubs.MessageHandling; using Arcus.Messaging.AzureFunctions.EventHubs; -using GuardNet; using Microsoft.Extensions.DependencyInjection; // ReSharper disable once CheckNamespace @@ -22,8 +21,6 @@ public static class IFunctionsHostBuilderExtensions public static EventHubsMessageHandlerCollection AddEventHubsMessageRouting( this IFunctionsHostBuilder builder) { - Guard.NotNull(builder, nameof(builder), "Requires a functions host builder to add the Azure EventHubs message router"); - return AddEventHubsMessageRouting(builder, configureOptions: null); } @@ -38,7 +35,10 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessageRouting( this IFunctionsHostBuilder builder, Action configureOptions) { - Guard.NotNull(builder, nameof(builder), "Requires a functions host builder to add the Azure EventHubs message router"); + if (builder is null) + { + throw new ArgumentNullException(nameof(builder)); + } return builder.Services.AddSingleton() .AddEventHubsMessageRouting(configureOptions); @@ -57,9 +57,6 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessageRouting implementationFactory) where TMessageRouter : IAzureEventHubsMessageRouter { - Guard.NotNull(builder, nameof(builder), "Requires a functions host builder to add the Azure EventHubs message router"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the Azure EventHubs message router"); - return AddEventHubsMessageRouting(builder, (provider, options) => implementationFactory(provider), configureOptions: null); } @@ -78,8 +75,15 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessageRouting configureOptions) where TMessageRouter : IAzureEventHubsMessageRouter { - Guard.NotNull(builder, nameof(builder), "Requires a functions host builder to add the Azure EventHubs message router"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the Azure EventHubs message router"); + if (builder is null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (implementationFactory is null) + { + throw new ArgumentNullException(nameof(implementationFactory)); + } return builder.Services.AddSingleton() .AddEventHubsMessageRouting(implementationFactory, configureOptions); diff --git a/src/Arcus.Messaging.EventHubs.Core/EventDataBuilder.cs b/src/Arcus.Messaging.EventHubs.Core/EventDataBuilder.cs index 777c1509..62f02b2a 100644 --- a/src/Arcus.Messaging.EventHubs.Core/EventDataBuilder.cs +++ b/src/Arcus.Messaging.EventHubs.Core/EventDataBuilder.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Text; using Arcus.Messaging.Abstractions; -using GuardNet; using Newtonsoft.Json; // ReSharper disable once CheckNamespace @@ -31,7 +30,6 @@ private EventDataBuilder(object eventBody, Encoding encoding) /// Thrown when the or the is null. public static EventDataBuilder CreateForBody(object eventBody) { - Guard.NotNull(eventBody, nameof(eventBody), "Requires a message body to include in the to-be-created Azure EventHubs message"); return CreateForBody(eventBody, Encoding.UTF8); } @@ -43,10 +41,9 @@ public static EventDataBuilder CreateForBody(object eventBody) /// Thrown when the or the is null. public static EventDataBuilder CreateForBody(object eventBody, Encoding encoding) { - Guard.NotNull(eventBody, nameof(eventBody), "Requires a message body to include in the to-be-created Azure EventHubs message"); - Guard.NotNull(encoding, nameof(encoding), "Requires an encoding instance to encode the passed-in message body so it can be included in the Azure EventHubs message"); - - return new EventDataBuilder(eventBody, encoding); + return new EventDataBuilder( + eventBody ?? throw new ArgumentNullException(nameof(eventBody)), + encoding ?? throw new ArgumentNullException(nameof(encoding))); } /// @@ -110,7 +107,7 @@ public EventDataBuilder WithTransactionId(string transactionId, string transacti { _transactionIdProperty = new KeyValuePair( transactionIdPropertyName ?? PropertyNames.TransactionId, - transactionId); + transactionId); } return this; @@ -145,7 +142,7 @@ public EventDataBuilder WithOperationParentId( { _operationParentIdProperty = new KeyValuePair( operationParentIdPropertyName ?? PropertyNames.OperationParentId, - operationParentId); + operationParentId); } return this; @@ -170,7 +167,7 @@ public EventData Build() if (_operationIdProperty.Key is null && _operationIdProperty.Value is not null) { eventData.CorrelationId = _operationIdProperty.Value?.ToString(); - } + } else if (_operationIdProperty.Value is not null) { eventData.Properties.Add(_operationIdProperty); diff --git a/src/Arcus.Messaging.EventHubs.Core/EventHubProducerClientMessageCorrelationOptions.cs b/src/Arcus.Messaging.EventHubs.Core/EventHubProducerClientMessageCorrelationOptions.cs index 6a34ed8a..44605948 100644 --- a/src/Arcus.Messaging.EventHubs.Core/EventHubProducerClientMessageCorrelationOptions.cs +++ b/src/Arcus.Messaging.EventHubs.Core/EventHubProducerClientMessageCorrelationOptions.cs @@ -5,7 +5,6 @@ using Arcus.Observability.Correlation; using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Producer; -using GuardNet; using Microsoft.Extensions.Logging; namespace Arcus.Messaging.EventHubs.Core @@ -28,7 +27,11 @@ public string TransactionIdPropertyName get => _transactionIdPropertyName; set { - Guard.NotNullOrWhitespace(value, nameof(value), "Requires a non-blank value for the message correlation transaction ID Azure Service Bus application property name"); + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("Requires a non-blank value for the message correlation transaction ID Azure Service Bus application property name", nameof(value)); + } + _transactionIdPropertyName = value; } } @@ -42,7 +45,11 @@ public string UpstreamServicePropertyName get => _upstreamServicePropertyName; set { - Guard.NotNullOrWhitespace(value, nameof(value), "Requires a non-blank value for the message correlation upstream service Azure Service Bus application property name"); + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("Requires a non-blank value for the message correlation upstream service Azure Service Bus application property name", nameof(value)); + } + _upstreamServicePropertyName = value; } } @@ -53,11 +60,7 @@ public string UpstreamServicePropertyName public Func GenerateDependencyId { get => _generateDependencyId; - set - { - Guard.NotNull(value, nameof(value), "Requires a function to generate the dependency ID used when tracking Azure Service Bus dependencies"); - _generateDependencyId = value; - } + set => _generateDependencyId = value ?? throw new ArgumentNullException(nameof(value)); } /// @@ -72,7 +75,11 @@ public Func GenerateDependencyId /// Thrown when the is null. public void AddTelemetryContext(Dictionary telemetryContext) { - Guard.NotNull(telemetryContext, nameof(telemetryContext), "Requires a telemetry context instance to add to the dependency tracking"); + if (telemetryContext is null) + { + throw new ArgumentNullException(nameof(telemetryContext)); + } + foreach (KeyValuePair item in telemetryContext) { TelemetryContext[item.Key] = item.Value; diff --git a/src/Arcus.Messaging.EventHubs.Core/Extensions/AzureClientFactoryBuilderExtensions.cs b/src/Arcus.Messaging.EventHubs.Core/Extensions/AzureClientFactoryBuilderExtensions.cs index 12f0ea42..3dc7c0bc 100644 --- a/src/Arcus.Messaging.EventHubs.Core/Extensions/AzureClientFactoryBuilderExtensions.cs +++ b/src/Arcus.Messaging.EventHubs.Core/Extensions/AzureClientFactoryBuilderExtensions.cs @@ -2,13 +2,12 @@ using Arcus.Security.Core; using Azure.Core.Extensions; using Azure.Messaging.EventHubs.Producer; -using GuardNet; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; // ReSharper disable once CheckNamespace -namespace Microsoft.Extensions.Azure +namespace Microsoft.Extensions.Azure { /// /// Extensions on the to add more easily Azure EventHubs producer clients with Arcus components. @@ -41,9 +40,6 @@ public static IAzureClientBuilder configureOptions) { - Guard.NotNull(builder, nameof(builder), "Requires an Azure client factory builder to add the Azure EventHubs producer client"); - Guard.NotNullOrWhitespace(connectionStringSecretName, nameof(connectionStringSecretName), "Requires a non-blank secret name to retrieve the Azure EventHubs connection string from the Arcus secret store"); + if (builder is null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrWhiteSpace(connectionStringSecretName)) + { + throw new ArgumentException("Requires a non-blank secret name that points to an Azure Event Hubs connection string", nameof(connectionStringSecretName)); + } return builder.AddClient((options, serviceProvider) => { @@ -87,7 +90,7 @@ public static IAzureClientBuilder + /// /// Registers a instance into the Azure client factory /// via a connection string available via the in the Arcus secret store. /// @@ -115,10 +118,6 @@ public static IAzureClientBuilder configureOptions) { - Guard.NotNull(builder, nameof(builder), "Requires an Azure client factory builder to add the Azure EventHubs producer client"); - Guard.NotNullOrWhitespace(connectionStringSecretName, nameof(connectionStringSecretName), "Requires a non-blank secret name to retrieve the Azure EventHubs connection string from the Arcus secret store"); - Guard.NotNullOrWhitespace(eventHubName, nameof(eventHubName), "Requires a non-blank Azure EventHubs name to register the Azure EventHubs producer client"); + if (builder is null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrWhiteSpace(connectionStringSecretName)) + { + throw new ArgumentException("Requires a non-blank secret name that points to an Azure Event Hubs connection string", nameof(connectionStringSecretName)); + } + + if (string.IsNullOrWhiteSpace(eventHubName)) + { + throw new ArgumentException("Requires a non-blank Azure EventHubs name to register the Azure EventHubs producer client", nameof(eventHubName)); + } return builder.AddClient((options, serviceProvider) => { @@ -182,8 +192,8 @@ private static string GetEventHubsConnectionString(string connectionStringSecret } catch (Exception exception) { - ILogger logger = - serviceProvider.GetService>() + ILogger logger = + serviceProvider.GetService>() ?? NullLogger.Instance; logger.LogTrace(exception, "Cannot synchronously retrieve Azure EventHubs connection string secret for '{SecretName}', fallback on asynchronously", connectionStringSecretName); diff --git a/src/Arcus.Messaging.EventHubs.Core/Extensions/EventDataExtensions.cs b/src/Arcus.Messaging.EventHubs.Core/Extensions/EventDataExtensions.cs index 5286fb23..31103261 100644 --- a/src/Arcus.Messaging.EventHubs.Core/Extensions/EventDataExtensions.cs +++ b/src/Arcus.Messaging.EventHubs.Core/Extensions/EventDataExtensions.cs @@ -1,6 +1,5 @@ using System; using Arcus.Messaging.Abstractions; -using GuardNet; // ReSharper disable once CheckNamespace namespace Azure.Messaging.EventHubs @@ -22,7 +21,6 @@ public static class EventDataExtensions /// Thrown when the is null. public static MessageCorrelationInfo GetCorrelationInfo(this EventData eventData) { - Guard.NotNull(eventData, nameof(eventData), "Requires an event data instance to retrieve the message correlation information"); return GetCorrelationInfo(eventData, PropertyNames.TransactionId); } @@ -44,9 +42,6 @@ public static MessageCorrelationInfo GetCorrelationInfo( this EventData eventData, string transactionIdPropertyName) { - Guard.NotNull(eventData, nameof(eventData), "Requires an event data instance to retrieve the message correlation information"); - Guard.NotNullOrWhitespace(transactionIdPropertyName, nameof(transactionIdPropertyName), "Requires a non-blank application property name to retrieve the transaction ID from the event data's properties"); - return GetCorrelationInfo(eventData, transactionIdPropertyName, PropertyNames.OperationParentId); } @@ -74,9 +69,20 @@ public static MessageCorrelationInfo GetCorrelationInfo( string transactionIdPropertyName, string operationParentIdPropertyName) { - Guard.NotNull(eventData, nameof(eventData), "Requires an event data instance to retrieve the message correlation information"); - Guard.NotNullOrWhitespace(transactionIdPropertyName, nameof(transactionIdPropertyName), "Requires a non-blank application property name to retrieve the transaction ID from the event data's properties"); - Guard.NotNullOrWhitespace(operationParentIdPropertyName, nameof(operationParentIdPropertyName), "Requires a non-blank application property name to retrieve the operation parent ID from the event data's properties"); + if (eventData is null) + { + throw new ArgumentNullException(nameof(eventData)); + } + + if (string.IsNullOrWhiteSpace(transactionIdPropertyName)) + { + throw new ArgumentException("Requires a non-blank application property name to retrieve the transaction ID from the event data's properties", nameof(transactionIdPropertyName)); + } + + if (string.IsNullOrWhiteSpace(operationParentIdPropertyName)) + { + throw new ArgumentException("Requires a non-blank application property name to retrieve the operation parent ID from the event data's properties", nameof(operationParentIdPropertyName)); + } string transactionId = DetermineTransactionId(eventData, transactionIdPropertyName); string operationId = DetermineOperationId(eventData.CorrelationId); diff --git a/src/Arcus.Messaging.EventHubs.Core/Extensions/EventHubProducerClientExtensions.cs b/src/Arcus.Messaging.EventHubs.Core/Extensions/EventHubProducerClientExtensions.cs index 3e5c7a61..195f15c2 100644 --- a/src/Arcus.Messaging.EventHubs.Core/Extensions/EventHubProducerClientExtensions.cs +++ b/src/Arcus.Messaging.EventHubs.Core/Extensions/EventHubProducerClientExtensions.cs @@ -4,11 +4,9 @@ using System.Runtime.Serialization; using System.Threading; using System.Threading.Tasks; -using Arcus.Messaging.Abstractions; using Arcus.Messaging.EventHubs.Core; using Arcus.Observability.Correlation; using Arcus.Observability.Telemetry.Core; -using GuardNet; using Microsoft.Extensions.Logging; // ReSharper disable once CheckNamespace @@ -55,13 +53,6 @@ public static async Task SendAsync( ILogger logger, CancellationToken cancellationToken = default(CancellationToken)) { - Guard.NotNull(client, nameof(client), "Requires an Azure EventHubs producer client while sending a correlated message"); - Guard.NotNull(eventBatch, nameof(eventBatch), "Requires a series of Azure EventHubs messages to send as correlated messages"); - Guard.NotNull(correlationInfo, nameof(correlationInfo), "Requires a message correlation instance to include the transaction ID in the send out messages"); - Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track the Azure EventHubs dependency while sending the correlated messages"); - Guard.NotAny(eventBatch, nameof(eventBatch), "Requires at least a single message to send to Azure EventHubs"); - Guard.For(() => eventBatch.Any(message => message is null), new ArgumentException("Requires non-null items in Azure EventHubs message sequence", nameof(eventBatch))); - await SendAsync(client, eventBatch, correlationInfo, logger, sendEventOptions: null, configureOptions: null, cancellationToken); } @@ -103,13 +94,6 @@ public static async Task SendAsync( Action configureOptions, CancellationToken cancellationToken = default(CancellationToken)) { - Guard.NotNull(client, nameof(client), "Requires an Azure EventHubs producer client while sending a correlated message"); - Guard.NotNull(eventBatch, nameof(eventBatch), "Requires a series of Azure EventHubs messages to send as correlated messages"); - Guard.NotNull(correlationInfo, nameof(correlationInfo), "Requires a message correlation instance to include the transaction ID in the send out messages"); - Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track the Azure EventHubs dependency while sending the correlated messages"); - Guard.NotAny(eventBatch, nameof(eventBatch), "Requires at least a single message to send to Azure EventHubs"); - Guard.For(() => eventBatch.Any(message => message is null), new ArgumentException("Requires non-null items in Azure EventHubs message sequence", nameof(eventBatch))); - await SendAsync(client, eventBatch, correlationInfo, logger, sendEventOptions: null, configureOptions, cancellationToken); } @@ -152,13 +136,6 @@ public static async Task SendAsync( SendEventOptions sendEventOptions, CancellationToken cancellationToken = default(CancellationToken)) { - Guard.NotNull(client, nameof(client), "Requires an Azure EventHubs producer client while sending a correlated message"); - Guard.NotNull(eventBatch, nameof(eventBatch), "Requires a series of Azure EventHubs messages to send as correlated messages"); - Guard.NotNull(correlationInfo, nameof(correlationInfo), "Requires a message correlation instance to include the transaction ID in the send out messages"); - Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track the Azure EventHubs dependency while sending the correlated messages"); - Guard.NotAny(eventBatch, nameof(eventBatch), "Requires at least a single message to send to Azure EventHubs"); - Guard.For(() => eventBatch.Any(message => message is null), new ArgumentException("Requires non-null items in Azure EventHubs message sequence", nameof(eventBatch))); - await SendAsync(client, eventBatch, correlationInfo, logger, sendEventOptions, configureOptions: null, cancellationToken); } @@ -203,7 +180,9 @@ public static async Task SendAsync( Action configureOptions, CancellationToken cancellationToken = default(CancellationToken)) { - EventData[] eventMessages = eventBatch.Select(ev => EventDataBuilder.CreateForBody(ev).Build()).ToArray(); + object[] batchArr = eventBatch?.ToArray() ?? throw new ArgumentNullException(nameof(eventBatch)); + EventData[] eventMessages = batchArr.Select(ev => EventDataBuilder.CreateForBody(ev).Build()).ToArray(); + await SendAsync(client, eventMessages, correlationInfo, logger, sendEventOptions, configureOptions, cancellationToken); } @@ -243,13 +222,6 @@ public static async Task SendAsync( ILogger logger, CancellationToken cancellationToken = default(CancellationToken)) { - Guard.NotNull(client, nameof(client), "Requires an Azure EventHubs producer client while sending a correlated message"); - Guard.NotNull(eventBatch, nameof(eventBatch), "Requires a series of Azure EventHubs messages to send as correlated messages"); - Guard.NotNull(correlationInfo, nameof(correlationInfo), "Requires a message correlation instance to include the transaction ID in the send out messages"); - Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track the Azure EventHubs dependency while sending the correlated messages"); - Guard.NotAny(eventBatch, nameof(eventBatch), "Requires at least a single message to send to Azure EventHubs"); - Guard.For(() => eventBatch.Any(message => message is null), new ArgumentException("Requires non-null items in Azure EventHubs message sequence", nameof(eventBatch))); - await SendAsync(client, eventBatch, correlationInfo, logger, sendEventOptions: null, configureOptions: null, cancellationToken); } @@ -291,13 +263,6 @@ public static async Task SendAsync( Action configureOptions, CancellationToken cancellationToken = default(CancellationToken)) { - Guard.NotNull(client, nameof(client), "Requires an Azure EventHubs producer client while sending a correlated message"); - Guard.NotNull(eventBatch, nameof(eventBatch), "Requires a series of Azure EventHubs messages to send as correlated messages"); - Guard.NotNull(correlationInfo, nameof(correlationInfo), "Requires a message correlation instance to include the transaction ID in the send out messages"); - Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track the Azure EventHubs dependency while sending the correlated messages"); - Guard.NotAny(eventBatch, nameof(eventBatch), "Requires at least a single message to send to Azure EventHubs"); - Guard.For(() => eventBatch.Any(message => message is null), new ArgumentException("Requires non-null items in Azure EventHubs message sequence", nameof(eventBatch))); - await SendAsync(client, eventBatch, correlationInfo, logger, sendEventOptions: null, configureOptions, cancellationToken); } @@ -340,13 +305,6 @@ public static async Task SendAsync( SendEventOptions sendEventOptions, CancellationToken cancellationToken = default(CancellationToken)) { - Guard.NotNull(client, nameof(client), "Requires an Azure EventHubs producer client while sending a correlated message"); - Guard.NotNull(eventBatch, nameof(eventBatch), "Requires a series of Azure EventHubs messages to send as correlated messages"); - Guard.NotNull(correlationInfo, nameof(correlationInfo), "Requires a message correlation instance to include the transaction ID in the send out messages"); - Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track the Azure EventHubs dependency while sending the correlated messages"); - Guard.NotAny(eventBatch, nameof(eventBatch), "Requires at least a single message to send to Azure EventHubs"); - Guard.For(() => eventBatch.Any(message => message is null), new ArgumentException("Requires non-null items in Azure EventHubs message sequence", nameof(eventBatch))); - await SendAsync(client, eventBatch, correlationInfo, logger, sendEventOptions, configureOptions: null, cancellationToken); } @@ -383,7 +341,7 @@ public static async Task SendAsync( /// that is an unsupported type for serialization. See the remarks for details. /// public static async Task SendAsync( - this EventHubProducerClient client, + this EventHubProducerClient client, IEnumerable eventBatch, CorrelationInfo correlationInfo, ILogger logger, @@ -391,12 +349,20 @@ public static async Task SendAsync( Action configureOptions, CancellationToken cancellationToken = default(CancellationToken)) { - Guard.NotNull(client, nameof(client), "Requires an Azure EventHubs producer client while sending a correlated message"); - Guard.NotNull(eventBatch, nameof(eventBatch), "Requires a series of Azure EventHubs messages to send as correlated messages"); - Guard.NotNull(correlationInfo, nameof(correlationInfo), "Requires a message correlation instance to include the transaction ID in the send out messages"); - Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track the Azure EventHubs dependency while sending the correlated messages"); - Guard.NotAny(eventBatch, nameof(eventBatch), "Requires at least a single message to send to Azure EventHubs"); - Guard.For(() => eventBatch.Any(message => message is null), new ArgumentException("Requires non-null items in Azure EventHubs message sequence", nameof(eventBatch))); + if (client is null) + { + throw new ArgumentNullException(nameof(client)); + } + + if (eventBatch is null) + { + throw new ArgumentNullException(nameof(eventBatch)); + } + + if (correlationInfo is null) + { + throw new ArgumentNullException(nameof(correlationInfo)); + } var options = new EventHubProducerClientMessageCorrelationOptions(); configureOptions?.Invoke(options); diff --git a/src/Arcus.Messaging.Pumps.EventHubs/AzureEventHubsMessagePump.cs b/src/Arcus.Messaging.Pumps.EventHubs/AzureEventHubsMessagePump.cs index 446a60d9..37d7b732 100644 --- a/src/Arcus.Messaging.Pumps.EventHubs/AzureEventHubsMessagePump.cs +++ b/src/Arcus.Messaging.Pumps.EventHubs/AzureEventHubsMessagePump.cs @@ -9,7 +9,6 @@ using Arcus.Messaging.Pumps.EventHubs.Configuration; using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Processor; -using GuardNet; using Microsoft.ApplicationInsights; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -47,18 +46,12 @@ internal AzureEventHubsMessagePump( IConfiguration applicationConfiguration, IServiceProvider serviceProvider, IAzureEventHubsMessageRouter messageRouter, - ILogger logger) + ILogger logger) : base(applicationConfiguration, serviceProvider, logger) { - Guard.NotNull(eventHubsConfiguration, nameof(eventHubsConfiguration), "Requires an Azure EventHubs configuration instance to setup the interaction with the Azure EventHubs when consuming event messages"); - Guard.NotNull(applicationConfiguration, nameof(applicationConfiguration), "Requires an application configuration instance to retrieve additional information for the message pump"); - Guard.NotNull(serviceProvider, nameof(serviceProvider), "Requires an application's service provider to retrieve registered services during the lifetime of the message pump, like Azure EventHubs message handlers and its dependencies"); - Guard.NotNull(messageRouter, nameof(messageRouter), "Requires an Azure EventHubs message router when consuming event messages from Azure EventHubs and routing them though Azure EventHubs message handlers"); - Guard.NotNull(logger, nameof(logger), "Requires a logger instance to write diagnostic messages during the lifetime of the message pump"); - - _eventHubsConfig = eventHubsConfiguration; - _messageRouter = messageRouter; - + _eventHubsConfig = eventHubsConfiguration ?? throw new ArgumentNullException(nameof(eventHubsConfiguration)); + _messageRouter = messageRouter ?? throw new ArgumentNullException(nameof(messageRouter)); + JobId = _eventHubsConfig.Options.JobId; _loggingScope = logger.BeginScope("Job: {JobId}", JobId); } @@ -86,7 +79,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } catch (Exception exception) { - Logger.LogCritical(exception, "Unexpected failure occurred during processing of messages in the Azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}': {Message}", JobId, ConsumerGroup, EventHubName, Namespace, exception.Message); + Logger.LogCritical(exception, "Unexpected failure occurred during processing of messages in the Azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}': {Message}", JobId, ConsumerGroup, EventHubName, Namespace, exception.Message); } finally { @@ -119,7 +112,7 @@ public override async Task StartProcessingMessagesAsync(CancellationToken stoppi _eventProcessor = await _eventHubsConfig.CreateEventProcessorClientAsync(); _eventProcessor.ProcessEventAsync += ProcessMessageAsync; _eventProcessor.ProcessErrorAsync += ProcessErrorAsync; - + Logger.LogTrace("Starting Azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}'", JobId, ConsumerGroup, EventHubName, Namespace); await _eventProcessor.StartProcessingAsync(stoppingToken); Logger.LogInformation("Azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}' started: {Time}", JobId, ConsumerGroup, EventHubName, Namespace, DateTimeOffset.UtcNow); @@ -136,7 +129,7 @@ private async Task ProcessMessageAsync(ProcessEventArgs args) if (_isHostShuttingDown) { - Logger.LogWarning("Abandoning message with ID '{MessageId}' as the Azure EventHubs message pump is shutting down", args.Data.MessageId); + Logger.LogWarning("Abandoning message with ID '{MessageId}' as the Azure EventHubs message pump is shutting down", args.Data.MessageId); return; } @@ -199,20 +192,20 @@ public override async Task StopProcessingMessagesAsync(CancellationToken cancell try { - Logger.LogTrace("Stopping Azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}'", JobId, ConsumerGroup, EventHubName , Namespace); + Logger.LogTrace("Stopping Azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}'", JobId, ConsumerGroup, EventHubName, Namespace); if (_eventProcessor != null) { await _eventProcessor.StopProcessingAsync(cancellationToken); _eventProcessor.ProcessEventAsync -= ProcessMessageAsync; - _eventProcessor.ProcessErrorAsync -= ProcessErrorAsync; + _eventProcessor.ProcessErrorAsync -= ProcessErrorAsync; } - Logger.LogInformation("Azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}' stopped: {Time}", JobId, ConsumerGroup, EventHubName , Namespace, DateTimeOffset.UtcNow); + Logger.LogInformation("Azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}' stopped: {Time}", JobId, ConsumerGroup, EventHubName, Namespace, DateTimeOffset.UtcNow); } catch (Exception exception) { - Logger.LogWarning(exception, "Cannot correctly close the azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}': {Message}", JobId, ConsumerGroup, EventHubName , Namespace, exception.Message); + Logger.LogWarning(exception, "Cannot correctly close the azure EventHubs message pump '{JobId}' on '{ConsumerGroup}/{EventHubsName}' in '{Namespace}': {Message}", JobId, ConsumerGroup, EventHubName, Namespace, exception.Message); } } diff --git a/src/Arcus.Messaging.Pumps.EventHubs/Configuration/AzureEventHubsMessagePumpConfig.cs b/src/Arcus.Messaging.Pumps.EventHubs/Configuration/AzureEventHubsMessagePumpConfig.cs index 1605eaff..6a40e12a 100644 --- a/src/Arcus.Messaging.Pumps.EventHubs/Configuration/AzureEventHubsMessagePumpConfig.cs +++ b/src/Arcus.Messaging.Pumps.EventHubs/Configuration/AzureEventHubsMessagePumpConfig.cs @@ -4,7 +4,6 @@ using Azure.Core; using Azure.Messaging.EventHubs; using Azure.Storage.Blobs; -using GuardNet; namespace Arcus.Messaging.Pumps.EventHubs.Configuration { @@ -52,19 +51,42 @@ private AzureEventHubsMessagePumpConfig( /// /// Thrown when the or the is null. internal static AzureEventHubsMessagePumpConfig CreateByConnectionString( - string eventHubsName, - string eventHubsConnectionStringSecretName, - string blobContainerName, + string eventHubsName, + string eventHubsConnectionStringSecretName, + string blobContainerName, string storageAccountConnectionStringSecretName, ISecretProvider secretProvider, AzureEventHubsMessagePumpOptions options) { - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsConnectionStringSecretName, nameof(eventHubsConnectionStringSecretName), "Requires a non-blank secret name to retrieve the connection string to the Azure EventHubs where the message pump will retrieve its event messages"); - Guard.NotNullOrWhitespace(blobContainerName, nameof(blobContainerName), "Requires a non-blank Azure Blob storage container name to store event checkpoints and load balance the consumed event messages send to the message pump"); - Guard.NotNullOrWhitespace(storageAccountConnectionStringSecretName, nameof(storageAccountConnectionStringSecretName), "Requires a non-blank secret name to retrieve the connection string to the Azure Blob storage where the event checkpoints will be stored and events will be load balanced during the event processing of the message pump"); - Guard.NotNull(secretProvider, nameof(secretProvider), "Requires an application's service provider to retrieve registered services during the lifetime of the message pump, like Azure EventHubs message handlers and its dependencies"); - Guard.NotNull(options, nameof(options), "Requires a set of user-defined options to influence the behavior of the Azure EventHubs message pump"); + if (string.IsNullOrWhiteSpace(eventHubsName)) + { + throw new ArgumentException("Requires a non-blank Azure Event hubs name to add a message pump", nameof(eventHubsName)); + } + + if (string.IsNullOrWhiteSpace(eventHubsConnectionStringSecretName)) + { + throw new ArgumentException("Requires a non-blank secret name that points to an Azure Event Hubs connection string", nameof(eventHubsConnectionStringSecretName)); + } + + if (string.IsNullOrWhiteSpace(blobContainerName)) + { + throw new ArgumentException("Requires a non-blank name for the Azure Blob container name, linked to the Azure Event Hubs", nameof(blobContainerName)); + } + + if (string.IsNullOrWhiteSpace(storageAccountConnectionStringSecretName)) + { + throw new ArgumentException("Requires a non-blank secret name that points to an Azure Blob storage connection string", nameof(storageAccountConnectionStringSecretName)); + } + + if (secretProvider is null) + { + throw new ArgumentNullException(nameof(secretProvider)); + } + + if (options is null) + { + throw new ArgumentNullException(nameof(options)); + } return new AzureEventHubsMessagePumpConfig(async () => { @@ -80,7 +102,7 @@ internal static AzureEventHubsMessagePumpConfig CreateByConnectionString( private static async Task GetConnectionStringFromSecretAsync( ISecretProvider secretProvider, - string connectionStringSecretName, + string connectionStringSecretName, string connectionStringType) { Task getConnectionStringTask = secretProvider.GetRawSecretAsync(connectionStringSecretName); @@ -115,12 +137,31 @@ internal static AzureEventHubsMessagePumpConfig CreateByTokenCredential( TokenCredential credential, AzureEventHubsMessagePumpOptions options) { - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(fullyQualifiedNamespace, nameof(fullyQualifiedNamespace), "Requires a non-blank Azure EventHubs namespace to connect to"); - Guard.For(() => !blobContainerUri.IsAbsoluteUri, "Requires a valid absolute URI endpoint for the Azure Blob container to store event checkpoints and load balance the consumed event messages send to the message pump"); - Guard.NotNull(credential, nameof(credential), "Requires a token credential to authenticate the interaction with the Azure EventHubs resource"); - Guard.NotNull(options, nameof(options), "Requires a set of user-defined options to influence the behavior of the Azure EventHubs message pump"); + + if (string.IsNullOrWhiteSpace(eventHubsName)) + { + throw new ArgumentException("Requires a non-blank Azure Event hubs name to add a message pump config", nameof(eventHubsName)); + } + + if (string.IsNullOrWhiteSpace(fullyQualifiedNamespace)) + { + throw new ArgumentException("Requires a non-blank Azure Event hubs fully-qualified namespace to add a message pump config", nameof(eventHubsName)); + } + + if (!blobContainerUri.IsAbsoluteUri) + { + throw new UriFormatException("Requires a valid absolute URI endpoint for the Azure Blob container to store event checkpoints and load balance the consumed event messages send to the message pump"); + } + + if (credential is null) + { + throw new ArgumentNullException(nameof(credential)); + } + + if (options is null) + { + throw new ArgumentNullException(nameof(options)); + } return new AzureEventHubsMessagePumpConfig(() => { diff --git a/src/Arcus.Messaging.Pumps.EventHubs/Extensions/IServiceCollectionExtensions.cs b/src/Arcus.Messaging.Pumps.EventHubs/Extensions/IServiceCollectionExtensions.cs index 8e261d7d..04c54fab 100644 --- a/src/Arcus.Messaging.Pumps.EventHubs/Extensions/IServiceCollectionExtensions.cs +++ b/src/Arcus.Messaging.Pumps.EventHubs/Extensions/IServiceCollectionExtensions.cs @@ -5,7 +5,6 @@ using Arcus.Security.Core; using Arcus.Security.Core.Caching; using Azure.Identity; -using GuardNet; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -51,13 +50,8 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessagePump( string blobContainerName, string storageAccountConnectionStringSecretName) { - Guard.NotNull(services, nameof(services), "Requires a set of services to add the Azure EventHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsConnectionStringSecretName, nameof(eventHubsConnectionStringSecretName), "Requires a non-blank secret name to retrieve the connection string to the Azure EventHubs where the message pump will retrieve its event messages"); - Guard.NotNullOrWhitespace(blobContainerName, nameof(blobContainerName), "Requires a non-blank Azure Blob storage container name to store event checkpoints and load balance the consumed event messages send to the message pump"); - Guard.NotNullOrWhitespace(storageAccountConnectionStringSecretName, nameof(storageAccountConnectionStringSecretName), "Requires a non-blank secret name to retrieve the connection string to the Azure Blob storage where the event checkpoints will be stored and events will be load balanced during the event processing of the message pump"); - - return services.AddEventHubsMessagePump( + return AddEventHubsMessagePump( + services, eventHubsName: eventHubsName, eventHubsConnectionStringSecretName: eventHubsConnectionStringSecretName, blobContainerName: blobContainerName, @@ -98,19 +92,33 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessagePump( string storageAccountConnectionStringSecretName, Action configureOptions) { - Guard.NotNull(services, nameof(services), "Requires a set of services to add the Azure EventHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsConnectionStringSecretName, nameof(eventHubsConnectionStringSecretName), "Requires a non-blank secret name to retrieve the connection string to the Azure EventHubs where the message pump will retrieve its event messages"); - Guard.NotNullOrWhitespace(blobContainerName, nameof(blobContainerName), "Requires a non-blank Azure Blob storage container name to store event checkpoints and load balance the consumed event messages send to the message pump"); - Guard.NotNullOrWhitespace(storageAccountConnectionStringSecretName, nameof(storageAccountConnectionStringSecretName), "Requires a non-blank secret name to retrieve the connection string to the Azure Blob storage where the event checkpoints will be stored and events will be load balanced during the event processing of the message pump"); + if (string.IsNullOrWhiteSpace(eventHubsName)) + { + throw new ArgumentException("Requires a non-blank Azure Event hubs name to add a message pump", nameof(eventHubsName)); + } + + if (string.IsNullOrWhiteSpace(eventHubsConnectionStringSecretName)) + { + throw new ArgumentException("Requires a non-blank secret name that points to an Azure Event Hubs connection string", nameof(eventHubsConnectionStringSecretName)); + } - return services.AddMessagePump((serviceProvider, options) => + if (string.IsNullOrWhiteSpace(blobContainerName)) + { + throw new ArgumentException("Requires a non-blank name for the Azure Blob container name, linked to the Azure Event Hubs", nameof(blobContainerName)); + } + + if (string.IsNullOrWhiteSpace(storageAccountConnectionStringSecretName)) + { + throw new ArgumentException("Requires a non-blank secret name that points to an Azure Blob storage connection string", nameof(storageAccountConnectionStringSecretName)); + } + + return AddMessagePump(services, (serviceProvider, options) => { ISecretProvider secretProvider = DetermineSecretProvider(serviceProvider); return AzureEventHubsMessagePumpConfig.CreateByConnectionString( - eventHubsName, eventHubsConnectionStringSecretName, - blobContainerName, storageAccountConnectionStringSecretName, + eventHubsName, eventHubsConnectionStringSecretName, + blobContainerName, storageAccountConnectionStringSecretName, secretProvider, options); }, configureOptions); @@ -139,13 +147,8 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessagePumpUsingMana string fullyQualifiedNamespace, string blobContainerUri) { - Guard.NotNull(services, nameof(services), "Requires a set of services to add the Azure EventHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(fullyQualifiedNamespace, nameof(fullyQualifiedNamespace), "Requires a non-blank Azure EventHubs namespace to connect to"); - Guard.NotNullOrWhitespace(blobContainerUri, nameof(blobContainerUri), "Requires a non-blank Azure Blob storage container endpoint to store event checkpoints and load balance the consumed event messages send to the message pump"); - Guard.For(() => !Uri.IsWellFormedUriString(blobContainerUri, UriKind.Absolute), "Requires a valid absolute URI endpoint for the Azure Blob container to store event checkpoints and load balance the consumed event messages send to the message pump"); - - return services.AddEventHubsMessagePumpUsingManagedIdentity( + return AddEventHubsMessagePumpUsingManagedIdentity( + services, eventHubsName: eventHubsName, fullyQualifiedNamespace: fullyQualifiedNamespace, blobContainerUri: blobContainerUri, @@ -177,13 +180,8 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessagePumpUsingMana string blobContainerUri, Action configureOptions) { - Guard.NotNull(services, nameof(services), "Requires a set of services to add the Azure EventHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(fullyQualifiedNamespace, nameof(fullyQualifiedNamespace), "Requires a non-blank Azure EventHubs namespace to connect to"); - Guard.NotNullOrWhitespace(blobContainerUri, nameof(blobContainerUri), "Requires a non-blank Azure Blob storage container endpoint to store event checkpoints and load balance the consumed event messages send to the message pump"); - Guard.For(() => !Uri.IsWellFormedUriString(blobContainerUri, UriKind.Absolute), "Requires a valid absolute URI endpoint for the Azure Blob container to store event checkpoints and load balance the consumed event messages send to the message pump"); - - return services.AddEventHubsMessagePumpUsingManagedIdentity( + return AddEventHubsMessagePumpUsingManagedIdentity( + services, eventHubsName: eventHubsName, fullyQualifiedNamespace: fullyQualifiedNamespace, blobContainerUri: blobContainerUri, @@ -191,7 +189,7 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessagePumpUsingMana configureOptions: configureOptions); } - /// + /// /// Adds a message pump to consume events from Azure EventHubs. /// /// The collection of services to add the message pump to. @@ -219,13 +217,8 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessagePumpUsingMana string blobContainerUri, string clientId) { - Guard.NotNull(services, nameof(services), "Requires a set of services to add the Azure EventHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(fullyQualifiedNamespace, nameof(fullyQualifiedNamespace), "Requires a non-blank Azure EventHubs namespace to connect to"); - Guard.NotNullOrWhitespace(blobContainerUri, nameof(blobContainerUri), "Requires a non-blank Azure Blob storage container endpoint to store event checkpoints and load balance the consumed event messages send to the message pump"); - Guard.For(() => !Uri.IsWellFormedUriString(blobContainerUri, UriKind.Absolute), "Requires a valid absolute URI endpoint for the Azure Blob container to store event checkpoints and load balance the consumed event messages send to the message pump"); - - return services.AddEventHubsMessagePumpUsingManagedIdentity( + return AddEventHubsMessagePumpUsingManagedIdentity( + services, eventHubsName: eventHubsName, fullyQualifiedNamespace: fullyQualifiedNamespace, blobContainerUri: blobContainerUri, @@ -263,20 +256,34 @@ public static EventHubsMessageHandlerCollection AddEventHubsMessagePumpUsingMana string clientId, Action configureOptions) { - Guard.NotNull(services, nameof(services), "Requires a set of services to add the Azure EventHubs message pump"); - Guard.NotNullOrWhitespace(eventHubsName, nameof(eventHubsName), "Requires a non-blank Azure EventHubs name where the events will be sent to when adding an Azure EvenHubs message pump"); - Guard.NotNullOrWhitespace(fullyQualifiedNamespace, nameof(fullyQualifiedNamespace), "Requires a non-blank Azure EventHubs namespace to connect to"); - Guard.NotNullOrWhitespace(blobContainerUri, nameof(blobContainerUri), "Requires a non-blank Azure Blob storage container endpoint to store event checkpoints and load balance the consumed event messages send to the message pump"); - Guard.For(() => !Uri.IsWellFormedUriString(blobContainerUri, UriKind.Absolute), "Requires a valid absolute URI endpoint for the Azure Blob container to store event checkpoints and load balance the consumed event messages send to the message pump"); + if (string.IsNullOrWhiteSpace(eventHubsName)) + { + throw new ArgumentException("Requires a non-blank Azure Event hubs name to add a message pump", nameof(eventHubsName)); + } + + if (string.IsNullOrWhiteSpace(fullyQualifiedNamespace)) + { + throw new ArgumentException("Requires a non-blank Azure Event hubs fully-qualified namespace to add a message pump", nameof(eventHubsName)); + } + + if (string.IsNullOrWhiteSpace(blobContainerUri)) + { + throw new ArgumentException("Requires a non-blank Azure Blob storage container endpoint to store event checkpoints and load balance the consumed event messages send to the message pump", nameof(blobContainerUri)); + } - return services.AddMessagePump((_, options) => + if (!Uri.IsWellFormedUriString(blobContainerUri, UriKind.Absolute)) { - return AzureEventHubsMessagePumpConfig.CreateByTokenCredential( - eventHubsName, - fullyQualifiedNamespace, - new Uri(blobContainerUri), - new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = clientId }), - options); + throw new UriFormatException("Requires a valid absolute URI endpoint for the Azure Blob container to store event checkpoints and load balance the consumed event messages send to the message pump"); + } + + return AddMessagePump(services, (_, options) => + { + return AzureEventHubsMessagePumpConfig.CreateByTokenCredential( + eventHubsName, + fullyQualifiedNamespace, + new Uri(blobContainerUri), + new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = clientId }), + options); }, configureOptions); } @@ -302,7 +309,7 @@ private static AzureEventHubsMessagePumpOptions CreateOptions(Action