简体   繁体   English

如何从 appsettings.json 读取 AWS RegionEndpoint?

[英]How to read AWS RegionEndpoint from appsettings.json?

Blazor Server. Blazor 服务器。 I have a model我有一个 model

public class S3SvcConfiguration: IS3SvcConfiguration
{
    public string AccessKey { get; set; }
    public string SecretKey { get; set; }
    public RegionEndpoint RegionPoint { get; set; }
    public string S3Url { get; set; }
}

But when I read the appsettings.json configuration the RegionEndpoint is null: The RegionEndpoint is AWS type.但是当我阅读 appsettings.json 配置时,RegionEndpoint 是 null:RegionEndpoint 是 AWS 类型。

"S3SvcConfiguration": {
    "RegionPoint": "us-east-2",
    "S3Url": "http://myhost:9000",
    "AccessKey": "qwerty",
    "SecretKey": "qwerty123"
  },

I read it我读了

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddServerSideBlazor();
    services.AddSingleton<IS3SvcConfiguration>(Configuration.GetSection("S3SvcConfiguration").Get<S3SvcConfiguration>());
    services.AddScoped <IS3FileSvc,S3FileSvc> ();
}

ALl values populated but RegionPoint is null. How to read appsettings.json configuration to populate the RegionPoint value?填充了所有值,但 RegionPoint 为 null。如何读取 appsettings.json 配置以填充 RegionPoint 值?

RegionEndpoint is part of their custom configuration but you try to populate it from a simple string when they would have had code to handle that. RegionEndpoint是他们自定义配置的一部分,但是当他们有代码来处理它时,您尝试从一个简单的字符串填充它。

Consider changing the approach to manually convert that string to the desired value考虑更改手动将该字符串转换为所需值的方法

public void ConfigureServices(IServiceCollection services) {
    services.AddRazorPages();
    services.AddServerSideBlazor();

    S3SvcConfiguration config = Configuration.GetSection("S3SvcConfiguration").Get<S3SvcConfiguration>();
    string region = Configuration.GetValue<string>("S3SvcConfiguration:RegionPoint");
    config.RegionPoint = RegionEndpoint.GetBySystemName(region);
    services.AddSingleton<IS3SvcConfiguration>(config);

    services.AddScoped <IS3FileSvc,S3FileSvc> ();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM