在 Azure Function v3 中获取连接字符串
Get Connection String in Azure Function v3
我很困惑。我想在 Azure v3 函数 (.Net Core 3.1) 中获取连接字符串。
我的本地设置看起来像
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
},
"ConnectionStrings": {
"DefaultConnection": "bla bla"
}
}
在我做的函数中
string defaultConnection = Environment.GetEnvironmentVariable("ConnectionStrings:DefaultConnection");
这在本地运行良好,但在 Azure 上,defaultConnection 为空。我在函数的应用程序设置的连接字符串部分下定义了连接。
我的方法对 Azure Function v3 正确吗?
请注意
Connection strings should only be used with a function app if you are
using entity framework. For other scenarios use App Settings.
所以如果你只是想获取DefaultConnection的值,可以把它放在Application settings下,这样就可以获取了
Environment.GetEnvironmentVariable("DefaultConnection");
Entity Framework的Azure功能请参考this article。
您需要指定连接字符串前缀(参见 documentation):
Environment.GetEnvironmentVariable("CUSTOMCONNSTR_DefaultConnection");
如果您使用的是 Microsoft.NET.Sdk.Functions 3.0.11 和 Microsoft.Azure.Functions.Extensions 1.1.0,并且您使用的是具有依赖注入的 Azure Functions,则可以执行以下操作来访问连接字符串(或任何配置)启动应用程序时。
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using System;
[assembly: FunctionsStartup(typeof(YourNamespace.Startup))]
namespace YourNamespace
{
public class Startup : FunctionsStartup
{
public IConfiguration Configuration { get; set; }
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
var config = builder.ConfigurationBuilder.Build();
// This can be used to get a connection string from local.settings.json and also works when deployed to Azure
var appConfigConnection = config.GetConnectionString("AppConfig");
// This is to access the environment variables.
var environment = Environment.GetEnvironmentVariable("Environment");
// This particular example uses the values retrieved to bootstrap Azure App Configuration
builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
{
options.Connect(appConfigConnection).Select(KeyFilter.Any, environment.ToLowerInvariant());
});
}
public override void Configure(IFunctionsHostBuilder builder)
{
// In case you need to access IConfiguration
Configuration = builder.GetContext().Configuration;
}
}
}
样本local.setting.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "your connection string",
"Environment": "Development"
},
"ConnectionStrings": {
"AppConfig": "your connection string"
}
}
我很困惑。我想在 Azure v3 函数 (.Net Core 3.1) 中获取连接字符串。
我的本地设置看起来像
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
},
"ConnectionStrings": {
"DefaultConnection": "bla bla"
}
}
在我做的函数中
string defaultConnection = Environment.GetEnvironmentVariable("ConnectionStrings:DefaultConnection");
这在本地运行良好,但在 Azure 上,defaultConnection 为空。我在函数的应用程序设置的连接字符串部分下定义了连接。
我的方法对 Azure Function v3 正确吗?
请注意
Connection strings should only be used with a function app if you are using entity framework. For other scenarios use App Settings.
所以如果你只是想获取DefaultConnection的值,可以把它放在Application settings下,这样就可以获取了
Environment.GetEnvironmentVariable("DefaultConnection");
Entity Framework的Azure功能请参考this article。
您需要指定连接字符串前缀(参见 documentation):
Environment.GetEnvironmentVariable("CUSTOMCONNSTR_DefaultConnection");
如果您使用的是 Microsoft.NET.Sdk.Functions 3.0.11 和 Microsoft.Azure.Functions.Extensions 1.1.0,并且您使用的是具有依赖注入的 Azure Functions,则可以执行以下操作来访问连接字符串(或任何配置)启动应用程序时。
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using System;
[assembly: FunctionsStartup(typeof(YourNamespace.Startup))]
namespace YourNamespace
{
public class Startup : FunctionsStartup
{
public IConfiguration Configuration { get; set; }
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
var config = builder.ConfigurationBuilder.Build();
// This can be used to get a connection string from local.settings.json and also works when deployed to Azure
var appConfigConnection = config.GetConnectionString("AppConfig");
// This is to access the environment variables.
var environment = Environment.GetEnvironmentVariable("Environment");
// This particular example uses the values retrieved to bootstrap Azure App Configuration
builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
{
options.Connect(appConfigConnection).Select(KeyFilter.Any, environment.ToLowerInvariant());
});
}
public override void Configure(IFunctionsHostBuilder builder)
{
// In case you need to access IConfiguration
Configuration = builder.GetContext().Configuration;
}
}
}
样本local.setting.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "your connection string",
"Environment": "Development"
},
"ConnectionStrings": {
"AppConfig": "your connection string"
}
}