4

I have a webjob app to process a ServiceBus queue, which runs fine, with the following method:

public static void ProcessQueueMessage([ServiceBusTrigger("myQueueName")] BrokeredMessage message, TextWriter log)

However, I would like to be able to change the queue name without recompiling, according for example to a configuration appsetting, can it be done?

Alberto Silva
  • 382
  • 3
  • 12
  • you can not do this. but you can change which queue you are reading from as you have a key in the appSettings. – Mostafa Nov 23 '15 at 02:50
  • Or you can create a new queue programmatically and add any messages to it with out the need to recompile. check this reference article: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-queues/ – Mostafa Nov 23 '15 at 02:51

2 Answers2

6

Yes, you can do this. You can implement your own INameResolver and set it on JobHostConfiguration.NameResolver. Then, you can use a queue name like %myqueue% in our ServiceBusTrigger attribute - the runtime will call your INameResolver to resolve that %myqeuue% variable - you can use whatever custom code you want to resolve the name. You could read it from app settings, etc.

mathewc
  • 13,312
  • 2
  • 45
  • 53
  • That did the trick, at first I wasn't aware that the % placeholders were required and tried to use other placeholders, like **{myqueue}** and then resolve the name with the placeholders, but then tried again as you suggested and it ran just fine :) – Alberto Silva Nov 23 '15 at 19:58
4

I've found an implementation of the INameResolver using configuration setting from the azure-webjobs-sdk-samples.

/// <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;
    }
}
Thomas
  • 24,234
  • 6
  • 81
  • 125