3

How can I provide settings that can be accessed through ConfigurationManager in asp.net 5?

I am using older packages that do not support the new Microsoft.Framework.ConfigurationModel and are looking to the ConfigurationManager to get connection strings, app settings, and even configuration sections. Adding an app.config to the project does not appear to have any effect.

For example, app.config:

<configuration>
  <connectionStrings>
    <add name="logging" connectionString="Server=.\SQLExpress;Database=logging;Trusted_Connection=True;"/>
  </connectionStrings>
</configuration>

ConfigurationManager.ConnectionStrings["logging"] returns null. Instead, the ConfigurationManager.ConnectionStrings only contains the machine.config defaults.

Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
  • did you try to use the web.config ? – agua from mars Apr 13 '15 at 15:21
  • Absolutely, as well as `assemblyname.config`. But this is actually a console app using the aspnet50 structure at the moment, and so the `web.config` wouldn't really apply. – Matt DeKrey Apr 13 '15 at 15:39
  • did you try to use `ConfigurationManager.OpenExeConfiguration or OpenMappedExeConfiguration` method ? Maybe it's not applicable in your scenarion – agua from mars Apr 13 '15 at 15:53
  • You can maybe set it with `AppDomain.CurrentDomain.SetupInformation.ConfigurationFile`but not sure – agua from mars Apr 13 '15 at 16:00
  • Unfortunately, the libraries don't support a configuration object, and it looks as though the `AppDomain.CurrentDomain.SetupInformation.ConfigurationFile` is set too late for asp.net-5 (I wasn't aware of that one!) – Matt DeKrey Apr 13 '15 at 17:21

2 Answers2

2

It seems that they decided to deprecating System.Configuration at all. So it's supposed (I'm guessing) that if you a lib which reads some configuration from web/exe.config (via ConfigurationManager) then you'll have to come up with out own syntax for configuring, read it from json/xml/whatever in Startup and initialize the lib via its api on your own. It's kinda inversion of control.

I asked the team for more clear docs in this issue.

Shrike
  • 9,218
  • 7
  • 68
  • 105
  • I hope you get some responses! Since this isn't really an answer, either, I've upvoted you for your effort, but marked my workaround for the time being. If they respond and you can update your answer with a complete solution, I'll be happy to change the accepted answer over to yours! – Matt DeKrey Apr 17 '15 at 01:01
2

For the time being, I have used a combination of the ResetConfigMechanism found in this answer and the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile setting that is mentioned above in the comments.

I do not find this a satisfactory answer (it's more of a workaround), and would love to accept someone else's answer if they have a better solution.

Just access the ConfigurationFixture.Current somewhere in your app before you need your configuration file to be correct and you should be good!

using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;

public sealed class ConfigurationFixture
{
    public static ConfigurationFixture Current = new ConfigurationFixture();

    private ConfigurationFixture()
    {
        // Run at start
        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App.config"));
        ResetConfigMechanism();
    }

    private static void ResetConfigMechanism()
    {
        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);
    }
}
Community
  • 1
  • 1
Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69