コード例 #1
0
        public async Task WithServiceBusRouting_WithMessageHandlerMessageBodySerializerSubType_DeserializesCustomMessage()
        {
            // Arrange
            var services       = new ServiceCollection();
            var collection     = new ServiceBusMessageHandlerCollection(services);
            var ignoredHandler = new StubServiceBusMessageHandler <TestMessage>();
            var spyHandler     = new StubServiceBusMessageHandler <Order>();

            var expectedMessage = new TestMessage {
                TestProperty = "Some value"
            };
            string expectedBody = JsonConvert.SerializeObject(expectedMessage);
            var    serializer   = new TestMessageBodySerializer(expectedBody, new SubOrder());

            collection.WithServiceBusMessageHandler <StubServiceBusMessageHandler <Order>, Order>(messageBodySerializer: serializer, implementationFactory: serviceProvider => spyHandler)
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <TestMessage>, TestMessage>(implementationFactory: serviceProvider => ignoredHandler);

            // Act
            services.AddServiceBusMessageRouting();

            // Assert
            IServiceProvider provider = services.BuildServiceProvider();
            var router = provider.GetRequiredService <IAzureServiceBusMessageRouter>();
            AzureServiceBusMessageContext context = AzureServiceBusMessageContextFactory.Generate();
            var correlationInfo = new MessageCorrelationInfo("operation-id", "transaction-id");

            ServiceBusReceivedMessage message = expectedMessage.AsServiceBusReceivedMessage();
            await router.RouteMessageAsync(message, context, correlationInfo, CancellationToken.None);

            Assert.True(spyHandler.IsProcessed);
            Assert.False(ignoredHandler.IsProcessed);
        }
        public async Task WithServiceBusRouting_WithMessageHandlerCanHandleAllFiltersAtOnce_AndStillFindsTheRightMessageHandler()
        {
            // Arrange
            var services        = new ServiceCollection();
            var collection      = new ServiceBusMessageHandlerCollection(services);
            var ignoredHandler1 = new StubServiceBusMessageHandler <TestMessage>();
            var ignoredHandler2 = new StubServiceBusMessageHandler <TestMessage>();
            var ignoredHandler3 = new StubServiceBusMessageHandler <Order>();
            var spyHandler      = new StubServiceBusMessageHandler <Order>();

            AzureServiceBusMessageContext context = AzureServiceBusMessageContextFactory.Generate();
            var expectedMessage = new TestMessage {
                TestProperty = "Some value"
            };
            string expectedBody = JsonConvert.SerializeObject(expectedMessage);
            var    serializer   = new TestMessageBodySerializer(expectedBody, OrderGenerator.Generate());

            collection
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <Order>, Order>(
                messageContextFilter: ctx => ctx != null,
                messageBodyFilter: body => body != null,
                messageBodySerializer: new TestMessageBodySerializer(expectedBody, new Customer()),
                implementationFactory: serviceProvider => ignoredHandler3)
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <TestMessage>, TestMessage>(
                messageBodyFilter: body => body is null,
                implementationFactory: serviceProvider => ignoredHandler2)
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <Order>, Order>(
                messageBodySerializer: serializer,
                messageBodyFilter: body => body.Customer != null,
                messageContextFilter: ctx => ctx.MessageId.StartsWith("message-id"),
                implementationFactory: serviceProvider => spyHandler)
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <TestMessage>, TestMessage>()
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <TestMessage>, TestMessage>(
                implementationFactory: serviceProvider => ignoredHandler1);

            // Act
            services.AddServiceBusMessageRouting();

            // Assert
            IServiceProvider provider = services.BuildServiceProvider();
            var router = provider.GetRequiredService <IAzureServiceBusMessageRouter>();

            var correlationInfo = new MessageCorrelationInfo("operation-id", "transaction-id");
            ServiceBusReceivedMessage message = expectedMessage.AsServiceBusReceivedMessage();
            await router.RouteMessageAsync(message, context, correlationInfo, CancellationToken.None);

            Assert.True(spyHandler.IsProcessed);
            Assert.False(ignoredHandler1.IsProcessed);
            Assert.False(ignoredHandler2.IsProcessed);
            Assert.False(ignoredHandler3.IsProcessed);
        }