1

When invoking

[System.Configuration.ConfigurationManager]::GetSection("MySection")

from within a PowerShell prompt, it throws an exception because the assembly containing the type represented by "MySection" in the app config is unable to be loaded. However, I have previously loaded the assembly containing that type, and I am even able to instantiate the type directly using 'new-object'.

How is the ConfigurationManager resolving types such that the assemblies already loaded into the PowerShell app domain are not visible to it?

Charles
  • 50,943
  • 13
  • 104
  • 142
Dan
  • 63
  • 1
  • 5
  • I ran into this exact problem last week. Hope you get an answer because I never figured it out. – OldFart Jun 03 '10 at 15:13
  • I think I found the issue here: http://msdn.microsoft.com/en-us/library/ms228245.aspx It appears the assembly defined in the 'type' element must be located in the same directory as the config file. It says 'web.config', but I think it's safe to assume this can be extended to the app config. FYI to a someone who responded earlier...I didn't attempt to create a static ConfigurationManager instance. The type in question, the one I was trying to create, is the type defined in the app config file...the one referenced by 'MySection'. – Dan Jun 03 '10 at 20:25

2 Answers2

0

How exactly did you load the assembly? Binding contexts matter:

Link

-Oisin

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
x0n
  • 51,312
  • 7
  • 89
  • 111
  • It was loaded using Assembly.LoadFrom. As to exactly how the assembly is loaded indirectly via GetSection I have no idea. But I think the MS documentation makes it clear that the assembly location matters in the case of app config files. – Dan Jun 04 '10 at 01:46
  • Yeah, the GetSection will probe for assemblies in the private path of the hosting process, i.e. powershell.exe. It might be easier to new up an appdomain and then marshal the string data back out of it. This is not too hard to write, even in powershell script. – x0n Jun 04 '10 at 17:22
0

Try changing the app_config_file location to a path to your own app config file that specifies a private probing path to the dir containing your DLL. See this SO post for more details. This works for connection string data but I'm not sure if it will work with a private probing path outside the app's base dir.

Community
  • 1
  • 1
Keith Hill
  • 194,368
  • 42
  • 353
  • 369