Loading app.config into the AppDomain

后端 未结 2 1048
遇见更好的自我
遇见更好的自我 2021-02-13 10:23

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)


        
相关标签:
2条回答
  • 2021-02-13 10:39

    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"

    0 讨论(0)
  • 2021-02-13 10:46

    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
    
    0 讨论(0)
提交回复
热议问题