How to set Azure WebJob queue name at runtime?

≡放荡痞女 提交于 2019-11-27 12:35:19

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

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

A slight better implementation of name resolver to avoid fetching from configuration all time. Uses 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];
    }
}

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!