Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading app.config into the AppDomain

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?

like image 228
Alexandre Rondeau Avatar asked Apr 25 '13 13:04

Alexandre Rondeau


People also ask

When assembly will load on AppDomain?

If an assembly is loaded into the same AppDomain, then the class can be instantiated in the usual way. But if an assembly is loaded into a different AppDomain then it can be instantiated using reflection. Another way is an interface.

What is AppDomain in C#?

The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown.

What is AppDomain and CurrentDomain?

The CurrentDomain property is used to obtain an AppDomain object that represents the current application domain. The FriendlyName property provides the name of the current application domain, which is then displayed at the command line.

How do I create an app for my domain?

You can also create application domains from which you execute code. You create a new application domain using one of the overloaded CreateDomain methods in the System. AppDomain class. You can give the application domain a name and reference it by that name.


1 Answers

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
like image 67
Robert Mooney Avatar answered Sep 17 '22 19:09

Robert Mooney