如何在Azure Function中获取连接字符串?

问题描述 投票:0回答:1

我在 Azure 云中有一个 Azure Function 和一个 PostreSQL DB。

我想从我的 Azure 函数访问连接字符串,我们称之为

IT-PostgreSQL

这是我的 Azure 函数:

namespace InjectClaimsTokenFunc
{
    public class InjectClaims
    {
        private readonly ILogger<InjectClaims> _logger;
        private readonly DataContext _context;

        public InjectClaims(ILogger<InjectClaims> logger, DataContext context)
        {
            _logger = logger;
            _context = context;
        }

        [Function("InjectClaimsFunc")]
        public async Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
        {
            string username = Environment.GetEnvironmentVariable("BASIC_AUTH_USERNAME", EnvironmentVariableTarget.Process);
            _logger.LogInformation($"Username: {username}");

            string password = Environment.GetEnvironmentVariable("BASIC_AUTH_PASSWORD", EnvironmentVariableTarget.Process);
            _logger.LogInformation($"Password: {password}");

            var connectionString = Environment.GetEnvironmentVariable("CUSTOMCONNSTR_IT-PostgreSQL") ?? "Not found";
            _logger.LogInformation($"ConnectionString: {connectionString}");
        }
    }
}

我可以从

BASIC_AUTH_USERNAME
中检索
BASIC_AUTH_PASSWORD
Application Settings

现在无论我如何尝试检索连接字符串,我总是会收到“未找到”连接字符串。如何从

Connection strings
检索连接字符串?连接字符串具有在 Azure 中设置的值。

azure azure-functions azure-postgresql
1个回答
0
投票

同样的方法,只需指定ConnectionString部分即可:

string connectionString = Environment.GetEnvironmentVariable("ConnectionStrings:T-PostgreSQL");
© www.soinside.com 2019 - 2024. All rights reserved.