我有一个webjob应用程序来处理运行良好的ServiceBus队列,使用以下方法:
public static void ProcessQueueMessage([ServiceBusTrigger("myQueueName")] BrokeredMessage message, TextWriter log)
但是,我希望能够在不重新编译的情况下更改队列名称,例如根据配置appsetting,可以这样做吗?
答案 0 :(得分:5)
是的,你可以这样做。您可以实施自己的 INameResolver 并将其设置在 JobHostConfiguration.NameResolver 上。然后,您可以在ServiceBusTrigger属性中使用类似%myqueue%的队列名称 - 运行时将调用您的INameResolver来解析该%myqeuue%变量 - 您可以使用您想要的任何自定义代码来解析名称。您可以从应用程序设置等中读取它。
答案 1 :(得分:4)
我使用azure-webjobs-sdk-samples中的配置设置找到了INameResolver的实现。
/// <summary>
/// Resolves %name% variables in attribute values from the config file.
/// </summary>
public class ConfigNameResolver : INameResolver
{
/// <summary>
/// Resolve a %name% to a value from the confi file. Resolution is not recursive.
/// </summary>
/// <param name="name">The name to resolve (without the %... %)</param>
/// <returns>
/// The value to which the name resolves, if the name is supported; otherwise throw an <see cref="InvalidOperationException"/>.
/// </returns>
/// <exception cref="InvalidOperationException"><paramref name="name"/>has not been found in the config file or its value is empty.</exception>
public string Resolve(string name)
{
var resolvedName = CloudConfigurationManager.GetSetting(name);
if (string.IsNullOrWhiteSpace(resolvedName))
{
throw new InvalidOperationException("Cannot resolve " + name);
}
return resolvedName;
}
}