Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create instance of type 'System.String'

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?

like image 998
bielu000 Avatar asked Dec 12 '17 06:12

bielu000


1 Answers

You Issue is in Json there is extra colon ":" 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"
  }

enter image description here

enter image description here

like image 69
Saineshwar Avatar answered Nov 11 '22 03:11

Saineshwar