Search code examples
message-queueproductionazure-webjobsstaging

Azure WebJob- QueueTrigger Staging and Production


I have an Azure WebJob that will be used for Staging and Production. The keys for the proper environments are set in the app.config file.

A ConfigManager class reads the properties from the app.config file.

 public static string FirstQueue
 {
     get { return ConfigurationManager.AppSettings["FirstQueue"]; }
 }

I would like the QueueTrigger to read the from the proper queue specified in the app.config file.

 public static void ProcessFirstQueue([QueueTrigger(ConfigManager.FirstQueue)] string message)
{
   //some function       
}

However, the QueueTrigger doesn't seem to like this. I get "An attribute argument must be a constant expression, type of expression...." Any suggestions to set this up. I do not want to hard code values in the queue trigger. Thanks


Solution

  • You can resolve the queue name at runtime using the

    INameResolver

    Here's an example to demonstrate this:

    WebJob Code:

    public class Program
    {
        private static void Main(string[] args)
        {
            var config =
                new JobHostConfiguration("insert connection string")
                {
                    NameResolver = new QueueNameResolver()
                };
            var host = new JobHost(config);
    
            host.RunAndBlock();
        }
    
        public static void ProcessNotification([QueueTrigger("%queueKey%")] string item)
        {
            //Handle message
        }
    
        public static void ProcessPoison([QueueTrigger("%queueKeyPoison%")] string item)
        {
            //Handle poison message
        }
    
    }
    

    Here's the QueueNameResolver class:

    public class QueueNameResolver : INameResolver
    {
        //name = queueKey or queueKeyPoison
        public string Resolve(string name)
        {
            //Use the name to get it from the app.config or somewhere else.
        }
    }
    

    Hope this helps,