Azure Functions V2 DI-配置

时间:2019-09-10 15:13:30

标签: azure-functions

我正在使用this microsoft documentation来配置功能。<​​/ p>

我想从我的local.appsettings.json中提取一些设置,但是默认情况下,提供者来源仅包括appsettings.json。如果我将appsettings.json文件添加到项目中,则似乎无法读取它-我怀疑是因为它正在从其他位置读取全局文件。

如果我在Startup.Configure方法中为local.appsettings.json添加了一个配置,那么它 似乎完全替换了默认配置源

现在一般不建议使用local.appsettings.json来支持环境变量吗?

有没有办法将我的配置添加到默认列表中?

更新

我总体上想要实现的是在自定义Configure()类的Startup方法内加载连接字符串。

澄清

感谢您的回答,但是我的问题略有不同。我知道我可以为local.appsettings.json添加配置,但是如果有的话,可以在我的Configure方法中添加

builder.Services.Configure<MyFuncOptions>(MyLocalConfigurationRoot);

它将删除主机添加的ChainedConfigurationProviderMemoryConfigurationProviderJsonConfigurationProviderEnvironmentVariablesConfigurationProvder。我想将我的添加到现有提供商列表中。

此后,我读到这是不可能的。

最终解决方案

由于Marc和Bowman的回答,我坚持了他们的建议(并对其进行了投票),最终得到了一个自定义的FunctionsStartup类和一个FuncOptions类来注入函数。这使我可以在启动期间加载配置以用于配置服务,然后可以在其他地方使用它们。不好意思,我的问题一开始就不太恰当。

public class FunctionsStartp : Startup
{
        public override void Configure(IFunctionsHostBuilder builder)
        {
            IConfiguration configurationRoot = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("local.settings.json", true, true)
                .Build();

            builder.Services.Configure<FuncOptions>(configurationRoot.GetSection("Values"));

            var athenaConn = configurationRoot["Values:AthenaConnectionString"];

            builder.Services.AddDbContext<AthenaContext>(options =>
            {
                options.UseSqlServer(athenaConn);
            });
        }
    }
}

public class GetNewPlunetOrders
{
    private readonly IOptions<FuncOptions> _opts;

    public GetNewOrders(IOptions<FuncOptions> opts) { _opts = opts; }

    [FunctionName(nameof(GetNewOrders))]
    public async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, 
            ILogger log)
    {
        ...
        var d = _opts.Value.AthenaConnectionString;
        ...
    }
}

2 个答案:

答案 0 :(得分:2)

将设置放置在本地计算机上的位置将是local.settings.json文件。这是我添加TableStorageConnection的示例(在这种情况下,它在本地使用模拟存储):

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

然后在您的代码中使用以下方法检索连接字符串:

Environment.GetEnvironmentVariable("TableStorageConnection")

当您将功能发布/发布到Azure时,您仍然需要通过以下方法之一将生产连接字符串配置为应用程序设置:

  • The portal。不建议使用此方法,因为这是手动过程,可以被新的部署覆盖。
  • Azure CLI。比通过门户网站设置更好。
  • 在发布管道中。例如,当使用Azure DevOps时,可以使用Function Azure App Task并在那里设置appSettings。这将是我推荐的方法。

我真的更喜欢使用GetEnvironmentVariable方法,因此我不需要路径引用,该引用将因计算机而异(本地与云)。

答案 1 :(得分:1)

您可以使用它从local.setting.json获取连接字符串:

var config = new ConfigurationBuilder()
        .SetBasePath("D:\\Users\\bowmanzh\\source\\repos\\FunctionApp7\\FunctionApp2")
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();
var appParameter = "AzureWebJobsStorage";
string appsetting = config[$"{appParameter}"];

SetBasePath中的值是local.setting.json所在的网址,这是我的local.setting.json代码:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxw==;BlobEndpoint=https://bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxnimagestorage02.file.core.windows.net/",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

如果您只想在local.settings.json中获得连接字符串,仅使用我向您显示的代码就足够了。在我这边,我得到了连接字符串。如果您还有其他问题,请告诉我。