Loading app.config into the AppDomain
Asked Answered
F

2

16

I can't get the App.Config file to load into the App Domain.

I'm using

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path)

from Powershell Calling .NET Assembly that uses App.config but the App.Config file is still not loaded.

I've also tried resetting the cache as explained in Using CurrentDomain.SetData("APP_CONFIG_FILE") doesn't work in PowerShell ISE .

Here is my test script:

$configFile = "{ActualPhysicalPath}\App.Config"
gc $configFile

Add-Type -AssemblyName System.Configuration
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)

[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $null)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configFile)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name

I'm always getting the connection strings stored in machine.config, rather than those in the App.config.

How can I get my specific App.Config file loaded in the app domain?

Footcloth answered 25/4, 2013 at 13:57 Comment(3)
Have you tried using ConfigurationManager.OpenExeConfiguration Method ?Maidamaidan
I just tried it and it's still not workingFootcloth
I misunderstood the question. Sorry. Can't you rename the config file to YOUR_APPLICATION_NAME.exe.config and copy it to the application folder? This should work. The application will load that file.Maidamaidan
B
18

Try moving your SetData statement before the GetField statement.

With PowerShell 5.0 on Windows 10, the guidance provided by the link you reference seems to work: I'm able to retrieve both AppSettings and ConnectionStrings.

Add-Type -AssemblyName System.Configuration

# Set this to the full path of your App.config
$configPath = "C:\Full\Path\To\App.config"

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configPath)
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)

[System.Configuration.ConfigurationManager]::AppSettings
[System.Configuration.ConfigurationManager]::ConnectionStrings
Biostatics answered 22/10, 2015 at 20:30 Comment(1)
I do not have a working workspace to test this, but if someone can confirm that this works I will give you the answer. Thanks for spending time on that really old questionFootcloth
N
-1

Have you tried the solution presented here: Change default app.config at runtime

It appears to describe some sort of cache, and that more work has to be done to ensure the loaded data is read.

Th important part of the first answer is: "There exists a class ClientConfigPaths that caches the paths. So, even after changing the path with SetData, it is not re-read, because there already exist cached values. The solution is to remove these, too"

Nataline answered 12/7, 2013 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.