gpt4 book ai didi

c# - Azure 函数 local.settings.json 在 ServiceBusTrigger 的 appsettings.json 中表示

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:28 24 4
gpt4 key购买 nike

我目前有一个使用 ServiceBusTrigger 绑定(bind)的 azure 函数

 [ServiceBusTrigger("%TopicName%", "%SubscripionName%", Connection = "MyConnection")]
string catclogueEventMsgs, ILogger log, ExecutionContext context)

它使用这个 local.settings.json 文件

   "Values": {

"MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx
"SubscriptionName": "testsubscriptionName"
"TopicName": "testtopicName",
}

如何在 appsettings.json 文件中表示这一点。会像下面这样吗?

   "Values": {
"MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx
"SubscriptionName": "testsubscriptionName"
"TopicName": "testtopicName",
}

我可以使用如下所示的“MySubs”对象来代替使用“Values”对象吗?

   "MySubs": {
"MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx
"SubscriptionName": "testsubscriptionName"
"TopicName": "testtopicName",
}

如果可以使用上述设置,我如何在 ServiceBusTrigger 绑定(bind)中表示它?我可以把它改成这个吗?

 [ServiceBusTrigger("%MySubs.TopicName%", "%MySubs.SubscripionName%", Connection = "MySubs.MyConnection")]
string catclogueEventMsgs, ILogger log, ExecutionContext context)

最佳答案

您确实可以读取 Values 数组外部的设置,如下所示:

WeatherApiConfig.cs

public class WeatherApiConfig
{
public string WeatherApiUrl { get; set; }
public string WeatherApiKey { get; set; }
}

New for Azure Functions V2, we have an appropriate way to handle DI as shown below:

启动.cs

[assembly: FunctionsStartup(typeof(BlazingDemo.Api.Startup))]

namespace BlazingDemo.Api
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();

var apiConfig = new WeatherApiConfig();
config.Bind(nameof(WeatherApiConfig), apiConfig);

builder.Services.AddSingleton(apiConfig);
builder.Services.AddHttpClient();
}
}
}

Local.settings.json

{  
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"WeatherApiConfig": {
"WeatherApiUrl": "http://api.openweathermap.org/data/2.5/weather",
"WeatherApiKey": "**removed**"
}
}

Note: The key for me was to add .SetBasePath(Directory.GetCurrentDirectory()) in Startup.cs since it couldn't find the file without it.

在生产中,我使用函数应用的 Application Settings 部分来配置这两个属性,如下所示:

Application Settings in Azure

关于c# - Azure 函数 local.settings.json 在 ServiceBusTrigger 的 appsettings.json 中表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com