Azure Function v2和连接字符串 [英] Azure Function v2 and connection strings

查看:111
本文介绍了Azure Function v2和连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下local.settings.json文件:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",

    "SureMdmApiKey": "xxx",
    "SureMdmApiUrl": "xxx",
    "SureMdmUsername": "xxx",
    "SureMdmPassword": "xxx"
  },
  "ConnectionStrings": {
    "StorageConnectionString": "aaaa",
    "DataContext": "aaaa"
  }
}

然后在Startup.cs文件中有以下代码:

then I have the following code in Startup.cs file:

[assembly: FunctionsStartup(typeof(FunctionAppSureMdmSync.Startup))]
namespace FunctionAppSureMdmSync
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var services = builder.Services;

            services.AddTransient<ISureMdmService>(s => new SureMdmService(
                url: System.Environment.GetEnvironmentVariable("SureMdmApiUrl"),
                username: System.Environment.GetEnvironmentVariable("SureMdmUserName"),
                password: System.Environment.GetEnvironmentVariable("SureMdmPassword"),
                apiKey: System.Environment.GetEnvironmentVariable("SureMdmApiKey")
                ));

            var connString = System.Environment.GetEnvironmentVariable("ConnectionStrings:DataContext");
            services.AddDbContext<DataContext>(options => options
                .UseSqlServer(connString, x => x.UseNetTopologySuite()));


            services.AddTransient<ITabletGroupService, TabletGroupService>();
            services.AddTransient<ITabletService, TabletService>();
        }

    }
}

,并且在本地运行正常

但是当我将此功能发布到Azure Portal并转到配置"并将"DataContext"添加到连接字符串"时:

But when I publish this function to Azure Portal and go to "Configuration" and add "DataContext" to "Connection Strings":

我的代码不起作用:

            var connString = System.Environment.GetEnvironmentVariable("ConnectionStrings:DataContext");

如何获取连接字符串?我希望它在本地和Portal Azure上都能正常工作吗?

How to get connection string? I want that it works both, on local and Portal Azure?

推荐答案

我发现,要访问Startup中的IConfiguration,您需要构建一个临时服务提供商,而您只是将其丢弃.最近可能会有更好的方法,但是仍然可以正常工作:

I’ve found that to access IConfiguration in Startup, you need to build a temporary service provider, that you just throw away. There might be better ways more recently, but it still works fine:

var configuration = builder.Services.BuildServiceProvider().GetService<IConfiguration>(); 

然后,不要使用System.Environment.GetEnvironmentVariable(),而是使用IConfiguration(所有配置都相同):

Then, don’t use System.Environment.GetEnvironmentVariable(), but IConfiguration instead (same applies for all your configuration):

var connString = configuration.GetConnectionString("DataContext");

这篇关于Azure Function v2和连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆