在 FunctionsStartup 中使用 ConfigurationBuilder [英] Using ConfigurationBuilder in FunctionsStartup

查看:43
本文介绍了在 FunctionsStartup 中使用 ConfigurationBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Azure 函数,我正在使用 DI 系统来注册一些类型;例如:

I have an Azure function, and I'm using the DI system in order to register some types; for example:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services
            .AddTransient<IMyInterface, MyClass>()
    . . . etc

但是,我还需要从我的环境设置中注册一些数据.在函数本身内部,我可以获得 ExecutionContext,所以我可以这样做:

However, I also was to register some data from my environment settings. Inside the function itself, I can get the ExecutionContext, and so I can do this:

IConfiguration config = new ConfigurationBuilder()
   .SetBasePath(context.FunctionAppDirectory)
   .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
   .AddEnvironmentVariables()
   .Build();

但是,在 FunctionsStartup 中,我无权访问 ExecutionContext.有没有一种方法可以从 FunctionsStartup 类中获取 ExecutionContext,或者另一种方法来确定当前运行目录,以便我可以设置基本路径?

However, in the FunctionsStartup, I don't have access to the ExecutionContext. Is there a way that I can either get the ExecutionContext from the FunctionsStartup class or, alternatively, another way to determine the current running directory, so that I can set the base path?

推荐答案

Azure Functions (v2) 中不需要任何配置对象.所有应用程序设置都作为环境变量注入.所以你可以做一个简单的 Environment.GetEnvironmentVariable()

You don't need any Configuration object in Azure Functions (v2). All the app settings get injected as Environment variables. So you can do just a simple Environment.GetEnvironmentVariable()

在本地运行时,local.settings.json 以相同的方式读取.

When running locally the local.settings.json gets read in the same way.

请参阅此处:https://docs.microsoft.com/en-us/sandbox/functions-recipes/environment-variables?tabs=csharp

这篇关于在 FunctionsStartup 中使用 ConfigurationBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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