azure-functions-dotnet-worker: ServiceBusTrigger and INameResolver

Hello,

Is it still possible to pass dynamic queue name to Service Bus trigger? Previously INameResolver was used for that, but the new extension is missing this interface.

public class QueueNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        return ConfigurationManager.AppSettings[name].ToString();
    }
}

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 5
  • Comments: 21 (3 by maintainers)

Most upvoted comments

This is a blocker for us moving from Web Jobs to Azure Functions.

Is there any update here? It seems I can’t even put my queue names in a separate ‘queue-names.json’ file

eg

    builder
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddJsonFile("queue-names.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();

If the queue names are in queue-names.json then it simply doesn’t work. They have to be in local.settings.json

@dhermyt I totally agree we should be able to inject our custom NameResolver like we can with the “normal” Azure Functions (called now class library AFAIK).

https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#version-3x-6

 var builder = new HostBuilder();
    var resolver = new CustomNameResolver();
    builder.ConfigureWebJobs(b =>
    {
        b.AddAzureStorageCoreServices();
    });
    builder.ConfigureServices(s => s.AddSingleton<INameResolver>(resolver));  // This is how to do it with "normal" azure functions and WebJobs
    var host = builder.Build();
    using (host)
    {
        await host.RunAsync();
    }

But have in mind that the Isolated mode does implement a the default NameResolver es mentioned here https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#custom-binding-expressions

“There’s a default NameResolver that takes effect if you don’t provide a custom one. The default gets values from app settings or environment variables.”

So, while it’s not as powerful and flexible as a custom NameResolver, it may work for you as the default behaviour is reading from an appsetting/enviromentvariable.

So this:

        [Function("Function1")]
        public void Run([ServiceBusTrigger("%myqueueName%", Connection = "MyConnection")] string myQueueItem, FunctionContext context)
        {
            var logger = context.GetLogger("Function1");
            logger.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
        }

reads the queue name from a configuration setting / enviroment variable called myqueueName. I have tested this myself both locally and in Azure.

I insist, we should be able to inject our custom NameResolver for max flexibility

Hello Fabio,

In my case the queue name comes from the configuration and should be set dynamically. This is how it looked in the previous SDK:

private const string InputQueueName = "%ServiceBus:Queues:QueueName%";

        [Function(FunctionName)]
        public async Task RunAsync([ServiceBusTrigger(InputQueueName)] Message message, CancellationToken cancellationToken)
        {

Here is the example on SO: https://stackoverflow.com/a/30515085

I’m looking for clarification. With ServiceBusTrigger, the value given to Connection is used to get the connection string from IConfiguration. This works as documented.

However, if you use the % % syntax for topicName and subscriptionName, it only pulls those values from the environment variables, NOT from IConfiguration. I have seen several statements in documentation (and in issues of this repo) that state these values are pulled from configuration when the value begins and ends with %, but based on my testing, that isn’t true.

Please let me know if I’m incorrect and if anyone has been able to set these values from IConfiguration. FYI, if you set the values in local.settings.json when running locally, those become environment variables at runtime.

I have added my code to Stack Overflow here : https://stackoverflow.com/questions/78897966/azure-functions-trigger-variables-isolated-functions/78897967#78897967

Please upvote, question and answer, if useful.

I’ve also tried several variations of trying to inject a custom INameResolver (with a dotnet-isolated) function before I came across this github issue. As with others, it always hits the internal name resolver first, barfs and then stops, before getting to mine, by which time it’s too late.

Given that .NET 8 is forcing Functions into being isolated process (and no more in-process), it would be good if we could get this resolved 😉

Thank you 👍🏻

I agree - I’m going to try and do a blog post on how I worked around this - but it’s convoluted, build step, generate local.settings.json for each user. Honestly, such a lot of work for something that should be so simple!