簡體   English   中英

如何從 appsettings.json 讀取 AWS RegionEndpoint?

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

Blazor 服務器。 我有一個 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; }
}

但是當我閱讀 appsettings.json 配置時,RegionEndpoint 是 null:RegionEndpoint 是 AWS 類型。

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

我讀了

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

填充了所有值,但 RegionPoint 為 null。如何讀取 appsettings.json 配置以填充 RegionPoint 值?

RegionEndpoint是他們自定義配置的一部分,但是當他們有代碼來處理它時,您嘗試從一個簡單的字符串填充它。

考慮更改手動將該字符串轉換為所需值的方法

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