Azure Function Queue Trigger Connection - 我可以从变量或特定的 Azure 应用程序配置中设置它吗?
Azure Function Queue Trigger Connection - can I set this from a variable or specifically Azure Application Configuration?
尽可能简单,我有以下内容:
public async Task Run([QueueTrigger("order-new", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
有什么方法可以做类似的事情:
public async Task Run([QueueTrigger("order-new", Connection = _connection)]string myQueueItem, ILogger log)
Connection作为变量传入哪里?我可以访问构造函数中的值,它来自 Azure 应用程序配置服务。我只是想不必在多个地方更改设置。谢谢。
更新:
我实际上已经找到了一种将实际字符串(不是变量)获取到方法参数中的方法,但是“连接”输入将其解释为需要查找的变量,而不是作为连接本身的文字字符串。
更新 #2:
我想我已经弄清楚了,有机会时会把它写下来(@baum-mit-augen 实际上删除了 post 把我带到那里)。
can I set this from a variable or specifically Azure Application
Configuration?
不是,它实际上是从环境变量中获取值。
但如果确实需要,可以将需要的配置导入到环境变量中when the function starts。
从你的问题描述来看,你好像是在Visual Studio上开发的,所以本地开发的时候在local.settings.json
里设置即可。并使用以下方法轻松上传设置:
确保您已将应用程序设置设置为您的 Azure 应用程序配置连接字符串,在此示例中,我将其命名为 APP_CONFIG_CONNECTION
要在 Visual Studio 中执行此操作,请右键单击该项目,然后单击属性:
在您的 Azure 函数中,它将位于配置部分:
安装需要的包:
安装包 Microsoft.Extensions.Configuration.AzureAppConfiguration - 版本 4.2.1
如果 Startup.cs 文件不存在,请添加:
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();
}
}
}
现在在您的函数中,您可以通过路径引用您的配置变量。
例子
[return: Queue(queueName: "%Some:Path:QueueName%", Connection = "Some:Path:ConnectionString")]
或
public async Task Run([QueueTrigger(queueName: "%Some:Path:QueueName%", Connection = "Some:Path:ConnectionString")]string myQueueItem, ILogger log)
BOOM,您的项目中没有连接字符串之外的设置。请注意,queueName 变量被“%”包围,因为它是一个文字,而 Connection 不是,因为它是一个变量。您还可以通过依赖注入访问其他变量:
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 }
});
}
希望这对其他人有用。
尽可能简单,我有以下内容:
public async Task Run([QueueTrigger("order-new", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
有什么方法可以做类似的事情:
public async Task Run([QueueTrigger("order-new", Connection = _connection)]string myQueueItem, ILogger log)
Connection作为变量传入哪里?我可以访问构造函数中的值,它来自 Azure 应用程序配置服务。我只是想不必在多个地方更改设置。谢谢。
更新: 我实际上已经找到了一种将实际字符串(不是变量)获取到方法参数中的方法,但是“连接”输入将其解释为需要查找的变量,而不是作为连接本身的文字字符串。
更新 #2: 我想我已经弄清楚了,有机会时会把它写下来(@baum-mit-augen 实际上删除了 post 把我带到那里)。
can I set this from a variable or specifically Azure Application Configuration?
不是,它实际上是从环境变量中获取值。
但如果确实需要,可以将需要的配置导入到环境变量中when the function starts。
从你的问题描述来看,你好像是在Visual Studio上开发的,所以本地开发的时候在local.settings.json
里设置即可。并使用以下方法轻松上传设置:
确保您已将应用程序设置设置为您的 Azure 应用程序配置连接字符串,在此示例中,我将其命名为 APP_CONFIG_CONNECTION
要在 Visual Studio 中执行此操作,请右键单击该项目,然后单击属性:
在您的 Azure 函数中,它将位于配置部分:
安装需要的包: 安装包 Microsoft.Extensions.Configuration.AzureAppConfiguration - 版本 4.2.1
如果 Startup.cs 文件不存在,请添加:
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();
}
}
}
现在在您的函数中,您可以通过路径引用您的配置变量。
例子
[return: Queue(queueName: "%Some:Path:QueueName%", Connection = "Some:Path:ConnectionString")]
或
public async Task Run([QueueTrigger(queueName: "%Some:Path:QueueName%", Connection = "Some:Path:ConnectionString")]string myQueueItem, ILogger log)
BOOM,您的项目中没有连接字符串之外的设置。请注意,queueName 变量被“%”包围,因为它是一个文字,而 Connection 不是,因为它是一个变量。您还可以通过依赖注入访问其他变量:
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 }
});
}
希望这对其他人有用。