使用 IOptions<> 将应用程序设置注入 Azure Functions v3.0

Injecting Application Settings into Azure Functions v3.0 using IOptions<>

如何使用 IOptions<>将应用程序设置注入 Azure Functions v3.0

始终获取 JwtIssuerOptions 空值

假设我们的设置文件,local.settings.json包含以下数据:

 {
  "IsEncrypted": false,
 "Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"SqlConnectionString": "Server"
},
"JwtIssuerOptions": {
"JwtKey": "rpqru",
"JwtIssuer": "mydomain",
"JwtExpireDays": 7,
"JwtExpireMinutes": 240,
"JwtAuidence": "localhost"
  }
}

函数启动

public class 启动:FunctionsStartup {

  public override void Configure(IFunctionsHostBuilder builder)
    {
        var localConfig = new ConfigurationBuilder()
             .SetBasePath(Environment.CurrentDirectory)
             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
           .AddEnvironmentVariables()
           .Build();           
        builder.Services.AddOptions<JwtIssuerOptions>().Configure<IConfiguration>((settings, configuration) =>
        {
            configuration.Bind("JwtIssuerOptions", settings);
        });
    }

JwtIssuerOptions Class 点赞

public class JwtIssuerOptions

  {
        
    public string JwtKey { get; set; }

    public string JwtIssuer { get; set; }

    public int JwtExpireDays { get; set; }

    public int JwtExpireMinutes { get; set; }

    public string JwtAuidence { get; set; }
}

像波纹管一样的功能

   public class MyFunctions
   
 {

  private readonly IOptions<JwtIssuerOptions> _options;

  public MyFunctions(IOptions<ApplicationSettings> applicationSettingsOptions, 
  IOptions<JwtIssuerOptions>  options)
  {
 // the named section of the configuration file "JwtIssuerOptions"
  _options;= options;
   }

   }

使用构建的本地配置中的部分配置所需的选项

函数启动

public override void Configure(IFunctionsHostBuilder builder) {
    IConfiguration localConfig = new ConfigurationBuilder()
         .SetBasePath(Environment.CurrentDirectory)
         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
       .AddEnvironmentVariables()
       .Build();
       
    builder.Services
        .Configure<JwtIssuerOptions>(localConfig.GetSection("JwtIssuerOptions"));
}

另一种方法是获取本地设置,将其添加到容器中

FunctionsStartup {
    public override void Configure(IFunctionsHostBuilder builder) {
        IConfiguration localConfig = new ConfigurationBuilder()
             .SetBasePath(Environment.CurrentDirectory)
             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
           .AddEnvironmentVariables()
           .Build();
        //load setting
        JwtIssuerOptions jwt = localConfig.GetSection("JwtIssuerOptions").Get<JwtIssuerOptions>();
        //register with container
        builder.Services.AddSingleton<JwtIssuerOptions>(jwt);
    }
}

并将其显式注入函数

public class MyFunctions {

    private readonly JwtIssuerOptions jwtOptions;

    public MyFunctions(IOptions<ApplicationSettings> applicationSettingsOptions, 
        JwtIssuerOptions jwtOptions) {
        this.jwtOptions = jwtOptions;

        //...
    }
    
    //...
}

首先,创建一个文件名 ApplicationSettings.cs,其中包含您在 EnvironmentVariables 中使用的所有属性名称,好吗?

public class ApplicationSettings
{
    public string DatabaseConnectionString { get; set; }
    public string valueXYZ { get; set; }
    public string CacheConnectionString { get; set; }
} 

之后,最好的方法是使用启动文件将此配置加载到内存中(如果您的项目不存在,请创建)

[ExcludeFromCodeCoverage]
public class Startup : FunctionsStartup
{
    private IConfiguration _configuration = null;

    public override void Configure(IFunctionsHostBuilder builder)
    {
        var serviceProvider = builder.Services.BuildServiceProvider();
        _configuration = serviceProvider.GetRequiredService<IConfiguration>();

        SetupMaps
            .ConfigureMaps();

        builder
            .Services
            .AddLogging();

        builder
            .Services
            .AddOptions<ApplicationSettings>() <-- YOUR CLASS
            .Configure<IConfiguration>(
            (settings, configuration) => { configuration.Bind(settings); });
    }

=== 现在在一些 class

中使用
namespace YOUR_NAMESPACE
{
    public class YOUR_CLASS: YOUR_INTERFACE
    {
    private readonly ApplicationSettings _applicationSettings;

    public YOUR_CLASS(
        IOptions<ApplicationSettings> applicationSettings)
    {
        _applicationSettings = applicationSettings.Value;
    }

    public async Task<RETURN_CLASS> METHOD_NAME(string PARAMETER)
    {
    //HERE YOU CAN USE _applicationSettings.PROP
    }