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.

5
  • did you try to use the web.config ? Apr 13, 2015 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. Apr 13, 2015 at 15:39
  • did you try to use ConfigurationManager.OpenExeConfiguration or OpenMappedExeConfiguration method ? Maybe it's not applicable in your scenarion Apr 13, 2015 at 15:53
  • You can maybe set it with AppDomain.CurrentDomain.SetupInformation.ConfigurationFilebut not sure Apr 13, 2015 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!) Apr 13, 2015 at 17:21

2 Answers 2

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.

1
  • 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! Apr 17, 2015 at 1: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);
    }
}
1
  • This no longer works for beta4; the AppDomain.CurrentDomain.BaseDirectory is not the right path. May 6, 2015 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.