0

I initialized the interactive element with the project from the context menu of my project. I am testing a function in C# interactive that needs to read my app.config file to get a connectionstring. I got the next error:

No connection string named 'ccnName' could be found in the application config file.

When I use the next code, i get a null value. I suppose it is because it is not reading the app.config of my project.

ConfigurationManager.ConnectionStrings["cnnName"]

It is the only connectionstring that the default app.config has:

[data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true]

In this post from 2012 with the same topic, one engineer involved in this project said that this option was not available. I hope it is available now in 2018

So, nowadays how can i load the app.config that i want?

Walter
  • 99
  • 2
  • 13
  • without seeing your connection string in the .config file, which you should share anyway have you tried doing this instead `ConfigurationManager.ConnectionStrings["cnnName"].ConnectionString;` – MethodMan Dec 17 '18 at 22:07
  • I just tried and i got the null reference point exception, there is only one connectionstring in the current or default appconfig in the index "0" – Walter Dec 17 '18 at 22:51
  • there is a website [connection strings](http://www.connectionstrings.com) once there locate the `SQLExpress` and they will have examples on how to configure the connection string inside the app.config or web.config file – MethodMan Dec 18 '18 at 13:09
  • How much authority do you have to change the function you want to test? – madreflection Dec 18 '18 at 17:20
  • 1
    @MethodMan thanks guy, but it is not about that, it is about reading appconfig from C# Interactive or Roslyn, C# interactive loads everything from your project but not the appconfig. – Walter Dec 18 '18 at 21:59
  • @madreflection the top level, i have just solved the problem, I avoided reading app.config, instead i overrode the constructor to take the connectionstring directly without reading the app.config – Walter Dec 18 '18 at 22:06

1 Answers1

0

"Constructor" was the magic word. This may not help in your case since you've found a solution, but it might be helpful for others in the same situation.

If you inject a System.Configuration.Configuration object into the class, you don't have to rely on ConfigurationManager's static properties.

public class LibraryClass
{
    private Configuration _configuration;

    public LibraryClass(Configuration configuration)
    {
        _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
    }

    public void FunctionUnderTest()
    {
        string connectionString = _configuration.ConnectionStrings.ConnectionStrings["cnnName"].ConnectionString;
        // Connect to the database as you normally would.
    }
}

In a console/GUI application and unit tests, load it like this to use {anything}.config:

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Register 'configuration' as a singleton using the container of your choice.

In a web application, load it like this to use web.config:

Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
// Register 'configuration' as a singleton using the container of your choice.

To use it in C# Interactive, load it using the first method and provide the dependency to the class directly:

#r "System.Configuration"

Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(
    new ExeconfigurationFileMap() { ExeConfigFilename = @"path\to\Arbitrary.config" },
    ConfigurationUserLevel.None);
var lib = new LibraryClass(configuration);
lib.FunctionUnderTest();

Note that the section properties are an extra layer deep compared to what you would normally expect. I think it has something to do with how ConfigurationManager's static properties work with the Configuration instance.

madreflection
  • 4,744
  • 3
  • 19
  • 29