2
votes

Is it possible to get the queue name in a service bus triggered Azure Function?

I know it is possible if the QueueName or TopicName+SubscriberName are hard coded. In that case I can make a const string and use it inside the function. However my service bus trigger gets the name from the settings using "%ServiceBusSettings:QueueName%", "%ServiceBusSettings:TopicName%" and "%ServiceBusSettings:SubscriberName%".

How to get the Queue or Topic+Subscriber name in this configurable case?

[FunctionName("topic-and-subscriber-function")]
public async Task Run(
    [ServiceBusTrigger("%ServiceBusSettings:TopicName%", "%ServiceBusSettings:SubscriptionName%", Connection = "ServiceBusSettings:ServiceBusConnection")] Message message,
    ILogger log, MessageReceiver messageReceiver)
{
    // Get TopicName and SubscriberName
}

[FunctionName("queue-function")]
public async Task Run(
    [ServiceBusTrigger("%ServiceBusSettings:QueueName%", Connection = "ServiceBusSettings:ServiceBusConnection")] Message message,
    ILogger log, MessageReceiver messageReceiver)
{
    // Get QueueName
}
1
@BassamGamal nope. this answer explains how to make it configurable. My example uses the approach described in this answer. However with this approach I don't know the queuename in the functionhwcverwe

1 Answers

0
votes

What you need to get is the name of the queue, which is a property of QueueClient, but Microsoft.Azure.ServiceBus.QueueClient itself cannot be serialized. The Microsoft.Azure.ServiceBus.Message can be serialized but does not have a queuename attribute. (And Microsoft.Azure.ServiceBus.Message has no parent class. This means that it is generally impossible to obtain queuename through this class.)

You can pass in queuename as part of the message, such as input json format message:

{
   "queuename":"testname",
   ...
}

then you can get the queuename in code.(Also You can get this value in bindings by this way. All key values passed in in json format can be obtained in the binding.)