Skip to content

Configuration

Ian Johnson edited this page Oct 29, 2022 · 3 revisions

[ConfigurationModel]

Classes attribute with [ConfigurationModel] will be incorporated into the hardened configuration system. An interface will be generated and registered with the DI container

[ConfigurationModel]
public partial class ConfigurationClass 
{
  private string _someProperty = "defaultValue";
  [FromEnvironmentVariable("ENVIRONMENT_VARIABLE")]
  private string _otherPropery = "someValue";
}

// generated
public partial class Configuration : IConfigurationClass
{
  public string SomeProperty
  {
    get => _someProperty;
    set => _someProperty = value;
  }

  // this property will be assigned value from env. 
  public string OtherProperty
  {
    get => _otherProperty;
    set => _otherProperty = value;
  }
}

public interface IConfigurationClass
{
  string SomeProperty { get; }

  string OtherProperty { get; }
}

Runtime

To configure your application at runtime every Hardened module can have a Configure method where models can be configured.

[HardenedStartup]
public class Application
{
    // IEnvironment is an optional parameter
    private void Configure(IAppConfig config, IEnvironment environment)
    {
        // configure the max age for static context to 15 minutes
        config.Amend<StaticContentConfiguration>( 
            staticConfig => staticConfig.CacheMaxAge = 15);

        // configure response header values
        config.Amend<ResponseHeaderConfiguration>(responseHeader =>
        {
            responseHeader.Add("TestResponseHeader", "TestValue");
            responseHeader.Add(c => c.Response.Headers.Set("OtherTest", "OtherValue"));
        });
    }
}

IEnvironment

This interface acts as a wrapper around environment variables as well as provides an application environment name. By default this name is development but can be set using the HARDENED_ENVIRONMENT variable

Clone this wiki locally