适用于ServiceBusTrigger的appsettings.json中表示的Azure函数local.settings.json [英] Azure functions local.settings.json represented in appsettings.json for a ServiceBusTrigger
问题描述
我目前使用ServiceBusTrigger绑定具有azure功能
I currently have an azure function using the ServiceBusTrigger binding
[ServiceBusTrigger("%TopicName%", "%SubscripionName%", Connection = "MyConnection")]
string catclogueEventMsgs, ILogger log, ExecutionContext context)
使用此local.settings.json文件
which uses this local.settings.json file
"Values": {
…
"MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx
"SubscriptionName": "testsubscriptionName"
"TopicName": "testtopicName",
}
我该如何在appsettings.json文件中表示它.会像下面吗?
How do I represent this in the appsettings.json file. Will it be like the below?
"Values": {
"MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx
"SubscriptionName": "testsubscriptionName"
"TopicName": "testtopicName",
}
我可以使用如下所示的"MySubs"对象代替我的"Values"对象吗?
Instead of using a "Values" object can I use eg "MySubs" object like the below?
"MySubs": {
"MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx
"SubscriptionName": "testsubscriptionName"
"TopicName": "testtopicName",
}
如果可以使用上述设置,我该如何在ServiceBusTrigger绑定中表示它?我会改成这个吗?
If its possible to use the above settings, how do I represent this in the ServiceBusTrigger binding? would i change it to this?
[ServiceBusTrigger("%MySubs.TopicName%", "%MySubs.SubscripionName%", Connection = "MySubs.MyConnection")]
string catclogueEventMsgs, ILogger log, ExecutionContext context)
推荐答案
您确实可以按以下方式读取Values
数组之外的设置:
You CAN indeed read settings outside the Values
array as follows:
public class WeatherApiConfig
{
public string WeatherApiUrl { get; set; }
public string WeatherApiKey { get; set; }
}
Azure Functions V2的新增功能,我们拥有如下所示处理DI的合适方法:
New for Azure Functions V2, we have an appropriate way to handle DI as shown below:
Startup.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**"
}
}
注意:对我来说,关键是要在
Startup.cs
中添加.SetBasePath(Directory.GetCurrentDirectory())
,因为没有它就找不到文件.
Note: The key for me was to add
.SetBasePath(Directory.GetCurrentDirectory())
inStartup.cs
since it couldn't find the file without it.
在生产中,我使用功能应用程序的Application Settings
部分来按如下方式配置这两个属性:
In production I use the function app's Application Settings
section to configure these two properties as follows:
这篇关于适用于ServiceBusTrigger的appsettings.json中表示的Azure函数local.settings.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!