如何用JSON文件的内容覆盖Blazor WebAssembly的IConfiguration?

huangapple go评论58阅读模式
英文:

How to overwrite blazor wasm IConfiguration with content of a json file?

问题

你们大家好,

这是我的第一个问题,请友善一点。

我有一个Blazor WebAssembly客户端应用程序。在这里有一个用来保存配置的类:

public sealed class AppConfiguration
{
  public string MeIsAString { get; set; } = "Hello World!";
}

在Program.cs中,通过WebAssemblyHostBuilder.Configuration.AddJsonStream()添加了一个自定义JSON文件到WebAssemblyHostBuilder中:

var builder = WebAssemblyHostBuilder.CreateDefault();
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
var httpClient = new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) };
builder.Services.AddScoped(sp => httpClient);
builder.Configuration.AddJsonStream(await (await httpClient.GetAsync("path-to-json-file")).Content.ReadAsStreamAsync());

JSON文件的内容如下:

{
  "AppConfiguration": {
    "MeIsAString": "different string"
  }
}

通过以下方式在应用程序内部读取配置(Configuration是Microsoft.Extensions.Configuration.IConfiguration):

var s = Configuration.Get<AppConfiguration>().MeIsAString; // s现在是"Hello World!"

一切正常。

我的问题是:为什么MeIsAString没有被JSON文件的内容覆盖?

我已经在AddJsonStream()返回的内容上调用了Build(),并尝试将其添加到builder使用的配置中,但它没有覆盖MeIsAString。当在Program.cs中逐步执行代码时,我注意到成功注册了一个配置提供程序。我还注意到,如果没有AddJsonStream(),通过Configuration.Get<>()获取配置是不起作用的。

英文:

Hello you guys out there,

this is my first question, please be kind.

I have a blazor webassembly client application. In there there is a class to hold configurations:

public sealed class AppConfiguration
{
  public string MeIsAString { get; set; } = &quot;Hello World!&quot;;
}

In Program.cs a custom json file gets added to the WebAssemblyHostBuilder via WebAssemblyHostBuilder.Configuration.AddJsonStream():

var builder = WebAssemblyHostBuilder.CreateDefault();
builder.RootComponents.Add&lt;App&gt;(&quot;#app&quot;);
builder.RootComponents.Add&lt;HeadOutlet&gt;(&quot;head::after&quot;);
var httpClient = new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) };
builder.Services.AddScoped(sp =&gt; httpClient);
builder.Configuration.AddJsonStream(await (await httpClient.GetAsync(&quot;path-to-json-file&quot;)).Content.ReadAsStreamAsync());

The json file looks like this:

{
  &quot;AppConfiguration&quot;: {
    &quot;MeIsAString&quot; : &quot;different string&quot;,
  },
}

Reading the configuration from within the app via (Configuration is Microsoft.Extensions.Configuration.IConfiguration):

var s = Configuration.Get&lt;AppConfiguration&gt;().MeIsAString; // s is now &quot;Hello World!&quot;

works fine.

My question is: Why is MeIsAString not overwritten with the content of the json file?

I've called Build() on whats returned by AddJsonStream() and tryed to add that to the configuration used by builder, but it does not override MeIsAString. When stepping through the code in Program.cs i noticed that one configuration provider is successfully registered. I also noticed that getting the config via Configuration.Get<>() does not work without AddJsonStream().

答案1

得分: 0

我能够自己解决了这个问题。

问题在于,Configuration.Get&lt;AppConfiguration&gt;() 会获取 json 文件顶层括号中的所有内容,并匹配 AppConfiguration 的成员(仅当 json 文件不为空时才有效)。

因为 AppConfiguration 没有名为 AppConfiguration 的成员,所以 Configuration.Get&lt;AppConfiguration&gt;() 返回的实例只有来自 AppConfiguration.cs 的值。

因此,从 json 文件中移除 &quot;AppConfiguration&quot;:{...} 或使用 Configuration.GetSection(&quot;AppConfiguration&quot;).Get&lt;AppConfiguration&gt;() 都可以解决这个问题。

英文:

I was able to solve this issue by myself.

The problem is, that Configuration.Get&lt;AppConfiguration&gt;() takes everything in between of the top level parenthesis in the json file and from that what matches members of AppConfiguration (this behaving is only valid if the json file isn't empty).

Because AppConfiguration has no member named AppConfiguration, the instance returned by Configuration.Get&lt;AppConfiguration&gt;() has values only from AppConfiguration.cs.

So removing &quot;AppConfiguration&quot;:{...} from the json file or working with Configuration.GetSection(&quot;AppConfiguration&quot;).Get&lt;AppConfiguration&gt;() solves the issue.

huangapple
  • 本文由 发表于 2023年7月3日 18:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定