Santosh Jindal
Santosh Jindal

Reputation: 61

Is it possible to create 2 Azure functions under same solution which are triggered based on service bus queue and both queues are different?

How can I create two azure functions in C# under single solution where each functionApp reads messages from two different queues respectively? I am unable to specify %queueName% for both functions at same time in localhost json file.

[FunctionName("FuncA")]
public static async Task Run([ServiceBusTrigger("%queueName%", Connection = "sb_connection_string")]string myQueueItem, ILogger log)
{
}

and

[FunctionName("FuncB")]
public static async Task Run([ServiceBusTrigger("%failedQueueName%", Connection = "sb_connection_string")]string myQueueItem, ILogger log)
{
}

local setting json file :

"queueName" : "queueA",
"%failedQueueName%":"queueB"

How to read messages from queueB in funcB?

Error: Microsoft.Azure.WebJobs.Host: '%failedQueueName%' does not resolve to a value.

Upvotes: 1

Views: 770

Answers (1)

suziki
suziki

Reputation: 14103

First of all, what you want to do is definitely possible.

Then I need to explain to you the meaning of the percent sign. In your binding or trigger, in addition to Connection being parsed as an environment variable by default, you need to use a form like %key% to specify the value from the environment variable in other locations. Once you mark this way, the function host will try to find the environment variable whose key name is 'key' in the environment variable and replace %key% with the value of the corresponding key.

So, the format on your side your be like these:

In trigger or binding, use:

[ServiceBusTrigger("%queueName%", Connection = "sb_connection_string")]

[ServiceBusTrigger("%failedQueueName%", Connection = "sb_connection_string")]

And set the 'queuename' and 'failedQueueName' in local.settings.json(On azure, you need to set it in the configuration settings.). Like this:

{
  "IsEncrypted": false,
  "Values": {
    "queueName": "xxx",
    "failedQueueName": "xxx",
    "sb_connection_string": "xxx"
  }
}

And this is the official doc:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-patterns#binding-expressions---app-settings

Upvotes: 1

Related Questions