From f83916d430e3442830c65fa38389afd3fae191aa Mon Sep 17 00:00:00 2001 From: NextTurn <45985406+NextTurn@users.noreply.github.com> Date: Sat, 28 Mar 2020 00:00:00 +0800 Subject: [PATCH] Load environment variables first --- src/Core/WinSWCore/ServiceDescriptor.cs | 43 ++++++++++++++----------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Core/WinSWCore/ServiceDescriptor.cs b/src/Core/WinSWCore/ServiceDescriptor.cs index 97a68fc..ce4c5c2 100755 --- a/src/Core/WinSWCore/ServiceDescriptor.cs +++ b/src/Core/WinSWCore/ServiceDescriptor.cs @@ -19,6 +19,8 @@ namespace winsw // ReSharper disable once InconsistentNaming protected readonly XmlDocument dom = new XmlDocument(); + private readonly Dictionary environmentVariables; + public static DefaultWinSWSettings Defaults { get; } = new DefaultWinSWSettings(); /// @@ -83,6 +85,8 @@ namespace winsw // Also inject system environment variables Environment.SetEnvironmentVariable(WinSWSystem.ENVVAR_NAME_SERVICE_ID, Id); + + this.environmentVariables = this.LoadEnvironmentVariables(); } /// @@ -93,6 +97,8 @@ namespace winsw #pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable. { this.dom = dom; + + this.environmentVariables = this.LoadEnvironmentVariables(); } // ReSharper disable once InconsistentNaming @@ -537,25 +543,7 @@ namespace winsw /// /// Environment variable overrides /// - public Dictionary EnvironmentVariables - { - get - { - Dictionary map = new Dictionary(); - XmlNodeList nodeList = dom.SelectNodes("//env"); - for (int i = 0; i < nodeList.Count; i++) - { - XmlNode node = nodeList[i]; - string key = node.Attributes["name"].Value; - string value = Environment.ExpandEnvironmentVariables(node.Attributes["value"].Value); - map[key] = value; - - Environment.SetEnvironmentVariable(key, value); - } - - return map; - } - } + public Dictionary EnvironmentVariables => new Dictionary(this.environmentVariables); /// /// List of downloads to be performed by the wrapper before starting @@ -700,5 +688,22 @@ namespace winsw } public string? SecurityDescriptor => SingleElement("securityDescriptor", true); + + private Dictionary LoadEnvironmentVariables() + { + XmlNodeList nodeList = dom.SelectNodes("//env"); + Dictionary environment = new Dictionary(nodeList.Count); + for (int i = 0; i < nodeList.Count; i++) + { + XmlNode node = nodeList[i]; + string key = node.Attributes["name"].Value; + string value = Environment.ExpandEnvironmentVariables(node.Attributes["value"].Value); + environment[key] = value; + + Environment.SetEnvironmentVariable(key, value); + } + + return environment; + } } }