I try to get my section from appsettings.json, and then bind it to the intance of MongoSettings class, but I have an exception which is:
"Cannot create instance of type 'System.String' because it is missing a public parameterless constructor."
It is strange, because I'm using the same method to get jwt settings.
Please take a look:
var jwtSettings = Configuration.GetSection("jwt").Get<JwtSettings>(); //it works
var mongoSettings = Configuration.GetSection("mongo").Get<MongoSettings>(); //it doesn't
appsettings.json
"Jwt": {
"issuer" : "localhost:5000",
"expiryMinutes" : 60,
"key" : "das#@4SD120847@12313"
},
"Mongo": {
"connection:" : "mongodb://localhost:27017",
"database" : "MemoTime"
}
MongoSettings:
public class MongoSettings
{
public string Connection { get; set; }
public string Database { get; set; }
}
JwtSettings:
public class JwtSettings
{
public string Key { get; set; }
public string ValidIssuer { get; set; }
public int ExpiryMinutes { get; set; }
}
As you can see, both clasess and sections in app settings looks similarly, so why getting settings for mongo does not work?
You Issue is in Json there is extra colon ":" at the end of key "connection:". That why it is giving error
Valid Json Data.
"Jwt":
{
"issuer": "localhost:5000",
"expiryMinutes": 60,
"key": "das#@4SD120847@12313"
},
"Mongo":
{
"connection": "mongodb://localhost:27017",
"database": "MemoTime"
}