英文:
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:
{
"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?
答案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 "https://localhost:7279"
from the Publish
folder.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论