I have read plenty of similar-ish questions, yet nothing worked for me. I am most likely doing something wrong but I don't see what.
Below is the config file I created, called ErrorCodes.json
at the root of the project (same level as appsettings.json
):
{
"ErrorMap": {
"Type": {
"Input": {
"Null": {
"Code": "ABC-1000",
"Message": "The request body is null or empty"
},
"Name": {
"Code": "ABC-1001",
"Message": "The 'name' property was not set"
},
"Age": {
"Code": "ABC-1002",
"Message": "The 'age' property was not set"
},
"Email": {
"Code": "ABC-1003",
"Message": "The 'email' property was not set"
}
}
}
}
}
In startup.cs
I have the following constructor:
public Startup()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("ErrorCodes.json", optional: false, reloadOnChange: false)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);
this.configuration = builder.Build();
}
And my ConfigureServices
method is here:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ErrorMap>(configuration.GetSection(nameof(ErrorMap)));
services.AddSingleton(
context => context.GetRequiredService<IOptions<ErrorMap>>().Value);
)
services.AddSingleton<ErrorCodeConfig>();
// some other stuff
}
ErrorCodeConfig is a "bean" (not sure about the name for that in the C# world) that depends on ErrorMap
, it's defined as such:
public class ErrorCodeConfig
{
private readonly ErrorMap _errors;
public ErrorCodeConfig(ErrorMap errors)
{
_errors = errors;
}
public Error Get(string typeKey, string errorKey)
{
return _errors.Get(typeKey, errorKey);
}
}
The class ErrorMap
and Error
are defined as such:
public class ErrorMap
{
public Dictionary<string, Dictionary<string, Error>> Type;
public Error Get(string key, string key2)
{
return Type.GetValueOrDefault(key).GetValueOrDefault(key2);
}
}
public class Error
{
public string Code { get; set; }
public string Message { get; set; }
}
I can run the application, everything gets injected properly except the Dictionary
in the ErrorMap
class. It remains null
and I am not sure why.
Here is what I have tried so far:
ConfigurationBuilder
to see if it was a read problem, so I temporarily renamed my file, and as it could not find it it threw an exception. So I'm assuming that at the very least it can find the file.ErrorMap
block to see if I could have it injected with the default config file, but I got no luck. It didn't crash but the config class was still not populated.Would you have any ideas of what I did wrong in my project? Thanks for reading
Tested on my computer with
var errorMap = new ErrorMap();
Configuration.GetSection(nameof(ErrorMap)).Bind(errorMap);
with this version of ErrorMap
public class ErrorMap
{
public Dictionary<string, Dictionary<string, Error>> Type { get; set; }
public Error Get(string key, string key2)
{
return Type.GetValueOrDefault(key).GetValueOrDefault(key2);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.