目录
介绍
要遵循的步骤
创建Azure函数应用
要在local.settings.json中制作的条目
要添加到函数类中运行方法的函数签名的附加参数
要添加到函数类中的运行方法主体的附加代码
部署
在门户中Azure Function App的配置部分添加应用程序设置和连接字符串条目
结果
介绍该代码是用C#——NET Core 3.1编写的,用于构建演示Azure函数。该函数将是一个带有匿名身份验证的HTTP触发函数。该函数将基本上返回应用程序设置或连接字符串条目的值,其键将在函数URL的查询参数中传递。本文的目的是向您展示如何从Azure Function App的配置部分读取条目,以及如何在开发过程中使用它们进行测试。
要遵循的步骤 创建Azure函数应用本文不涉及从https://portal.azure.com创建Azure Function App的详细信息。如果您需要了解从Azure门户创建Azure Function App的步骤,请在下面的评论中告诉我。您需要为运行此演示而创建的Function App应具有以下设置:
- 平台:.NET Core 3.1
- 环境:Windows
用于构建此演示的IDE是Visual Studio 2019。
要在local.settings.json中制作的条目{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": ,
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"TestApp": "Test App Settings Entry"
},
"ConnectionStrings": {
"TestConnection": "This is a test connection string"
}
}
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
ILogger log, ExecutionContext context)
{......
string connectionStr = req.Query["connectionStr"];
string appSetting = req.Query["appSetting"];
#region Must include when trying to read entries from Configuration section of Azure Function
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
#endregion
#region Read Application Settings Entry and Connection String Entry
var connectionString = config.GetConnectionString(connectionStr);
string appSettingValue = config.GetValue(appSetting);
#endregion
string responseMessage = $"Connection String: {connectionStr.ToString()}
AppSetting Va;ue: {appSettingValue}";
return new OkObjectResult(responseMessage);
将您的函数代码发布到已使用门户创建的函数应用中。本文不向你展示将函数部署到Azure平台所需的步骤。成功部署后,您应该会获得如下所示的URL。这将是部署到Azure Function App后Function的HTTP端点。
http://readconfig.azurewebsites.net
在门户中Azure Function App的配置部分添加应用程序设置和连接字符串条目
在localhost上构建和启动的代码应生成如下所示的URL:
http://localhost:7071/api/readconfig
这是localhost中函数的HTTP API端点。根据上述说明,在Web浏览器上使用下面的URL来确认输出是否与您在“local.settings.json”文件中创建的条目匹配。
localhost:7071/api/readconfig?connectionStr=TestConnection&appSetting=TestApp
输出:
Connection String: This is a test connection string ------------ AppSetting Value:
Test App Settings Entry
在启动托管在Azure上的函数的公共URL时,收到的输出将与根据上述建议在Azure函数应用程序配置部分中创建的条目相匹配。
readconfig.azurewebsites.net/api/readconfig?
connectionStr=TestConnection&appSetting=TestApp
输出:
Connection String: Function Connection ------------ AppSetting Value: Function Setting
https://www.codeproject.com/Tips/5320155/Reading-Entries-from-Application-Settings-and-Conn