Difference between revisions of "Intrinsic Component Configuration"

From OPC Labs Knowledge Base
Jump to navigation Jump to search
Line 7: Line 7:
 
* In .NET console apps: [https://docs.microsoft.com/en-us/dotnet/core/extensions/configuration Configuration in .NET] (Microsoft).
 
* In .NET console apps: [https://docs.microsoft.com/en-us/dotnet/core/extensions/configuration Configuration in .NET] (Microsoft).
 
By far, the most commonly used configuration provider will probably the [https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/ JSON configuration provider], and you will place your configuration data into the ''appsettings.json'' or ''appsettings.<code>Environment</code>.json'' file (e.g. ''appsettings.Production.json'').
 
By far, the most commonly used configuration provider will probably the [https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/ JSON configuration provider], and you will place your configuration data into the ''appsettings.json'' or ''appsettings.<code>Environment</code>.json'' file (e.g. ''appsettings.Production.json'').
 +
  
 
= Note on Programmatic Component Configuration =
 
= Note on Programmatic Component Configuration =
Line 14: Line 15:
 
# If the component supports programmatic component configuration, and the code that creates the component specifies a configuration interface, using a constructor designed for IoC (Inversion of Control), the configuration given by the specified interface is applied (this, again, can involve multiple configuration providers in specific sequence). Only instance members of the component can be configured in this way.
 
# If the component supports programmatic component configuration, and the code that creates the component specifies a configuration interface, using a constructor designed for IoC (Inversion of Control), the configuration given by the specified interface is applied (this, again, can involve multiple configuration providers in specific sequence). Only instance members of the component can be configured in this way.
 
If the programmatic component configuration is used, the initial values (of instance members) that the component will end up using may be different from what you specify using the built-in component configuration feature.
 
If the programmatic component configuration is used, the initial values (of instance members) that the component will end up using may be different from what you specify using the built-in component configuration feature.
 +
 +
= Changing the Configuration Providers =
 +
If your application requires a set of configuration providers different from the default, you can configure them differently through <code>IHostBuilder</code> interface by accessing the result value of <code>StaticHost.GetHostBuilder()</code>. For example, to support a possibility to configure component parameters by key pairs from INI files, add the following code at the beginning of your program:
 +
<syntaxhighlight lang="csharp">
 +
StaticHost.GetHostBuilder().ConfigureAppConfiguration((hostingContext, config) =>
 +
{
 +
    IHostEnvironment hostEnvironment = hostingContext.HostingEnvironment;
 +
    config
 +
        .AddIniFile("appsettings.ini", optional: true, reloadOnChange: false)
 +
        .AddIniFile($"appsettings.{hostEnvironment.EnvironmentName}.ini",
 +
            optional: true, reloadOnChange: false);
 +
});
 +
</syntaxhighlight>
  
 
= Component Support =
 
= Component Support =

Revision as of 13:23, 18 January 2021

Introduction

The parameters of main QuickOPC components can be configured externally, without you having to write a specific code for it. The external configuration can be provided by multiple means, e.g. settings file (such as appsettings.json), environment variables, or Azure App configuration. This feature allows tweaking of component parameters for experiments, testing, and in production. Its main benefit is that the component parameters can be changed without somebody having to implement the code for it first, and/or without having to change and rebuild the application (which may come really handy if rebuilding the application is not an option for you, e.g. in many restricted production environments).

Where the configuration is read from (the "configuration providers") is determined by the default settings for your application host. For the usual hosts, these defaults are described here:

By far, the most commonly used configuration provider will probably the JSON configuration provider, and you will place your configuration data into the appsettings.json or appsettings.Environment.json file (e.g. appsettings.Production.json).


Note on Programmatic Component Configuration

The sequence of high-level component configuration evaluation is as follows:

  1. The values are set to their (constant, documented) defaults (both static and instance members).
  2. Built-in configuration, if present, is applied (this can involve multiple configuration providers in specific sequence). This affects both the static and instance members of the component.
  3. If the component supports programmatic component configuration, and the code that creates the component specifies a configuration interface, using a constructor designed for IoC (Inversion of Control), the configuration given by the specified interface is applied (this, again, can involve multiple configuration providers in specific sequence). Only instance members of the component can be configured in this way.

If the programmatic component configuration is used, the initial values (of instance members) that the component will end up using may be different from what you specify using the built-in component configuration feature.

Changing the Configuration Providers

If your application requires a set of configuration providers different from the default, you can configure them differently through IHostBuilder interface by accessing the result value of StaticHost.GetHostBuilder(). For example, to support a possibility to configure component parameters by key pairs from INI files, add the following code at the beginning of your program:

StaticHost.GetHostBuilder().ConfigureAppConfiguration((hostingContext, config) =>
{
    IHostEnvironment hostEnvironment = hostingContext.HostingEnvironment;
    config
        .AddIniFile("appsettings.ini", optional: true, reloadOnChange: false)
        .AddIniFile($"appsettings.{hostEnvironment.EnvironmentName}.ini",
            optional: true, reloadOnChange: false);
});

Component Support

The built-in component configuration feature is supported by following components, and for the data members listed:

EasyAEClient
AdaptableParameters, SharedParameters, InstanceParameters, IsolatedParameters.
EasyDAClient
AdaptableParameters, SharedParameters, InstanceParameters, IsolatedParameters.
EasyUAClient
AdaptableParameters, SharedParameters, InstanceParameters, IsolatedParameters.
EasyUASubscriber
AdaptableParameters, SharedParameters, InstanceParameters, IsolatedParameters.