dotnet hotchocolate azure function isolated (how to access conString and jwt options from host.json)

54 views Asked by At

enter image description here

How do I access connectionString and jwtSwitting in 'host.settings.json'?

like this (it's not hotchocolate project, it's normal dotnet api): appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "Microsoft.AspNetCore": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Default": "Server=localhost; User=root; Database=FINDUS; Password=xxxxxx"
  },
  "JwtSettings": {
    "SigningKey": "23kfioejrkdjfkelrkdoemvasde31sxnmd",
    "Issuer":"CwkSocial",
    "Audiences":["SwaggerUI"]
  }
}

Program.cs(access jwtSettings):

 var jwtSettings = new JwtSettings();
            builder.Configuration.Bind(nameof(JwtSettings), jwtSettings);
            var jwtSection = builder.Configuration.GetSection(nameof(JwtSettings));

            builder.Services.Configure<JwtSettings>(jwtSection);

the example is not hotchocolate project.

1

There are 1 answers

0
Qiang Fu On BEST ANSWER

For accessing variables in isolated local.settings.json you could try this:
JwtSettings.cs

    public class JwtSettings
    {
        public string? SigningKey { get; set; }
        public string? Issuer { get;set; }
        public List<string>? Audiences { get; set; }
    }
var config = new ConfigurationBuilder()
    .SetBasePath(Environment.CurrentDirectory)
    .AddJsonFile("local.settings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

JwtSettings jwtSettings = config.GetSection("JwtSettings").Get<JwtSettings>();

Test
enter image description here