How to set Azure WebJob queue name at runtime?

后端 未结 4 1740
一个人的身影
一个人的身影 2020-11-29 05:37

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.

The proble

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 06:29

    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);
    }
    

    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.

    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();
        }
    }
    

    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();
    

提交回复
热议问题