ASP.NET Monsters #10: Loading Settings From a Database in ASP.NET Core

Sign in to queue

Description

While configuration has been greatly simplified in ASP.NET Core, static JSON on the server won't provide an answer for all the situations all of the time. One such case is when you need to save and load configuration settings from a database so, here, we'll show you how.

In this episode, Monster James walks us through the components required to build out a simple, custom configuration provider that can read in your settings from a database.  The Monsters also talk through the mechanics and importance of overriding values of settings when using multiple sources for configuration.

You can follow the Monsters on Twitter and check out our blog for related posts.

The production code for this episode is hh.

Embed

Download

Download this episode

The Discussion

  • User profile image
    Peter Puglisi

    Hello, I think I may have a solution for your contest. How do I submit it ?
    Cheers

  • User profile image
    Vaso

    QW1hemluZyB2aWRlb3MsIGtlZXAgZG9pbmchIDop

  • User profile image
    Vaso

    Just submit it here :)

  • User profile image
    Peter Puglisi

    Okay guys, here's my contest entry:

    Append the production codes together in order of the production series.

    As of episode 10, you will get VGhlIE1vbnN0ZXIGIGhh

    Base64 decode this to ASCII - you can use https://www.base64decode.org/.

    Decoded text is => The Monster ha

    So maybe this was eventually leading to "The Monster has 3 heads" or something like that :)

  • User profile image
    Vaso

    Well done! This is it! :) I think you should be the winner :) Let's see what ASP.NET Monsters say!

  • User profile image
    peterpuglisi

    @Vaso:Thanks Vaso, I will await the official take from the Monsters! :)

  • User profile image
    BrunoLM

    Nice video. I can't wait to see how you separate the database and services in class libraries and use DI.

  • User profile image
    Canadian​James

    @peterpuglisi:You got it! It's not quite right, but you solved the puzzle! The phrase is base64 encoded and broken down into two-character "production codes" in order of the episodes, so you knocked it out of the park. Ep 11 & 12 are already in post-production, but we'll make the announcement soon! Can you DM or email me so we can connect about the prize?

  • User profile image
    Canadian​James

    @Vaso: looks like you were on to us as well ;)

  • User profile image
    Canadian​James

    @BrunoLM: Thanks Bruno, glad you're enjoying the series and I hope you check out Episode 12 which covers the basics of DI. I like your angle there on breaking it out into DLLs, so I'll add that to our docket. Cheers!

  • User profile image
    peterpuglisi

    @CanadianJames:That's great to hear James! I've sent you a connection request on LinkedIn. Btw, I'm finding the course content really useful and practical and wanted to say thanks to the ASP.NET Monsters team for making it all happen. Cheers!

  • User profile image
    Jovan

    You have simple key:values in table in your example. Is there some example of storing JSON from appsettings.json file into database and using these values the same way as the JSON values from appsettings.json? We could keep just connection string configuration in appsettings.json and move all other JSON nodes in database.
    This way some app admin can manage JSON values directly in database instead of opening static files.

  • User profile image
    Canadian​James

    @Jovan: Yes, you can do this. In the sample in the video, the configuration builder loads the connection string from the project.json. All your other settings could be in the database. There are certainly some places where it makes sense to make certain settings configurable at run time by a site administrator, particularly in the context of multi-tenant or self-hosted instances of your app, but there will also always be a place for environment-specific data to be stuffed into a static file. Remember that settings are loaded into your application at startup, so not all things are appropriate in the DB. 

    Thanks for tuning in!

  • User profile image
    Jasmine

    Hey don't know if anyone else got this error InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfigurationRoot' while attempting to activate 'Hope.Backend.Controllers.BackendController'.
    I fixed it by adding
    services.AddInstance<IConfiguration>(Configuration);
    to the ConfigureServices method in Startup.cs

  • User profile image
    Jasmine

    I had a question thou like for sitetitle if i switch to another page it's not there how do I get it to be on all the pages in the website

  • User profile image
    Saeef

    how can i get the code for this session?

    please send me the link

  • User profile image
    arsebiscuits

    @Saeef

    They mention using sample code from the ASP.Net docs, I think they're using this: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration

  • User profile image
    Steven​Francis

    Hello,

    I am trying to make a settings table but I don't want it to be just <key,value> pairs as I have complex objects or nested objects of database, since I use it from admin portal to make dynamic settings.

    I tried to follow this sample but didn't work since ConfigurationProvider uses IDictionary<string,string> and has no IDictionary<string,object>.

    So how do I approach that in .net core 2.0?

    Thanks in advance.

Add Your 2 Cents