public async Task WithServiceBusMessageHandler_WithContextFilterWithMessageBodySerializerWithMessageBodyFilter_UsesAll(bool matchesContext, bool matchesBody) { // Arrange var collection = new ServiceBusMessageHandlerCollection(new ServiceCollection()); var expectedBody = $"test-message-body-{Guid.NewGuid()}"; var expectedMessage = new TestMessage(); var expectedContext = AzureServiceBusMessageContextFactory.Generate(); var serializer = new TestMessageBodySerializer(expectedBody, expectedMessage); // Act collection.WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>( messageContextFilter: context => { Assert.Same(expectedContext, context); return(matchesContext); }, messageBodySerializer: serializer, messageBodyFilter: body => { Assert.Same(expectedMessage, body); return(matchesBody); }); // Assert IServiceProvider provider = collection.Services.BuildServiceProvider(); IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(provider, NullLogger.Instance); Assert.NotNull(handlers); MessageHandler handler = Assert.Single(handlers); Assert.NotNull(handler); Assert.Equal(matchesBody, handler.CanProcessMessageBasedOnMessage(expectedMessage)); Assert.Equal(matchesContext, handler.CanProcessMessageBasedOnContext(expectedContext)); MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody); Assert.NotNull(result); Assert.Same(expectedMessage, result.DeserializedMessage); }
public async Task WithMessageHandler_WithMessageBodySerializerImplementationFactoryWithMessageBodyFilterWithMessageHandlerImplementationFactory_UsesSerializer(bool matches) { // Arrange var collection = new ServiceBusMessageHandlerCollection(new ServiceCollection()); var expectedBody = $"test-message-body-{Guid.NewGuid()}"; var expectedMessage = new TestMessage(); var serializer = new TestMessageBodySerializer(expectedBody, expectedMessage); var expectedHandler = new TestServiceBusMessageHandler(); // Act collection.WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>( messageBodySerializerImplementationFactory: serviceProvider => serializer, messageBodyFilter: body => { Assert.Same(expectedMessage, body); return(matches); }, messageHandlerImplementationFactory: serviceProvider => expectedHandler); // Assert IServiceProvider provider = collection.Services.BuildServiceProvider(); IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(provider, NullLogger.Instance); Assert.NotNull(handlers); MessageHandler handler = Assert.Single(handlers); Assert.NotNull(handler); Assert.Same(expectedHandler, handler.GetMessageHandlerInstance()); bool actual = handler.CanProcessMessageBasedOnMessage(expectedMessage); Assert.Equal(matches, actual); MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody); Assert.NotNull(result); Assert.Same(expectedMessage, result.DeserializedMessage); }
public async Task WithMessageHandler_WithMessageBodySerializer_UsesSerializer() { // Arrange var collection = new ServiceBusMessageHandlerCollection(new ServiceCollection()); var expectedBody = $"test-message-body-{Guid.NewGuid()}"; var expectedMessage = new TestMessage(); var serializer = new TestMessageBodySerializer(expectedBody, expectedMessage); // Act collection.WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>(messageBodySerializer: serializer); // Assert IServiceProvider provider = collection.Services.BuildServiceProvider(); IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(provider, NullLogger.Instance); Assert.NotNull(handlers); MessageHandler handler = Assert.Single(handlers); Assert.NotNull(handler); MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody); Assert.NotNull(result); Assert.Same(expectedMessage, result.DeserializedMessage); }
/// <summary> /// Adds a <see cref="IAzureServiceBusMessageHandler{TMessage}" /> implementation to process the messages from 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> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="handlers"/> is <c>null</c>.</exception> public static ServiceBusMessageHandlerCollection WithServiceBusMessageHandler <TMessageHandler, TMessage>(this ServiceBusMessageHandlerCollection handlers) where TMessageHandler : class, IAzureServiceBusMessageHandler <TMessage> where TMessage : class { Guard.NotNull(handlers, nameof(handlers), "Requires a set of handlers to add the message handler"); handlers.Services.AddTransient <IMessageHandler <TMessage, AzureServiceBusMessageContext>, TMessageHandler>(); return(handlers); }