我对部署后应在 Azure 函数内使用的设置存储在何处感到有点困惑。
在本地测试中,我有像 local.settings.json
中的连接字符串这样的设置,建议通过以下代码读取:
[FunctionName("SomeEvent")]
public static void Run(
[EventHubTrigger("some-hub", Connection = "EventHubConnection" )]
EventData ev,
ILogger log,
ExecutionContext context,
IConfiguration configuration,
[EventHub("brd-iot-eventhubs", Connection = "OutputEventHubConnection")]
ICollector<EventData> outputEvents)
{
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile(SETTINGS, optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
string customername = config.GetValue<string>("Customer");
string connectionString = config.GetConnectionString("myDatabase");
[...]
现在我想在 Azure 上部署后进行设置(具有不同的值)。 AFAIK host.json 有所不同,因为这不是每个功能的配置,而是全局配置。
我可以直接在 Azure 中手动编辑属性,但我想要一个可以部署的设置文件。有什么方法可以做到这一点?也许我只需要知道设置文件的名称。
最佳答案
您可以将自定义设置放入您需要的任何文件中,并像您所做的那样使用 ConfigurationBuilder
读取它们。将自定义设置文件(例如appsettings.json
)放在功能项目下,并将其设置为复制到output/publish目录(如果较新则复制),这样就可以通过VS发布进行部署。
但是对于与函数绑定(bind)相关的设置,例如 EventHubConnection
和 OutputEventHubConnection
,函数运行时从环境变量中读取它们。我们必须回复 Azure 门户上的应用程序设置
,将它们注入(inject)到环境变量中。
还要注意的是,VS 为我们提供了一个友好的 UI,以便我们在本地和 Azure 环境中配置不同的设置。我们可以选择从本地插入或者创建新的设置。(注意这里的空值表示对应的环境中没有该设置)
关于azure - 在哪里存储 local.settings.json 中的设置以进行 proddeployment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53759522/