簡體   English   中英

在 Azure Function v3 中獲取連接字符串

[英]Get Connection String in Azure Function v3

我很困擾。 我想在 Azure v3 function (.Net Core 3.1)中獲取連接字符串。

我的本地設置看起來像

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
    },
    "ConnectionStrings": {
      "DefaultConnection": "bla bla"
    }
}

在 function 我做

string defaultConnection = Environment.GetEnvironmentVariable("ConnectionStrings:DefaultConnection");

這在本地工作正常,但在 Azure 上,defaultConnection 是 null。 我在函數的應用程序設置的連接字符串部分下定義了連接。

在此處輸入圖像描述

我的方法對 Azure function v3 是否正確?

您需要指定連接字符串前綴(請參閱文檔):

Environment.GetEnvironmentVariable("CUSTOMCONNSTR_DefaultConnection");

請注意

如果您使用實體框架,則連接字符串只能與 function 應用程序一起使用。 對於其他情況,請使用應用程序設置。

所以如果你只是想獲取DefaultConnection的值,你可以把它放在Application settings下,你可以這樣獲取

Environment.GetEnvironmentVariable("DefaultConnection");

對於帶有實體框架的 Azure function,請參考這篇文章

If you're using Microsoft.NET.Sdk.Functions 3.0.11 and Microsoft.Azure.Functions.Extensions 1.1.0 and you're using Azure Functions with Dependency Injection, you can do the following to access the connection string (or any配置)當您啟動應用程序時。

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"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM