Add props to Message from Service Bus events (ServiceBusSender.Send / ServiceBusProcessor.ProcessMessage)

Anisha KC 105 Reputation points Microsoft Employee
2023-09-08T18:19:45.92+00:00

In the older service bus package (Microsoft.Azure.ServiceBus), we could grab the message from an Event (https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-end-to-end-tracing?tabs=net-standard-sdk)

For example, we could listen for Event named Microsoft.Azure.ServiceBus.Send.Start and as part of the payload, there would be a List of Messages being sent.

How can we do the same with the new package (Azure.Messaging.ServiceBus)? I looked at this link but couldn't find anything: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-end-to-end-tracing?tabs=net-standard-sdk-2

For additional context, I am trying to get the message before it is sent and add some values to the User Properties (or with the new package, it would be Application properties)

Some sample of our code with the old package as an example/for additional context:

const string SERVICE_BUS_STOP_PROCESSING_EVENT = "Microsoft.Azure.ServiceBus.Process.Start";
const string SERVICE_BUS_START_SENDING_EVENT = "Microsoft.Azure.ServiceBus.Send.Start";

public void OnNext(KeyValuePair<string, object> value)
{
    switch (value.Key)
    {
        case SERVICE_BUS_STOP_PROCESSING_EVENT:
            {
                var payload = new EventPayload();
                try
                {
                    // As part of the payload the Messages would be in the form of Message[] or List<Message>
                    var message = payload.FetchPayloadProperty<Message>(value.Key, "Message", value.Value);
					// Our logic for adding user properties to the message
                }
                catch (Exception e)
                {
                    // ...
                }
            }
            break;
        case SERVICE_BUS_START_SENDING_EVENT:
            {
                var payload = new EventPayload();
                messageArray = payload.FetchPayloadProperty<Message[]>(value.Key, "Messages", value.Value);
                // Our logic for adding user properties to the message
            }
            break;
    }
}

Thank you!

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
623 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mike Urnun 9,811 Reputation points Microsoft Employee
    2023-09-28T18:57:45.5366667+00:00

    Hello @Anisha KC Sorry for the late reply. Were you able to overcome this? If still facing the same question, could you clarify if you were referring to how the activity baggage no longer flows through the UserProperties which became ApplicationProperties in the new package as described in the following migration doc?

    If just updating the ApplicationProperties, here's a sample: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/TopicFilters/Program.cs#L171

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.