Using Azure ServiceBus Message properties on Azure Functions in .net 5

If you want to use .net 5 with Azure functions then there is quite a difference in comparance with .net 3.1. There are already a few articles on this:

I recently worked on a Azure Function that we wanted to develop with .net 5. The function listens to a Azure ServiceBus Topic and processes the message and sends the result on one or two next Topics.

public OutputToServiceBus Run(
    [ServiceBusTrigger("topic-in", "subscription", Connection = "ServiceBusConnectionString")] string message
     FunctionContext context)
  // Do some stuff
  // Return results to multiple topics using ServiceBusOutput binding
  return new OutputToServiceBus 
       OutputToTopicOut1 = new OutputToTopicOut1 { },        
OutputToTopicOut2 = new OutputToTopicOut2 { },   }

The connection to the inbound topic can be made using the ServiceBusTrigger attribute. In the sample code the function returns a instance of the class OutputToServiceBus. This class looks like this

public class OutputToServiceBus
  [ServiceBusOutput("topic-out-1", Connection = "ServiceBusConnectionString")]
  public OutputToTopicOut1 OutputToTopicOut1 { get; set; }
  [ServiceBusOutput("topic-out-2", Connection = "ServiceBusConnectionString")]
  public OutputToTopicOut2 OutputToTopicOut2 { get; set; }


The attribute ServiceBusOutput indicates that the data of the field where it is on, will be sent to the servicebus. It is even possible to sent messages to multiple topics. The object will be serialized as a json string and will be sent.

But the message is a string...

The incoming message is a string. In Azure Functions build with .net 3.1 this was a message object. So how can the correlationId, the userproperties be retrieved? The answer lies in the FunctionContext. This object contains this information, but there is more possible. You can directly consume some items as parameters like this:

public async Task<Message> Run( 
   [ServiceBusTrigger(queueName: "queueName", Connection = "AzureServiceBus")]
   string body,
   string correlationId,
   string messageId,
   string sessionId,
   IDictionary<string, object> userProperties,
   int deliveryCount,
   FunctionContext functionContext

Very easy and usefull. 

The correlationId

For me personal there is only one thing that would be a nice add on. I can get the correlationId of an incoming message, but I want to be able to pass this correlationId to the messages that I sent out with the Azure Function. For this I am waiting on a response on this Github question: Setting a correlationId on a message when using ServiceBusOutput · Issue #474 · Azure/azure-functions-dotnet-worker (