问题
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