/// <summary> /// Adds a <see cref="IMessageHandler{TMessage, TMessageContext}" /> implementation to process the messages from an Azure Service Bus. /// resources. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <typeparam name="TMessageContext">The type of the context in which the message handler will process the message.</typeparam> /// <param name="services">The collection of services to use in the application.</param> /// <param name="messageContextFilter">The function that determines if the message handler should handle the message based on the context.</param> /// <param name="messageBodySerializer">The custom <see cref="IMessageBodySerializer"/> to deserialize the incoming message for the <typeparamref name="TMessageHandler"/>.</param> /// <param name="messageBodyFilter">The filter to restrict the message processing based on the incoming message body.</param> /// <param name="implementationFactory">The function that creates the message handler.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="services"/>, <paramref name="messageBodyFilter"/>, or <paramref name="implementationFactory"/> is <c>null</c>.</exception> public static MessageHandlerCollection WithMessageHandler <TMessageHandler, TMessage, TMessageContext>( this MessageHandlerCollection services, Func <TMessageContext, bool> messageContextFilter, IMessageBodySerializer messageBodySerializer, Func <TMessage, bool> messageBodyFilter, Func <IServiceProvider, TMessageHandler> implementationFactory) where TMessageHandler : class, IMessageHandler <TMessage, TMessageContext> where TMessage : class where TMessageContext : MessageContext { Guard.NotNull(services, nameof(services), "Requires a set of services 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(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 services"); services.Services.AddTransient <IMessageHandler <TMessage, TMessageContext>, MessageHandlerRegistration <TMessage, TMessageContext> >( serviceProvider => new MessageHandlerRegistration <TMessage, TMessageContext>( messageContextFilter: messageContextFilter, messageBodySerializer: messageBodySerializer, messageBodyFilter: messageBodyFilter, messageHandlerImplementation: implementationFactory(serviceProvider), logger: serviceProvider.GetService <ILogger <MessageHandlerRegistration <TMessage, TMessageContext> > >())); return(services); }
/// <summary> /// Adds a <see cref="IMessageHandler{TMessage}" /> implementation to process the messages from an Azure Service Bus. /// resources. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <param name="services">The collection of services to use in the application.</param> /// <param name="messageBodySerializer">The custom <see cref="IMessageBodySerializer"/> to deserialize the incoming message for the <typeparamref name="TMessageHandler"/>.</param> public static MessageHandlerCollection WithMessageHandler <TMessageHandler, TMessage>( this MessageHandlerCollection services, IMessageBodySerializer messageBodySerializer) where TMessageHandler : class, IMessageHandler <TMessage, MessageContext> where TMessage : class { Guard.NotNull(services, nameof(services), "Requires a set of services 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"); return(services.WithMessageHandler <TMessageHandler, TMessage, MessageContext>(messageBodySerializer)); }
/// <summary> /// Adds a <see cref="IAzureServiceBusMessageHandler{TMessage}" /> implementation to process the messages from an Azure Service Bus. /// resources. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <param name="handlers">The collection of handlers to use in the application.</param> /// <param name="messageBodySerializer">The custom <see cref="IMessageBodySerializer"/> to deserialize the incoming message for the <see cref="IAzureServiceBusMessageHandler{TMessage}"/>.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="handlers"/>, <paramref name="messageBodySerializer"/> is <c>null</c>.</exception> public static ServiceBusMessageHandlerCollection WithServiceBusMessageHandler <TMessageHandler, TMessage>( this ServiceBusMessageHandlerCollection handlers, IMessageBodySerializer messageBodySerializer) where TMessageHandler : class, IAzureServiceBusMessageHandler <TMessage> 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"); handlers.WithMessageHandler <TMessageHandler, TMessage, AzureServiceBusMessageContext>(messageBodySerializer); return(handlers); }
/// <summary> /// Adds a <see cref="IMessageHandler{TMessage}" /> implementation to process the messages from an Azure Service Bus. /// resources. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <param name="services">The collection of services to use in the application.</param> /// <param name="messageBodyFilter">The filter to restrict the message processing based on the incoming message body.</param> /// <param name="messageBodySerializer">The custom <see cref="IMessageBodySerializer"/> to deserialize the incoming message for the <typeparamref name="TMessageHandler"/>.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="services"/> or <paramref name="messageBodyFilter"/> is <c>null</c>.</exception> public static MessageHandlerCollection WithMessageHandler <TMessageHandler, TMessage>( this MessageHandlerCollection services, IMessageBodySerializer messageBodySerializer, Func <TMessage, bool> messageBodyFilter) where TMessageHandler : class, IMessageHandler <TMessage, MessageContext> where TMessage : class { Guard.NotNull(services, nameof(services), "Requires a set of services 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"); return(WithMessageHandler <TMessageHandler, TMessage, MessageContext>(services, messageBodySerializer, messageBodyFilter)); }
/// <summary> /// Adds a <see cref="IMessageHandler{TMessage}" /> implementation to process the messages from an Azure Service Bus. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <param name="services">The collection of services to use in the application.</param> /// <param name="messageBodySerializer">The function to create the custom <see cref="IMessageBodySerializer"/> that deserializes the incoming message for the <see cref="IMessageHandler{TMessage,TMessageContext}"/>.</param> /// <param name="implementationFactory">The function that creates the service.</param> public static MessageHandlerCollection WithMessageHandler <TMessageHandler, TMessage>( this MessageHandlerCollection services, IMessageBodySerializer messageBodySerializer, Func <IServiceProvider, TMessageHandler> implementationFactory) where TMessageHandler : class, IMessageHandler <TMessage, MessageContext> where TMessage : class { Guard.NotNull(services, nameof(services), "Requires a set of services to add the message handler"); Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a function to create the message handler with dependent services"); Guard.NotNull(messageBodySerializer, nameof(messageBodySerializer), "Requires an custom message body serializer instance to deserialize incoming message for the message handler"); return(services.WithMessageHandler <TMessageHandler, TMessage, MessageContext>(messageBodySerializer, implementationFactory)); }
/// <summary> /// Adds a <see cref="IMessageHandler{TMessage}" /> implementation to process the messages from an Azure Service Bus. /// resources. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <typeparam name="TMessageContext">The type of the context in which the message handler will process the message.</typeparam> /// <param name="services">The collection of services to use in the application.</param> /// <param name="messageBodySerializer">The custom <see cref="IMessageBodySerializer"/> to deserialize the incoming message for the <typeparamref name="TMessageHandler"/>.</param> public static MessageHandlerCollection WithMessageHandler <TMessageHandler, TMessage, TMessageContext>( this MessageHandlerCollection services, IMessageBodySerializer messageBodySerializer) where TMessageHandler : class, IMessageHandler <TMessage, TMessageContext> where TMessage : class where TMessageContext : MessageContext { Guard.NotNull(services, nameof(services), "Requires a set of services 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"); return(services.WithMessageHandler <TMessageHandler, TMessage, TMessageContext>( messageBodySerializer, serviceProvider => ActivatorUtilities.CreateInstance <TMessageHandler>(serviceProvider))); }
/// <summary> /// Adds a <see cref="IAzureServiceBusMessageHandler{TMessage}" /> implementation to process the messages from an Azure Service Bus. /// resources. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <param name="handlers">The collection of handlers to use in the application.</param> /// <param name="messageContextFilter">The function that determines if the message handler should handle the message based on the context.</param> /// <param name="messageBodySerializer">The custom <see cref="IMessageBodySerializer"/> to deserialize the incoming message for the <see cref="IAzureServiceBusMessageHandler{TMessage}"/>.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="handlers"/> or <paramref name="messageContextFilter"/>, or <paramref name="messageBodySerializer"/> is <c>null</c>.</exception> public static ServiceBusMessageHandlerCollection WithServiceBusMessageHandler <TMessageHandler, TMessage>( this ServiceBusMessageHandlerCollection handlers, Func <AzureServiceBusMessageContext, bool> messageContextFilter, IMessageBodySerializer messageBodySerializer) where TMessageHandler : class, IAzureServiceBusMessageHandler <TMessage> 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"); handlers.WithMessageHandler <TMessageHandler, TMessage, AzureServiceBusMessageContext>(messageContextFilter, messageBodySerializer); return(handlers); }
/// <summary> /// Adds a <see cref="IAzureServiceBusMessageHandler{TMessage}" /> implementation to process the messages from an Azure Service Bus. /// resources. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <param name="handlers">The collection of handlers to use in the application.</param> /// <param name="messageBodySerializer">The custom <see cref="IMessageBodySerializer"/> to deserialize the incoming message for the <see cref="IAzureServiceBusMessageHandler{TMessage}"/>.</param> /// <param name="messageBodyFilter">The filter to restrict the message processing based on the incoming message body.</param> /// <param name="implementationFactory">The function that creates the message handler.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="handlers"/>, <paramref name="messageBodySerializer"/>, <paramref name="messageBodyFilter"/>, or <paramref name="implementationFactory"/> is <c>null</c>.</exception> public static ServiceBusMessageHandlerCollection WithServiceBusMessageHandler <TMessageHandler, TMessage>( this ServiceBusMessageHandlerCollection handlers, Func <TMessage, bool> messageBodyFilter, IMessageBodySerializer messageBodySerializer, Func <IServiceProvider, TMessageHandler> implementationFactory) where TMessageHandler : class, IAzureServiceBusMessageHandler <TMessage> 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"); handlers.WithMessageHandler <TMessageHandler, TMessage, AzureServiceBusMessageContext>(messageBodySerializer, messageBodyFilter, implementationFactory); return(handlers); }
/// <summary> /// Initializes a new instance of the <see cref="MessageHandlerRegistration{TMessage, TMessageContext}"/> class. /// </summary> /// <param name="messageContextFilter">The filter to determine if a given <see cref="MessageContext"/> can be handled by the <see cref="IMessageHandler{TMessage,TMessageContext}"/> implementation.</param> /// <param name="messageBodySerializer">The optional custom serializer that will deserialize the incoming message for the <paramref name="messageHandlerImplementation"/>.</param> /// <param name="messageBodyFilter">The filter to determine if a given message can be handled by the <see cref="IMessageHandler{TMessage,TMessageContext}"/> implementation.</param> /// <param name="messageHandlerImplementation">The <see cref="IMessageHandler{TMessage,TMessageContext}"/> implementation that this registration instance represents.</param> /// <param name="logger">The logger instance to write diagnostic trace messages during the restriction filters.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="messageHandlerImplementation"/> is <c>null</c>.</exception> internal MessageHandlerRegistration( Func <TMessageContext, bool> messageContextFilter, IMessageBodySerializer messageBodySerializer, Func <TMessage, bool> messageBodyFilter, IMessageHandler <TMessage, TMessageContext> messageHandlerImplementation, ILogger <MessageHandlerRegistration <TMessage, TMessageContext> > logger) { Guard.NotNull(messageHandlerImplementation, nameof(messageHandlerImplementation), "Requires a message handler implementation to apply the message processing filters to"); _messageContextFilter = messageContextFilter; _messageBodyFilter = messageBodyFilter; _logger = logger ?? NullLogger <MessageHandlerRegistration <TMessage, TMessageContext> > .Instance; Service = messageHandlerImplementation; Serializer = messageBodySerializer; }
/// <summary> /// Adds a <see cref="IMessageHandler{TMessage, TMessageContext}" /> implementation to process the messages from an Azure Service Bus. /// resources. /// </summary> /// <typeparam name="TMessageHandler">The type of the implementation.</typeparam> /// <typeparam name="TMessage">The type of the message that the message handler will process.</typeparam> /// <typeparam name="TMessageContext">The type of the context in which the message handler will process the message.</typeparam> /// <param name="services">The collection of services to use in the application.</param> /// <param name="messageContextFilter">The function that determines if the message handler should handle the message based on the context.</param> /// <param name="messageBodySerializer">The custom <see cref="IMessageBodySerializer"/> to deserialize the incoming message for the <typeparamref name="TMessageHandler"/>.</param> /// <param name="messageBodyFilter">The filter to restrict the message processing based on the incoming message body.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="services"/> or <paramref name="messageBodyFilter"/> is <c>null</c>.</exception> public static MessageHandlerCollection WithMessageHandler <TMessageHandler, TMessage, TMessageContext>( this MessageHandlerCollection services, Func <TMessageContext, bool> messageContextFilter, IMessageBodySerializer messageBodySerializer, Func <TMessage, bool> messageBodyFilter) where TMessageHandler : class, IMessageHandler <TMessage, TMessageContext> where TMessage : class where TMessageContext : MessageContext { Guard.NotNull(services, nameof(services), "Requires a set of services 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(messageBodyFilter, nameof(messageBodyFilter), "Requires a filter to restrict the message processing based on the incoming message body"); return(services.WithMessageHandler <TMessageHandler, TMessage, TMessageContext>( messageContextFilter, messageBodySerializer, messageBodyFilter, serviceProvider => ActivatorUtilities.CreateInstance <TMessageHandler>(serviceProvider))); }