C# Console config renaming and relocating

0

I am trying to figure out how to get this working. I have a c# console application. After build, the default config file I got is myApp.exe.config I want to achieve the following:

  1. I want to rename the config file to myApp.xml
  2. I want to put it in a different location managed by some scheduling system.

I made it work by doing the following:

    private static void resetConfig()
    {
        var directory = System.IO.Directory.GetCurrentDirectory();
        var filename = $"{directory}\\myApp.xml";
        if (File.Exists(filename))
        {
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", filename);

            typeof(ConfigurationManager)
                .GetField("s_initState", BindingFlags.NonPublic |
                                         BindingFlags.Static)
                .SetValue(null, 0);

            typeof(ConfigurationManager)
                .GetField("s_configSystem", BindingFlags.NonPublic |
                                            BindingFlags.Static)
                .SetValue(null, null);

            typeof(ConfigurationManager)
                .Assembly.GetTypes()
                .Where(x => x.FullName ==
                            "System.Configuration.ClientConfigPaths")
                .First()
                .GetField("s_current", BindingFlags.NonPublic |
                                       BindingFlags.Static)
                .SetValue(null, null);
        }
    }

however, recently I upgraded some packages. And I got the following error while running it.

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'SomePackage, Version=6.0.0.0, Culture=neutral, PublicKeyToken=f3619abdd9aa739d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

The difference in config I noticed after upgrading the package is some new runtime section got added:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="SomePackage" publicKeyToken="f3619abdd9aa739d" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.2.1.2" newVersion="6.2.1.2" />
  </dependentAssembly>
</runtime>

Does anyone know how to fix the above error?

c#
asp.net
configuration
asked on Stack Overflow Mar 15, 2019 by David

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0