A collection of useful service bus features for .NET isolated middleware.
Add the NuGet package to your project.
The abstract class ServiceBusMiddlewareBase supports the following methods:
BeforeInvocationAsync: executed before the invocation of theFunctionExecutionDelegate.AfterInvocationAsync: executed after the invocation of theFunctionExecutionDelegate, unless an exception is thrown.AlwaysAfterInvocation: executed after the invocation of theFunctionExecutionDelegate, regardless of an exception being thrown.OnExceptionAsync: executed when an exception is thrown during the invocation of theFunctionExecutionDelegate. Returntruewhen the exception is handled,falsewhen the exception should be rethrown.
The ExceptionInsightMiddleware adds exception details to a dead-lettered message:
- Dead letter reason
- Exception message
Please note: it is currently not possible to read the maximum delivery count setting of a queue or topic. Therefore, you have to configure the ExceptionInsightMiddlewareOptions manually
and ensure the maximum delivery count is set to a value equal or less than the service bus setting.
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(
workerApplication =>
{
workerApplication.UseExceptionInsightMiddleware();
})
// ...The ScheduledRetryMiddleware is an extension of the ExceptionInsightMiddleware, you should not register both middleware.
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(
workerApplication =>
{
workerApplication.UseScheduledRetryMiddleware();
})
// ...- MaxDeliveryCount: the maximum number of delivery attempts before a message is dead-lettered.
- BackoffMode: the backoff mode to use for rescheduling messages.
- BackoffInSeconds: the number of seconds to wait before rescheduling a message.
It's possible to use a custom IServiceBusClientProvider to provide a ServiceBusClient for sending messages:
services.AddScoped<IServiceBusClientProvider, MyServiceBusClientProvider>();When you want to resubmit a dead-lettered message manually, make sure the ScheduledRetryMiddleware.DeliveryAttempts property is removed.
Gets the ServiceBusTriggerAttribute from the function context:
var attribute = functionContext.GetServiceBusTrigger();Determines whether the function is a service bus trigger. Can be used when registering middleware:
functionsWorkerApplicationBuilder.UseWhen<MySerivceBusMiddleware>(context => context.IsServiceBusTrigger());- The
ExceptionInsightMiddlewareandScheduledRetryMiddlewareonly support a trigger with aServiceBusRecievedMessagebinding. A trigger-binding to astringis not supported.