Skip to content

Configuration

Serenity integrates with the built-in Configuration system of ASP.NET Core.

We also prefer the Options pattern where possible, which provides an abstraction of configuration retrieval, and from its storage location. This is achieved using the dependency injection pattern.

The configuration for a typical Serene / StartSharp application resides in the appsettings.json file by default but it is possible to move some parts or all of it to a database, cloud, or any other location.

appsettings.machine.json

In addition to the standard appsettings.json and appsettings.{Environment}.json files, the template also loads an optional appsettings.machine.json file. It is added in Program.cs:

return Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStaticWebAssets();
        webBuilder.UseStartup<Startup>();
    })
    .ConfigureAppConfiguration((builderContext, config) =>
    {
        config.AddJsonFile("appsettings.bundles.json");
        config.AddJsonFile("appsettings.machine.json", optional: true);
    });

Because it is loaded after the other appsettings files, any value it contains overrides the corresponding ones. It is intended for machine-specific local settings that differ from developer to developer, such as connection strings, SMTP settings, or toggling features locally.

appsettings.machine.json is local only:

  • It is ignored by source control — the template's .gitignore contains appsettings*.machine.json, so it is never committed.
  • It should not be deployed — it only exists on the machine it was created on.

If a value should take effect for everyone, put it in appsettings.json (or appsettings.{Environment}.json) instead.

Options Pattern

Here is a sample of configuration classes that are using the options pattern:

assembly class
Serenity.Net.Services ConnectionStringOptions
Serenity.Net.Services UploadSettings
Serenity.Net.Web CssBundlingOptions
Serenity.Net.Web ScriptBundlingOptions

Automatic Configuration with ConfigureSections

Each option class declares the name of the configuration section it binds to through the [DefaultSectionKey] attribute:

[DefaultSectionKey(SectionKey)]
public class ConnectionStringOptions : Dictionary<string, ConnectionStringEntry>,
    IOptions<ConnectionStringOptions>
{
    /// <summary>
    /// Default section key for ConnectionStringOptions
    /// </summary>
    public const string SectionKey = "Data";

    //...

}

The ConfigureSections extension method, called once in Startup.cs, scans the type source for every option class marked with [DefaultSectionKey] and automatically calls services.Configure<TOptions>(Configuration.GetSection(...)) for it:

services.ConfigureSections(Configuration);

With that single call, Serenity's built-in settings — connection strings, upload, script/CSS bundling, SMTP, ClamAV, membership, environment, and more — are all bound to their configuration sections. You no longer need to write the manual services.Configure<...> calls yourself.

You only have to configure an option class manually if it doesn't carry a [DefaultSectionKey] attribute, e.g. one of your own custom options classes:

services.Configure<MyCustomOptions>(Configuration.GetSection("MyCustom"));

Settings in the Database

Some settings are better changed by an administrator in the application than by editing appsettings.json on the server. An options class opts in with [DbSettings] and a [DefaultSectionKey] under DbSettings:, e.g. DbSettings:Site:

[DbSettings, DefaultSectionKey("DbSettings:Site")]
public class SiteDbSettings
{
    public string? SupportEmail { get; set; }
    public int PageSize { get; set; } = 20;
}
  • The public read/write properties of simple types (texts, numbers, booleans, enumerations, dates) are the settings. A property with [Ignore] is left out, and one that is read-only is shown but not changed.
  • A value is only ever provided under the DbSettings section, so a row of the table can't set a connection string, a secret or any other key: the provider reads only the rows of the settings that the classes declare, and not the read-only ones. [OverlayConfig] on a property also provides its value for one key of another section, so an existing options class gets it: [OverlayConfig(typeof(LocalizationSettings), nameof(LocalizationSettings.BaseLanguage))]. An overlay can't name the ConnectionStrings, Data or DbSettings sections, and two settings can't overlay the same key.
  • [RequiresRestart] marks a setting whose change applies after a restart, e.g. one that is read once from IOptions<T> rather than from IOptionsMonitor<T>.
  • The permission attributes work as on a row, and the texts are translated with the keys of a form of the section key, e.g. Forms.DbSettings.Site.PageSize and Forms.DbSettings.Site.PageSize_Hint, and the display name and the description of the class Forms.DbSettings.Site.GroupTitle and Forms.DbSettings.Site.GroupDescription, see Property Items.

Pro.Extensions keeps the values in the cfg_Settings table with services.AddDbSettings(Configuration), and StartSharp shows them on its Settings page, where a setting has a check box: ticked, its value is kept in the database (also when it is the default), unticked, the setting has its default and can't be edited. See Settings Page.

See the documents below for more information about ASP.NET Core configuration and options pattern:

Configuration in ASP.NET Core - Microsoft Docs

Options Pattern in ASP.NET Core - Microsoft Docs