如何在运行时设置Azure WebJob队列名称? [英] How to set Azure WebJob queue name at runtime?
问题描述
我正在开发要与多个Azure网站一起使用的Azure WebJobs可执行文件.每个网站都需要自己的Azure存储队列.
I am developing an Azure WebJobs executable that I would like to use with multiple Azure websites. Each web site would need its own Azure Storage queue.
我看到的问题是ProcessQueueMessage
要求将队列名称静态定义为第一个参数inputText
的属性.我希望队列名称是正在运行的Azure网站实例的配置属性,并让作业可执行文件在启动时在运行时读取该内容.
The problem I see is that the ProcessQueueMessage
requires the queue name to be defined statically as an attribute of the first parameter inputText
. I would rather have the queue name be a configuration property of the running Azure Website instance, and have the job executable read that at runtime when it starts up.
有什么办法吗?
推荐答案
现在可以完成此操作.只需创建一个INameResolver
,即可解析任何用%(百分号)符号包围的字符串.例如,如果这是您的函数,并且指定了队列名称:
This can now be done. Simply create an INameResolver
to allow you to resolve any string surrounded in % (percent) signs. For example, if this is your function with a queue name specified:
public static void WriteLog([QueueTrigger("%logqueue%")] string logMessage)
{
Console.WriteLine(logMessage);
}
注意字符串logqueue
周围如何有%(百分号)符号.这意味着作业系统将尝试使用INameResolver
解析该名称,您可以创建INameResolver
然后注册您的作业.
Notice how there are % (percent) signs around the string logqueue
. This means the job system will try to resolve the name using an INameResolver
which you can create and then register with your job.
这里是一个解析器的示例,该解析器将只使用百分号中指定的字符串并在配置文件的AppSettings中查找它:
Here is an example of a resolver that will just take the string specified in the percent signs and look it up in your AppSettings in the config file:
public class QueueNameResolver : INameResolver
{
public string Resolve(string name)
{
return ConfigurationManager.AppSettings[name].ToString();
}
}
然后在Program.cs文件中,只需将其连接起来即可:
And then in your Program.cs file, you just need to wire this up:
var host = new JobHost(new JobHostConfiguration
{
NameResolver = new QueueNameResolver()
});
host.RunAndBlock();
这篇关于如何在运行时设置Azure WebJob队列名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!