Azure Web Job 多个连接字符串

3
Azure WebJob从运行该任务的Web应用程序配置参数AzureWebJobsStorage中获取连接字符串。我需要使用一个WebJob监控位于不同存储中的两个队列。是否有可能为WebJob设置多个连接字符串呢?

你不能添加多个具有不同名称和值的连接字符串吗? - henrikmerlander
据我所知,Azure WebJob 使用父应用程序中默认的连接字符串名称“AzureWebJobsStorage”。 - minuzZ
你有使用连接字符串的代码示例吗? - henrikmerlander
@henmer,你对WebJobs有了解吗? WebJobs SDK考虑到从主应用程序传入的AzureWebJobsStorage连接字符串。我无法在WebJob中访问它,因为WebJobs不知道它将托管在哪里。 - minuzZ
1
@minuzZ // AzureWebJobsStorage只是默认的连接字符串,你可以使用多个连接字符串。 - Youngjae
1个回答

3

与此帖子相关的可能性如下:

在您的情况下,您想要绑定到不同的存储账户,因此您的函数可能看起来像这样:

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

}

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

}

如果您想从配置中获取connectionstrings,也可以实现自定义的INameResolver:

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

使用它的方法:

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

你的新功能看起来像这样:

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

}

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

}
  • appSettings中的storageAccount1和storageAccount2是连接字符串的关键字。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接