One of the biggest flaws I wanted to correct with DasBlog Core was that if you updated the underlying configuration file it would take a site restart for the settings to be refreshed. For example if I want to change Theme or the site name, I would need to initiate a site restart. This was clearly untenable. I had also went through the hassle of creating a DasBlog CLI but it was useless without a reset.

Columbus Zoo

So the following is how I originally set this up, I added the xml file using the XmlConfigurationProvider, note that I used the reloadOnChange which I assume uses a FileSystemWatcher and refreshes when the file changes:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddXmlFile("DasBlogSettings.xml", optional: true, reloadOnChange: true)
        …

ASP.NET Core supports dependency injection, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies, the following code sets that up for me:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.Configure(Configuration);
    services.Configure(Configuration);
    ...
    services
        .AddTransient();
}

My DasBlogSettings class uses the options pattern to pull in the settings across my entire site, however, it does not pick up refreshed values:

public class DasBlogSettings : IDasBlogSettings
{
    public IMetaTags MetaTags { get; set; }
    public ISiteConfig SiteConfiguration { get; set; }

    public DasBlogSettings(IOptions siteConfig, IOptions metaTagsConfig)
    {
        SiteConfiguration = siteConfig.Value;
        MetaTags = metaTagsConfig.Value;
    }
} 

So the following is the corrected code, I use IOptionsMonitor to retrieve the current Snapshot values of the IMetaTags and ISiteConfig:

public class DasBlogSettings : IDasBlogSettings
{
    public IMetaTags MetaTags { get; set; }
    public ISiteConfig SiteConfiguration { get; set; }
    public DasBlogSettings(IOptionsMonitor siteConfig, IOptionsMonitor metaTagsConfig)
    {
        SiteConfiguration = siteConfig.CurrentValue;
        MetaTags = metaTagsConfig.CurrentValue;
    }
}

Now I can happily update settings via the site or CLI and immediately see the changes everywhere without downtime. Basic, but still … yay!


Share on Twitter Facebook

Comment Section

Comments are closed.