• Guides
  • Api Reference
  • FAQ
  • Guides
  • Triggers
  • Service Bus Triggers
  • Message Properties
Show / Hide Table of Contents
  • Overview
    • Introduction
    • Getting Started
    • Philosophy
    • FAQ
    • Upgrading to 1.0
  • Triggers
    • HTTP Triggers
      • Overview
      • Routing
      • Responses
      • Authorization
      • Claims Mapping
      • Headers
      • Swagger / OpenAPI
    • Service Bus Triggers
      • Queues
      • Topic and Subscriptions
      • Message Properties
      • Message Sessions
    • Cosmos DB
      • Overview
      • Error Handling
    • SignalR
    • Timer Triggers
    • Azure Storage Triggers
      • Queues
      • Blobs
    • Event Hub Triggers
  • Output Bindings
  • Cross Cutting Concerns
    • Connection Strings
    • IoC Containers
    • Logging
    • Trigger Contexts
    • Validation
    • Serialization
    • Suggested Solution Structure
  • Contributing
    • Compiling
    • Debugging

Service Bus Message Properties

Sometimes it can be useful to access the properties that accompany a Service Bus message, for example the dequeue count. To do that inject the IContextProvider interface into your handler and use the ServiceBusContext property to access the properties:

internal class SendEmailCommandHandler : ICommandHandler<SendEmailCommand>
{
    private readonly IContextProvider _contextProvider;

    public SendEmailCommandHandler(IContextProvider contextProvider)
    {
        _contextProvider = contextProvider;
    }

    public Task ExecuteAsync(SendEmailCommand command)
    {
        if (_contextProvider.ServiceBusContext.DeliveryCount >= 5)
        {
            // do something else
        }
        // We won't really send an email from this sample
        return Task.CompletedTask;
    }
}

Properties available include:

Property Description
DeliveryCount The number of times the message has been delivered
EnqueuedTimeUTc The date and time the message was enqueued
MessageId The ID of the message

Care should be taken when doing this as it removes one of the benefits of the mediator pattern - the abstraction between caller and handler and essentially tightly couples the handler to the service bus. An alternative approach that maintains the decoupling and separation of concerns would be to implement a decorated ICommandDispatcher (see the AzureFromTheTrenches.Commanding documentation).

  • Improve this Doc
  • 0 Comments
Back to top Copyright © 2018 James Randall