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

查看:67
本文介绍了在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/zh-CN/sandbox/functions-recipes/environment-variables?tabs=csharp

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

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