65.9K
CodeProject 正在变化。 阅读更多。
Home

使用C# .NET Core 3.1、5、6从Azure函数中的应用程序设置和连接字符串读取条目

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2021年12月16日

CPOL

2分钟阅读

viewsIcon

9117

downloadIcon

69

这是一个编码解决方案,用于使用Azure Functions V2.x及更高版本中“配置”菜单下的“应用程序设置”和“连接字符串”部分来存储可配置的条目。

引言

代码是用C# - NET Core 3.1编写的,用于构建一个演示Azure函数,但也支持更高版本的.NET。该函数将是一个具有匿名身份验证的HTTP触发函数。该函数基本上将返回应用程序设置或连接字符串条目的值,其键将作为函数URL的查询参数传递。本文旨在向您展示如何读取Azure Function App配置部分中的条目,以及如何在开发过程中使用它们进行测试。

遵循的步骤

创建一个Azure Function App

本文不涉及从https://portal.azure.com创建Azure Function App的细节。如果您需要了解从Azure门户创建Azure Function App的步骤,请在下面的评论中告诉我。您需要创建用于运行此演示的Function App应具有以下设置

  1. 平台:.NET Core 3.1或更高版本
  2. 环境:Windows

用于构建此演示的IDE是Visual Studio 2019或更高版本。

需要在local.settings.json中进行的条目

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": <Your account specific entry will be autopoulated here 
                            if you are using Visual Studio>,
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "TestApp": "Test App Settings Entry"
  },
  "ConnectionStrings": {
    "TestConnection": "This is a test connection string"
  }
}

添加到函数类中Run方法的函数签名中的附加参数

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {......

添加到函数类中Run方法主体的附加代码

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<string>(appSetting);
#endregion

string responseMessage = $"Connection String: {connectionStr.ToString()} 
                          AppSetting Va;ue: {appSettingValue}";
                                     
return new OkObjectResult(responseMessage);

部署

将您的函数代码发布到已经使用门户创建的函数应用程序中。本文未显示将函数部署到Azure平台所需的步骤。成功部署后,您应该会得到如下URL。这将是您的函数在部署到Azure Function App后的HTTP端点。

http://readconfig.azurewebsites.net

在门户的Azure Function App配置部分中添加应用程序设置和连接字符串条目

结果

在本地主机上构建和启动代码时,应该会生成如下URL

https://:7071/api/readconfig

这是您本地主机中函数的HTTP API端点。在Web浏览器上使用下面的URL确认输出是否与您根据上述说明在“local.settings.json”文件中输入的条目匹配。

<a href="https://:7071/api/readconfig?connectionStr=TestConnection&appSetting=TestApp">
localhost:7071/api/readconfig?connectionStr=TestConnection&appSetting=TestApp</a>

输出

Connection String: This is a test connection string ------------ AppSetting Value: 
Test App Settings Entry

启动托管在Azure上的函数的公共URL时,收到的输出将与上述建议中在Azure Function App配置部分中输入的条目匹配。

<a href="http://readconfig.azurewebsites.net/api/readconfig?connectionStr=TestConnection&
appSetting=TestApp">readconfig.azurewebsites.net/api/readconfig?
connectionStr=TestConnection&appSetting=TestApp</a>

输出

Connection String: Function Connection ------------ AppSetting Value: Function Setting

历史

  • 2021年12月16日:初始版本
© . All rights reserved.