Skip to content

Improving AppSettings Processing

Steve Ives edited this page Jan 12, 2022 · 14 revisions

The Harmony Core SolutionTemplates include code and sample configuration files for a mechanism that allows environment variables that are required at runtime to be specified via the special appsettings.json file. This mechanism is described in detail in the Environment Variables documentation.

Earlier versions of the solution templates contained code that did not operate as intended, and this document contains instructions on how to alter the supplied code to provide a fully working solution.

Determining if Your Code Needs Fixing

To determine if your code needs this fix to be applied, look in your Services.Models project directory and edit the file called AppSettings.dbl. If you look towards the end of that file and find a class named AppEnvironmentVariable that looks like this:

;;; <summary>
;;; Represents a aingle environment variable.
;;; </summary>
public class AppEnvironmentVariable

    ;;; <summary>
    ;;; The name of the environment variable.
    ;;; </summary>
    public readwrite property Name, string

    ;;; <summary>
    ;;; The value of the environment variable.
    ;;; </summary>
    public readwrite property Value, string

endclass

Then your code needs updating. If the class is not present then your code has already been fixed.

Fixing the Code

Correcting the Code in AppSettings.dbl

  1. Edit Services.Models\AppSettings.dbl and remove the definition of the AppEnvironmentVariable class (shown above).

  2. Change the definition of the EnvironmentVariables property from this:

public readwrite property EnvironmentVariables, @List<AppEnvironmentVariable>, new List<AppEnvironmentVariable>()

To this

public readwrite property EnvironmentVariables, @Dictionary<string,string>, new Dictionary<string, string>()
  1. Change the code in the public method ProcessEnvironmentVariables from this:
data ev, @AppEnvironmentVariable
foreach ev in EnvironmentVariables
begin
    data sts, int
    xcall setlog(ev.Name,ev.Value,sts)
end

To this:

data ev, @KeyValuePair<string,string>
foreach ev in EnvironmentVariables
begin
    data sts, int
    xcall setlog(ev.Key,ev.Value,sts)
end
  1. Save and close the source file.

Correcting the Data in appsettings.json

  1. Edit the file Services.Host\appsettings.json

The file as originally shipped looked like this:

{
  "AppSettings": {
    "HttpPort": 80,
    "HttpsPort": 443,
    "EnvironmentVariables": [
      {
        "Name": "HARMONY_TOKEN_DURATION",
        "Value": "1"
      },
      {
        "Name": "HARMONY_LOG_LEVEL",
        "Value": "2"
      }
    ]
  }
}

The HttpPort and HttpsPort settings were never used and can be removed, and the EnvironmentVariables section must now be changed from being an array of objects with Name and Value properties to an array of named string properties.

  1. Alter the content of the file to look like this:
{
  "AppSettings": {
    "EnvironmentVariables": {
      "HARMONY_TOKEN_DURATION": "1",
      "HARMONY_LOG_LEVEL": "2"
    }
  }
}

Correcting the Data in Other AppSettings Files

If your environment includes configuration files for other environments (e.g. appsettings.Development.json or appsettings.Production.json) then apply similar changes to those files also.

For example, in the sample environment created from the Harmony Core solution templates, you would have a file named appsettings.Development.json and would need to change this:

{
  "AppSettings": {
    "HttpPort": 8085,
    "HttpsPort": 8086,
    "EnvironmentVariables": [
      {
        "Name": "HARMONY_TOKEN_DURATION",
        "Value": "8767"
      },
      {
        "Name": "HARMONY_LOG_LEVEL",
        "Value": "6"
      }
    ]
  }
}

To this:

{
  "AppSettings": {
    "EnvironmentVariables": {
      "HARMONY_TOKEN_DURATION": "8767",
      "HARMONY_LOG_LEVEL": "6"
    }
  }
}
Clone this wiki locally