Is there a way to dynamically determine the amount and names of queues triggered in Azure webjob during startup time?

若如初见. 提交于 2019-12-11 17:55:08

问题


I am using an Azure webjob with queue triggered functions to listen on several Azure queues. The processing method for each queue is identical (but the queues still need to be separate). I was wondering if there is a way to store the list of queue names in configuration and dynamically create functions that are triggered on those queues during startup time?

I know it is possible to do this for a single queue using INameResolver, but I couldn't find a solution for multiple queues.


回答1:


Actually, you could write server processing methods in a Function. When a webjob running, it will traverses all the methods in your function.

You could refer to the following code to dynamic trigger multiple queues.

In Program:

static void Main()
{
    var host = new JobHost(new JobHostConfiguration
    {
        NameResolver = new QueueNameResolver(),

    });
    host.RunAndBlock();
}

In Function:

public class Functions
{
    public static void ProcessQueueMessage([QueueTrigger("%queuename1%")] string message, TextWriter log)
    {
        log.WriteLine(message);
        Console.WriteLine("success");
    }
    public static void ProcessQueueMessage1([QueueTrigger("%queuename2%")] string message, TextWriter log)
    {
        log.WriteLine(message);
        Console.WriteLine("success2");
    }
}

In QueueNameResolver:

public class QueueNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        return ConfigurationManager.AppSettings[name].ToString();
    }
}

In App.config:

<appSettings>
    <add key="queuename1" value="queue"/>
    <add key="queuename2" value="myqueue"/>
</appSettings>

No matter you add message to queue or myqueue, it will always listen to them.



来源:https://stackoverflow.com/questions/50148862/is-there-a-way-to-dynamically-determine-the-amount-and-names-of-queues-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!