Search code examples
.net-coretriggersazure-webjobs

Is it possible to set the queue name into an appsettings.json file?


In order to dynamically starts a process, I am using a webjob with a function triggered by an Azure Storage Account Queue.

We have a "developpement" and a "release" version of our web application, and so we want to target different queues of the storage account to separate our versions of the webjobs. We currently do that for our databases by using connectionStrings in an appsettings.json, and we want to do the same for our webjobs, but we didn't found any way to do that.

//What I have : 
public static async Task CFT([QueueTrigger("test")] string message, ILogger log)

//What I want to have (if possible):

public static async Task CFT([QueueTrigger(Configuration.GetString("TableStorage"))] string message, ILogger log)

If it's not possible here, is it possible to do it in the Main program for exemple ?

Thank you for your help.


Solution

  • The webjob supports custom binding expressions, you could refer to this doc:How to use the Azure WebJobs SDK for event-driven background processing. So what you need to do is just set the binding like this %queuename%, and set it in the appsettings.json and don't need to set anything else.

    Here is my test.

    appsettings.json
    
    {
          "AzureWebJobsStorage": "connection sring",
          "queuename": "myqueue"
    }
    

    Function.cs

    public static void ProcessQueueMessage([QueueTrigger("%queuename%")] string message, ILogger logger)
            {
                logger.LogInformation(message);
            }
    

    The result:

    enter image description here