I lost some time today investigating how to load an app.config for a separate domain. My first attempt looked like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<startup> | |
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> | |
</startup> | |
<appSettings> | |
<add key="Hello" value="Hello World!"/> | |
</appSettings> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(ConfigurationManager.AppSettings["Hello"]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var appDomainSetup = new AppDomainSetup(); | |
var configPath= Path.Combine(Environment.CurrentDirectory, "OtherConsoleApp.exe.config"); | |
appDomainSetup.ConfigurationFile = configPath; | |
var domain=AppDomain.CreateDomain("OtherConsoleApp",null,appDomainSetup); | |
domain.ExecuteAssembly("OtherConsoleApp.exe"); | |
AppDomain.Unload(domain); |
However the call to ConfigurationManager.AppSettings always returned null. I first thought that I did something wrong when configuring the AppDomainSetup.
I first tried to replace the AppDomainSetup configuration to explicitly load the config settings:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var appDomainSetup = new AppDomainSetup(); | |
var configPath= Path.Combine(Environment.CurrentDirectory, "OtherConsoleApp.exe.config"); | |
appDomainSetup.SetConfigurationBytes(File.ReadAllBytes(configPath)); |
But this did not solve the issue. In the end I discovered that it was the ConfigurationManager.AppSettings that was causing the issue. When I replaced it with the code below, it started to work!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); | |
Console.WriteLine(config.AppSettings.Settings["Hello"].Value); | |
} | |
} |