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"
}
},
dotnet watch -p src/WebApi run
(in development mode ) also not working . I got this errorUnhandled 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
But when debugging ( in development) is working fine. I have no idea why this happens