本地发布的C# API未能从appsettings.json文件中获取值。

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

Locally published C# API not getting values from appsettings.json

问题

I wanted to run metrics against my .NET 7 API. To do so, I read, to get the most accurate metrics, I should publish it. But, when I do, my application is not getting the values from my appsettings.json.

Both my appsettings.json and appsettings.development.json look like this:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "MongoDbOptions": {
    "ConnectionString": "mongodb://localhost",
    "DatabaseName": "testDb"
  },
  "AllowedHosts": "*",
  "ApplicationOptions": {
    "ShowExceptions": true,
    "ValidateQuery": true
  }
}

In my Program.cs file, I configure them via:

builder.Services.Configure<MongoDbOptions>(builder.Configuration.GetSection(nameof(MongoDbOptions)));
builder.Services.Configure<ApplicationOptions>(builder.Configuration.GetSection(nameof(ApplicationOptions)));

They are injected into the code via:
IOptions<ApplicationOptions> applicationOptions and IOptions<MongoDbOptions> mongoDbOptions.

Everything works fine when I run the API in VS Code. I can run queries against my local mongo db and I can access the values of the ApplicationOptions.

In a console running as Admin, I publish it locally via:

dotnet publish -c Release -o ./bin/Publish

Then, run it:

dotnet D:\GitLocal/CrudMetrics/bin/Publish/CrudMetrics.Api.dll --urls "https://localhost:7279"

When I call a route via Postman that hits my local mongo db, I get this error:

MongoDB.Driver.MongoConfigurationException: The connection string '' is not valid.

To test that it was getting any value from the appsettings.json, in my controller where IOptions<ApplicationOptions> applicationOptions is injected into, I instantly throw an exception to see if I get the value from the application options.

throw new Exception($"Value of _applicationOptions.ShowExceptions:{_applicationOptions.ShowExceptions}.");

This returns this error:

System.Exception: Value of _applicationOptions.ShowExceptions:False.

Obviously, it is fine that it is an error, but you can see that ShowExceptions is false, but in the appsettings.json it is true.

I can see both the appsettings.json and appsettings.development.json files are in the same directory as the CrudMetrics.Api.dll file. If I open them in Notepad, I can see the correct, expected values that are above and also used when running it via VS Code.

But, for some reason, these values are not getting pulled in. What am I missing?

英文:

I wanted to run metrics against my .NET 7 API. To do so, I read, to get the most accurate metrics, I should publish it. But, when I do, my application is not getting the values from my appsettings.json.

Both my appsettings.json and appsettings.development.json look like this:

{
  &quot;Logging&quot;: {
    &quot;LogLevel&quot;: {
      &quot;Default&quot;: &quot;Information&quot;,
      &quot;Microsoft.AspNetCore&quot;: &quot;Warning&quot;
    }
  },
  &quot;MongoDbOptions&quot;: {
    &quot;ConnectionString&quot;: &quot;mongodb://localhost&quot;,
    &quot;DatabaseName&quot;: &quot;testDb&quot;
  },
  &quot;AllowedHosts&quot;: &quot;*&quot;,
  &quot;ApplicationOptions&quot;: {
    &quot;ShowExceptions&quot;: true,
    &quot;ValidateQuery&quot;: true
  }
}

In my Program.cs file, I configure them via:

builder.Services.Configure&lt;MongoDbOptions&gt;(builder.Configuration.GetSection(nameof(MongoDbOptions)));
builder.Services.Configure&lt;ApplicationOptions&gt;(builder.Configuration.GetSection(nameof(ApplicationOptions)));

They are injected into the code via:
IOptions&lt;ApplicationOptions&gt; applicationOptions and IOptions&lt;MongoDbOptions&gt; mongoDbOptions.

Everything works fine when I run the API in VS Code. I can run queries against my local mongo db and I can access the values of the ApplicationOptions.

In a console running as Admin, I publish it locally via:

dotnet publish -c Release -o ./bin/Publish

Then, run it:

dotnet D:\GitLocal/CrudMetrics/bin/Publish/CrudMetrics.Api.dll --urls &quot;https://localhost:7279&quot;

When I call a route via Postman that hits my local mongo db, I get this error:

> MongoDB.Driver.MongoConfigurationException: The connection string '' is not valid.

To test that it was getting any value from the appsettings.json, in my controller where IOptions&lt;ApplicationOptions&gt; applicationOptions is injected into, I instantly throw an exception to see if I get the value from the application options.

throw new Exception($&quot;Value of _applicationOptions.ShowExceptions:{_applicationOptions.ShowExceptions}.&quot;);

This returns this error:

> System.Exception: Value of _applicationOptions.ShowExceptions:False.

Obviously, it is fine that it is an error, but you can see that ShowExceptions is false, but in the appsettings.json it is true.

I can see both the appsettings.json and appsettings.development.json files are in the same directory as the CrudMetrics.Api.dll file. If I open them in Notepad, I can see the correct, expected values that are above and also used when running it via VS Code.

But, for some reason, these values are not getting pulled in. What am I missing?

答案1

得分: 1

问题是您从与“./Publish/”文件夹不同的位置运行应用程序。应该将“appsetings.json”的位置设定为您启动应用程序的位置。

尝试从“Publish”文件夹中运行dotnet CrudMetrics.Api.dll --urls "https://localhost:7279"

英文:

The problem is that you are running the app from another location then the ./Publish/ folder. The location of the appsetings.json is supposed to be in the location from where you are starting the application.

Try running dotnet CrudMetrics.Api.dll --urls &quot;https://localhost:7279&quot;from the Publish folder.

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

发表评论

匿名网友

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

确定