I am working a asp .net core web API 6.0 ( clean architecture project).
This is my appsettings.Production.json and appsettings.Development.json
{
"Tokens": {
"Key": "my-token",
"Issuer": "issuer",
"Audience": "audience",
"ExpirationInMinutes": 1440
},
// removed rest
}
using Microsoft.Extensions.Configuration;
public static IServiceCollection AddTokenAuthentication(this IServiceCollection services, IConfiguration config)
{
var secret = config.GetValue<string>("Tokens:Key"); // this value is null while running in development / production mode.
var key = Encoding.ASCII.GetBytes(secret);
// removed rest
}
The error I got in terminal while running in development/ production environment
Unhandled exception. System.ArgumentNullException: String reference not set to an instance of a String. (Parameter 's') at System.Text.Encoding.GetBytes(String s) at Infrastructure.Files.AuthenticationExtension.AddTokenAuthentication(IServiceCollection services, IConfiguration config) in /src/Infrastructure/Files/AuthenticationExtension.cs:line 14 at Infrastructure.DependencyInjection.AddInfrastructure(IServiceCollection services, IConfiguration configuration) in /src/Infrastructure/DependencyInjection.cs:line 75 at Program.<Main>$(String[] args) in/src/WebApi/Program.cs:line 21 Aborted (core dumped)
Note:
If I hard-code the value of secret
like this
var secret = "my-token";
then the project is running fine in both environment.
All of the environment variables are null while running in development and production environment.
Same error for GetConnectionString
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<DbContext>(options =>
options.UseNpgsql(
configuration.GetConnectionString("DefaultConnection"), // this is also null while running in both environment.. If I hard-coded then working fine
b => b.MigrationsAssembly(typeof(DbContext).Assembly.FullName))
.EnableSensitiveDataLogging());
// removed rest
}
Why this is happens?
Is there any mistakes in my code?
Please help me to find the mistake?
EDIT:
I also have tried copied all from appsettings.Development.json
and pasted it into appsettings.json
That does not work.
and here is my
launchSettings.json
"WebApi": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApi-Production": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7152;http://localhost:5105",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
3
Answers
Do you set the environment variable ASPNETCORE_ENVIRONMENT=Production in the production environment?
By default, ASP.NET Core will read config from appsettings.json file and appsettings.Environment.json file(e.g. appsettings.Development.json).
You can set ASPNETCORE_ENVIRONMENT=Production in production environment or set production config in appsettings.json file.
These documents may be help you:
—————-for example————–
My appsettings.json
Read config in code:
This is really Stupid. I was also running on this issue and been looking for an answer as the value kept getting null from any of the appSettings for almost all day until I reached your post today. What give me the clue was the picture posted by WHENJUN.
If you see the properties of the Configuration object, you can see the 2 appsettings.JSON are filled in his example. So I went ahead and put a breakpoint in my code and realized they are not loading either. Then I just said to myself, it is really stupid, but I am sure c# is looking for the actual root folder and not the application folder of the solution.
So, I moved my Appsettings files outside the folder I was working on since I created my test app on VSCode (this might be better documented somewhere on how to tell your app where these files are Supposed to be picked up from).
So in my case they are looking for the root of my app folder. Once I made the move, Iconfiguration started picking up the Options pattern as expected and I was able to get the values from Appsettings without issues. Thanks WenJun for the Visual, it saved me an all Nighter.
Let me know if this worked for you, here is a screenshot just in case:
Set your UAC to "Never" … OR
Run VS 2022 as administrator … we’ve run into this issue here also.
I’m not sure why UAC prevents a user that is Administrator from running VS 2022 and to access Environment variables … sounds like an OS bug or VS 2022 bug.