public async Task ServiceBusQueueMessagePumpWithServiceBusDeadLetterOnFallback_PublishServiceBusMessage_MessageSuccessfullyProcessed() { // Arrange var config = TestConfig.Create(); string connectionString = config.GetServiceBusConnectionString(ServiceBusEntityType.Queue); var options = new WorkerOptions(); options.AddServiceBusQueueMessagePump( configuration => connectionString, opt => opt.AutoComplete = false) .WithServiceBusMessageHandler <ShipmentAzureServiceBusMessageHandler, Shipment>((AzureServiceBusMessageContext context) => true) .WithServiceBusFallbackMessageHandler <OrdersAzureServiceBusDeadLetterFallbackMessageHandler>(); Order order = OrderGenerator.Generate(); await using (var worker = await Worker.StartNewAsync(options)) await using (var service = await TestMessagePumpService.StartNewAsync(config, _logger)) { // Act await service.SendMessageToServiceBusAsync(connectionString, order.AsServiceBusMessage()); // Assert await service.AssertDeadLetterMessageAsync(connectionString); } }
private static void AddServiceBusMessagePump(WorkerOptions options, KeyRotationConfig rotationConfig, string jobId) { options.AddServiceBusQueueMessagePump(rotationConfig.KeyVault.SecretName, opt => { opt.JobId = jobId; // Unrealistic big maximum exception count so that we're certain that the message pump gets restarted based on the notification and not the unauthorized exception. opt.MaximumUnauthorizedExceptionsBeforeRestart = 1000; }).WithServiceBusMessageHandler <OrdersAzureServiceBusMessageHandler, Order>(); }
public async Task ServiceBusMessagePump_FailureDuringMessageHandling_TracksCorrelationInApplicationInsights() { // Arrange var config = TestConfig.Create(); string connectionString = config.GetServiceBusConnectionString(ServiceBusEntityType.Queue); var spySink = new InMemoryLogSink(); var options = new WorkerOptions(); options.Configure(host => host.UseSerilog((context, currentConfig) => { currentConfig .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.FromLogContext() .Enrich.WithVersion() .Enrich.WithComponentName("Service Bus Queue Worker") .WriteTo.Sink(spySink); })); options.AddServiceBusQueueMessagePump(configuration => connectionString, opt => opt.AutoComplete = true) .WithServiceBusMessageHandler <OrdersSabotageAzureServiceBusMessageHandler, Order>(); string operationId = $"operation-{Guid.NewGuid()}", transactionId = $"transaction-{Guid.NewGuid()}"; ServiceBusMessage orderMessage = OrderGenerator.Generate().AsServiceBusMessage(operationId, transactionId); await using (var worker = await Worker.StartNewAsync(options)) { await using (var service = await TestMessagePumpService.StartNewAsync(config, _logger)) { // Act await service.SendMessageToServiceBusAsync(connectionString, orderMessage); } // Assert RetryAssertUntilTelemetryShouldBeAvailable(() => { Assert.Contains(spySink.CurrentLogEmits, log => log.Exception?.InnerException?.Message.Contains("Sabotage") == true && log.ContainsProperty(ContextProperties.Correlation.OperationId, operationId) && log.ContainsProperty(ContextProperties.Correlation.TransactionId, transactionId)); }, timeout: TimeSpan.FromMinutes(1)); } }
public async Task ServiceBusQueueMessagePumpWithServiceBusDeadLetterAfterContextPredicate_PublishServiceBusMessage_MessageSuccessfullyProcessed() { // Arrange var config = TestConfig.Create(); string connectionString = config.GetServiceBusConnectionString(ServiceBusEntityType.Queue); var options = new WorkerOptions(); options.AddServiceBusQueueMessagePump(configuration => connectionString, opt => opt.AutoComplete = false) .WithServiceBusMessageHandler <CustomerMessageHandler, Customer>(context => context.Properties["Topic"].ToString() == "Customers") .WithServiceBusMessageHandler <OrdersAzureServiceBusDeadLetterMessageHandler, Order>(context => context.Properties["Topic"].ToString() == "Orders") .WithMessageHandler <PassThruOrderMessageHandler, Order, AzureServiceBusMessageContext>((AzureServiceBusMessageContext context) => false); Order order = OrderGenerator.Generate(); await using (var worker = await Worker.StartNewAsync(options)) await using (var service = await TestMessagePumpService.StartNewAsync(config, _logger)) { // Act await service.SendMessageToServiceBusAsync(connectionString, order.AsServiceBusMessage()); // Assert await service.AssertDeadLetterMessageAsync(connectionString); } }