20

VS2015 Pro, .NET Core, Web Application.

I'm trying to figure out how to get objects out of appsettings, that have nested objects. I can do it with single level objects by creating a POCO with simple types that match the config names. But with more complex objects, it's not working. I'm getting top level objects back, but they are coming back null. Here's what I have so far:

StartUp.ConfigureServices:

enter image description here

appsettings.json:

enter image description here

POCO APIContext:

enter image description here

Using Class:

enter image description here

So I get my three API objects back but they are all null. I don't know enough about the Configuration in Startup.cs to know what it's supposed to look like, but here it is.

enter image description here

5
  • What are the errors you are experiencing? Commented Mar 16, 2017 at 14:58
  • @koelkastfilosoof Sorry, I made some changes mid typing of that question. No more errors, but I updated the question to show that my API objects are coming back null. Commented Mar 16, 2017 at 15:21
  • Can you put a breakpoint in the Configure method in your Startup and check the IConfiguration? Verify that all the settings are loaded correctly Commented Mar 16, 2017 at 16:02
  • @koelkastfilosoof I added an image of my configuration members, but I'm not sure how it's supposed to look to begin with. Commented Mar 16, 2017 at 16:21
  • Please don't make code screenshots. Use copy-paste and code formatting instead. It is much easier to read and reproduce. Commented Apr 19, 2017 at 21:07

1 Answer 1

21

Nested options are supported and successfully deserialized from appsettings.json. However, fields instead of properties break built-in POCO object deserialization. With the properties the problem should disappear:

public API FirstAPI { get; set; }

public string Path { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works. And sorry about the screenshots, I will correct that on future posts.

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.