How To Configure Queue Name for Queue Trigger In Azure Function App
Asked Answered
A

2

42

I'm creating a function app in Azure and want to use a queue trigger. I know how to configure the queue name at design time, e.g:

[FunctionName("MyTestFunction")]
public static void Run([QueueTrigger("myqueue-items", Connection = "testdelete")]string myQueueItem, TraceWriter log)

However, I'd like to be able to define and reference it in a configuration file. I'm aware of the existence of function.json (Probably this one), host.json and local.settings.json, but I don't know how to set a queue name in there and have it be referenced in the function.

If I deploy a freshly created function created in visual studio (With the new 15.3 update), I can see the following in the function.json file post deployment (even though the file doesn't exist when i develop locally):

  "bindings": [
    {
      "type": "queueTrigger",
      "queueName": "myqueue-items",
      "connection": "testdelete",
      "name": "myQueueItem"
    }

I've found that if I create that file, and change the "queueName" to something that doesn't match the value in the actual function, it unfortunately doesn't override it (That would have been too easy I guess).

How can I reference the bindings in the function.json in the functions QueueTrigger attribute?

Presumably whatever the solution is will allow me to do the same with poison queue handling?

The reason I want to do this, is because I need to deploy multiple instances of the exact same function, but pointing each one at a different queue (In order to get around max memory limitations).

Thanks.

Animus answered 19/8, 2017 at 21:11 Comment(1)
I suggest you implement the INameResolver interface. #44901665Eveleen
M
103

Could you not just reference the queue name as a setting (using the %settingName% syntax) for your App Function? Then in each function app you deploy have change the setting to the required queue name.

[FunctionName("MyTestFunction")]
public static void Run([QueueTrigger("%MyQueueName%", Connection = "testdelete")]string myQueueItem, TraceWriter log)

And specify the setting in local.settings.json for running locally

{
  "Values: {
     "MyQueueName": "myqueue-items"
   }
}
Macdonell answered 21/8, 2017 at 0:47 Comment(7)
How does this translate to an azure deployment i.e. where do I set MyQueueName to have it picked up once deployed? And similarly for the connection string 'testdelete'?Venom
I haven't worked with these settings for a while now, but from memory they are in the AppSettings of the function app - so it depends on how you are publishing the function app (settings might be in an ARM template)Macdonell
@Taran, you can place these into your Application Settings of your Function AppDealer
This actually works! Also if you want to override these settings in azure portal appsettings tab then you don't need to write parent setting name Values:MyQueueName , just only: MyQueueName.Asseveration
so how would you do this if for example you are using nodejs and have to work witht he function.json file?Knisley
Is this documented anywhere so I can read more about it?Lunulate
@CrhistianRamirez the Microsoft documentation is hereSpile
S
2

I came here googling for a way to dynamically change the queue name and finally figured out how to change the queue name on the start of the function app.

You can leverage the environment variable like Garth proposed:

public static void Run([QueueTrigger("%MyQueueName%", Connection = "testdelete")]string myQueueItem, TraceWriter log)

In the Startup class (that inherits from FunctionsStartup) override the following method and set the queue name to anything you like:

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
    Environment.SetEnvironmentVariable("MyQueueName", $"personal-queue-{Environment.UserName}");
    
    base.ConfigureAppConfiguration(builder);
}

I tried setting this in public override void Configure(IFunctionsHostBuilder builder) but it seems the environment was already read and the MyQueueName variable was not updated. Doing this in the ConfigureAppConfiguration method worked for me on .NET 6.0 (in-process function app)

Edit: after I posted this, I saw camelCase's comment that INameResolver could also do the trick and he's correct!

Sunburn answered 13/9, 2023 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.