What is the consistent approach with Azure Functions, Servicebus and CorrelationId?

Viewed 347

In my project, the architecture is quite simple: some Azure Functions triggered by Servicebus.

For example, I have an invoice process:

  1. create an invoice
  2. create a PDF of this invoice and take money
  3. mark an invoice as paid

Each step is an Azure Function. When the function finished to create the invoice (1), it sends a message in Servicebus to trigger the creation of the PDF for the invoice (2). Then, if the payment went through, the function will send a message to mark the invoice as paid.

I saw this code

using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace WebApi.CorrelationId.MessageHandlers
{
    public class AddCorrelationIdToResponseHandler : DelegatingHandler
    {
        private const string CorrelationIdHeaderName = "X-Correlation-Id";

        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var responseMessage = await base.SendAsync(request, cancellationToken);

            responseMessage
                .Headers
                .Add(CorrelationIdHeaderName, request.GetCorrelationId().ToString());

            return responseMessage;
        }
    }
}

Then, in the configuration code

var httpConfiguration = new HttpConfiguration();

// your regular configuration

httpConfiguration.MessageHandlers.Add(new AddCorrelationIdToResponseHandler());

I'm not sure if we have a valid HttpConfiguration in Azure Functions.

How can I track all steps with a CorrelationId?

1 Answers
Related