使用依赖注入的Azure函数ICONFIG

发布于 2025-02-13 16:41:30 字数 824 浏览 0 评论 0原文

我需要来自配置文件的一些信息,我在我的local.settings.json中有此信息,

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "EmailProcessConnection": "",
    "SendDeveloperMails": 0,
    "Email": {
      "Email": "",
      "Password": ""
    },
   
  },
  "ConnectionStrings": {
    "HumanRisksDbContext": ""
  }
}

我有一个服务,该服务是从我的时间触发azure函数中调用的。它的构造函数就是这样,

public ControlRisksService(HumanRisksDbContext context, 
    ILogHandler logHandler, IConfiguration configuration)
{
    this.context = context;
    _logHandler = logHandler;
    _config = configuration;
}

我需要从_config那里获得这样的信息,稍后

_config.GetValue<bool>("SendDeveloperMails")

我该如何获取此信息?

I need some information from config file, I have this information in my local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "EmailProcessConnection": "",
    "SendDeveloperMails": 0,
    "Email": {
      "Email": "",
      "Password": ""
    },
   
  },
  "ConnectionStrings": {
    "HumanRisksDbContext": ""
  }
}

I have a service which is being called from my time trigger Azure Function. Its constructor is like this

public ControlRisksService(HumanRisksDbContext context, 
    ILogHandler logHandler, IConfiguration configuration)
{
    this.context = context;
    _logHandler = logHandler;
    _config = configuration;
}

I need to get information from _config like this later on,

_config.GetValue<bool>("SendDeveloperMails")

How can I get this information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨巷深深 2025-02-20 16:41:30

您可以从local.settings.json使用emovention Variable

var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();
var connString = config["ConnectionStrings"];

另外,您需要将更新的local.settings.json文件放在中d:\ myApp \ bin \ debug \ net6.0 \ local.settings.json位置,然后在启动文件中注册JSON program.cs如下:

var host = new HostBuilder()
    .ConfigureAppConfiguration(app =>
    {
        app.AddJsonFile("appsettings.json", optional: false, 
            reloadOnChange: true).AddEnvironmentVariables().Build();
    }).Build();

You can get information from the local.settings.json using EnvironmentVariable:

var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();
var connString = config["ConnectionStrings"];

Also, you need to put the updated local.settings.json file into the D:\MyApp\bin\Debug\net6.0\local.settings.json location, and then register the JSON in the startup file program.cs like below:

var host = new HostBuilder()
    .ConfigureAppConfiguration(app =>
    {
        app.AddJsonFile("appsettings.json", optional: false, 
            reloadOnChange: true).AddEnvironmentVariables().Build();
    }).Build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文