0

As simple as I can put it, I have the following:

public async Task Run([QueueTrigger("order-new", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)

Is there any way I can do something like:

 public async Task Run([QueueTrigger("order-new", Connection = _connection)]string myQueueItem, ILogger log)

Where the Connection is passed in as a variable? I have access to the value in the constructor, it is from Azure Application Configuration service. I am just trying not to have to change settings in more than one place. Thank you.

Update: I have actually found a way to get the actual string (not a variable) into the method arguments, but the "Connection" input is interpreting it as a variable it needs to look for, not the literal string which is the connection itself.

Update #2: I think I have it figured out, will write it up when I get a chance (@baum-mit-augen actually deleted the post that led me there).

naspinski
  • 34,020
  • 36
  • 111
  • 167
  • I am having a hard understanding your question. In general you inject IConfiguration and it gives you azure app configuration. But you want it by variable? – misha130 Apr 14 '21 at 20:27
  • Not necessarily, as far as I know, I need to get it into the arguments constructor of the 'Run' method (Connection="xxx"), if there is a another way to specify this, that would work as well! – naspinski Apr 15 '21 at 01:29

2 Answers2

1

Make sure you have an Application Setting set to your Azure Application Configuration connection string, in this example I have it named APP_CONFIG_CONNECTION

To do this in Visual Studio, right click the project, then click properties: enter image description here

In your Azure Function, it will be in the configuration section: enter image description here

Install the needed package: Install-Package Microsoft.Extensions.Configuration.AzureAppConfiguration -Version 4.2.1

Add a Startup.cs file if it does not already exist:

using System;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace
{
    class Startup : FunctionsStartup
    {
        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            string cs = Environment.GetEnvironmentVariable("APP_CONFIG_CONNECTION");
            builder.ConfigurationBuilder.AddAzureAppConfiguration(cs);
        }

        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddAzureAppConfiguration();
        }
    }
}

Now in your functions, you can refer to your config variables by path.

Examples

[return: Queue(queueName: "%Some:Path:QueueName%", Connection = "Some:Path:ConnectionString")]

or

public async Task Run([QueueTrigger(queueName: "%Some:Path:QueueName%", Connection = "Some:Path:ConnectionString")]string myQueueItem, ILogger log)

BOOM, no settings in your project outside of the connection string. Notice that the queueName variable is surround with '%' as it is a literal, while the Connection is not as it is a variable. You can also access other variables via dependency injection as well:

    private readonly SomeApiClient _api;

    public My_QueueTrigger(IConfiguration configuration)
    {
        var config = configuration.Get<AppSettings>();
        _api = new SomeApiClient (new
        {
            ApiUrl = config.ApiUrl,
            AuthUrl = config.AuthUrl,
            ClientId = config.ClientId,
            ClientSecret = config.ClientSecret,
            Roles = new[] { ApiRole.FullAccess }
        });
    }

Hope this is useful to someone else out there.

naspinski
  • 34,020
  • 36
  • 111
  • 167
-1

can I set this from a variable or specifically Azure Application Configuration?

No, It actually gets the value from the environment variable.

But if it is really necessary, you can import the required configuration into the environment variable when the function starts.

From your question description, it seems you are developing on Visual Studio, so just set settings in local.settings.json when develop on local. And upload the settings easily by using this:

enter image description here

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • The whole point I am trying to accomplish is to not have _any_ settings local to the project itself. I want all settings to be on the Azure Application Configuration service. I suppose I could script out the replacement upon a deployment job, but there has to be a better way, right? – naspinski Apr 15 '21 at 13:49
  • this is incorrect information, it is possible as shown in my solution – naspinski Apr 16 '21 at 15:31