3
votes

Azure WebJob obtains connection string from web application (which runs the job) configuration parameter - AzureWebJobsStorage. I need to monitor two queues in different storages using one WebJob. Is it possible somehow to have multiple connection strings for a WebJob?

1
Can't you add multiple connection strings with different names and values? - henrikmerlander
AFAIK Azure WebJob uses the default connection string name "AzureWebJobsStorage" from parent application. - minuzZ
Do you have any code example where you use the connection string? - henrikmerlander
@henmer, are you familiar with webjobs at all? WebJobs SDK takes in account AzureWebJobsStorage connection string from parent application. I cannot access it t WebJob, because WebJobs knows nothing about where it will be hosted. - minuzZ
@minuzZ // AzureWebJobsStorage's just default but not the only thing. you can do with multiple connection strings. - Youngjae

1 Answers

3
votes

Related to this post it is possible :

In your case, you'd like to bind to differents storage accounts so your function can look like that:

public static void JobQueue1(
    [QueueTrigger("queueName1"),
    StorageAccount("storageAccount1ConnectionString")] string message)
{

}

public static void JobQueue2(
    [QueueTrigger("queueName2"),
    StorageAccount("storageAccount2ConnectionString")] string message)
{

}

You can also implement a custom INameResolver if you want to get the connectionstrings from the config :

public class ConfigNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        string resolvedName = ConfigurationManager.AppSettings[name];
        if (string.IsNullOrWhiteSpace(resolvedName))
        {
            throw new InvalidOperationException("Cannot resolve " + name);
        }

        return resolvedName;
    }
}

to use it:

var config = new JobHostConfiguration();
config.NameResolver = new ConfigNameResolver();
...
new JobHost(config).RunAndBlock();

And your new functions look like that:

public static void JobQueue1(
    [QueueTrigger("queueName1"),
    StorageAccount("%storageAccount2%")] string filename)
{

}

public static void JobQueue2(
    [QueueTrigger("queueName2"),
    StorageAccount("%storageAccount1%")] string filename)
{

}
  • storageAccount1 and storageAccount2 are the connection string key in the appSettings