19

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?

4
  • 1
    "issuer" : "localhost:5000", <---> public string ValidIssuer { get; set; } ? Commented Dec 12, 2017 at 6:27
  • Ou...you're right I missed it. But the rest properties are bound properly Commented Dec 12, 2017 at 6:30
  • 2
    : cannot exist anywhere in the key name. Commented Jan 2, 2020 at 9:48
  • Damn, what are the chances, I made the same stupid mistake :') Commented Dec 11, 2023 at 18:50

1 Answer 1

30

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"
  }

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

You're right. Now it works. How I could missed it...? Thanks :)
What kind of project is this? I tried .NET core console app and added Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Json but error occurs: 'IConfigurationSection' does not contain a definition for 'Get' and no extension method 'Get' accepting a first argument of type 'IConfigurationSection' could be found.
This asp.net core web application
Thanks. I found it to be an ASP.NET MVC project. An empty ASP.NET core web app still can not figure out the Get<T> method. This method is actually declared in Microsoft.Extensions.Configuration.ConfigurationBinder in Microsoft.Extensions.Configuration.Binder.dll.
See Startup class in that project in that you can see ConfigureServices Method where we are going to write code for reading data from appsettings.json file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.