48

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 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.

Is there any way to do this?

Jan_V
  • 4,244
  • 1
  • 40
  • 64
eklein23
  • 483
  • 4
  • 5

4 Answers4

102

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();
dprothero
  • 2,683
  • 2
  • 21
  • 28
  • 9
    The default JobHostConfiguration in recent versions of the SDK automatically includes a NameResolver, so no need to define your own anymore, you can directly use the %configkey% and it should work. – viblo Sep 26 '17 at 13:34
  • @viblo Can you provide a reference for this? Is this still the case with WebJobs 3.x? – Scotty H Mar 12 '19 at 21:13
  • @ScottH I dont know what the status is for the latest 3.x sdk, I have not upgraded yet. Seems like they changed things quite a lot: https://github.com/Azure/azure-webjobs-sdk/issues/1870 – viblo Mar 13 '19 at 19:43
  • However, this is not setting it at runtime; configuration is defined pre-runtime so although this is the popular answer it doesn't answer the question "how to set the queue name (to an arbitrary string) at runtime? – Bozhidar Stoyneff Aug 05 '20 at 16:15
  • This seems to be the current documentation on "custom binding expressions" as these placeholders are called: https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#custom-binding-expressions – Palec Oct 10 '22 at 13:41
  • The default INameResolver implementation uses IConfiguration indexer, i.e. you can use configuration keys as the placeholders, e.g. `%ServiceBus:InputQueue%`: https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Host/DefaultNameResolver.cs – Palec Oct 10 '22 at 13:47
2

This is probably an old question, but in case anyone else stumbles across this post. This is now supported by passing a JobHostConfiguration object into the JobHost constructor.

http://azure.microsoft.com/en-gb/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/#config

Gumzle
  • 847
  • 6
  • 16
1

A slight better implementation of name resolver to avoid fetching from configuration all time. It uses a Dictionary to store the config values once retrieved.

using Microsoft.Azure.WebJobs;
using System.Collections.Generic;
using System.Configuration;

public class QueueNameResolver : INameResolver
{
    private static Dictionary<string, string> keys = new Dictionary<string, string>();
    public string Resolve(string name)
    {
        if (!keys.ContainsKey(name))
        {
            keys.Add(name, ConfigurationManager.AppSettings[name].ToString());
        }
        return keys[name];
    }
}
d219
  • 2,707
  • 5
  • 31
  • 36
NitinSingh
  • 2,029
  • 1
  • 15
  • 33
-1

Unfortunately, that is not possible. You can use the IBinder interface to bind dynamically to a queue but you will not have the triggering mechanism for it.

Basically, the input queue name has to be hardcoded if you want triggers. For output, you can use the previously mentioned interface.

Here is a sample for IBinder. The sample binds a blob dynamically but you can do something very similar for queues.

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103