Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate ConfigurationManager in LINQPad

Tags:

c#

linqpad

I'm trying to test some code in LINQPad. However, the base class calls Configuration Manager. How can I simulate that when testing in LINQPad.

void Main()
{
    var tRepo = new TestRepository();
    var result = tRepo.GetAsync(1);
    result.Dump();
}

public partial class TestRepository : BaseRepository<Customer>, ICustomerRepository 
{
    // Here base throws the errror
    public TestRepository() : base("DbConnString")
    {            
    }
}

Here's the constructor for BaseRepository (from a compiled DLL, so not editable in LINQPad):

protected BaseRepository(string connectionStringName)
{
    var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName];

    Connection = new SqlConnection(connectionString.ConnectionString);
    Connection.Open();
}
like image 243
M Kenyon II Avatar asked Sep 29 '16 13:09

M Kenyon II


People also ask

What is ConfigurationManager appSettings?

it is a .net builtin mechanism to define some settings before the application starts, without recompiling. see msdn configurationmanager.

What is the namespace for ConfigurationManager in C#?

Namespace: System.Configuration To use the ConfigurationManager class, your project must reference the System. Configuration assembly. By default, some project templates, like Console Application, do not reference this assembly so you must manually reference it.

What is configuration manager asp net?

The ConfigurationManager class enables a Web or Windows application to access machine, application, and user configuration files. The name and location of the configuration files depend on whether you are working with a Web application or a Windows console application.


2 Answers

The answer can be found on the LinqPad website FAQ

http://www.linqpad.net/faq.aspx

I'm referencing a custom assembly that reads settings from an application configuration file (app.config). Where should I put my application config file so that LINQPad queries will pick it up?

Create a file called linqpad.config in the same folder as LINQPad.exe and put your configuration data there. Don't confuse this with linqpad.exe.config:

•linqpad.exe.config is for the LINQPad GUI

•linqpad.config is for your queries.

like image 103
Simon Price Avatar answered Sep 18 '22 16:09

Simon Price


Something that might be useful for you, I created it some time ago.

This is an extension method, which you can use to force the reload of configuration from specific file. It uses reflection to change the private fields in the manager, clears the configuration and then conditionally reloads it. It is much easier than manually editing the config file of LINQPad.

public static void ForceNewConfigFile(this Type type, bool initialize = true)
{
    var path = type.Assembly.Location + ".config";
    if (!File.Exists(path))
        throw new Exception("Cannot find file " + path + ".");

    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);

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

    var typeOfClientConfigPaths = typeOfConfigManager.Assembly.GetTypes().Where(x => x.FullName == "System.Configuration.ClientConfigPaths").Single();
    typeOfClientConfigPaths.GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null);

    if (initialize)
    {
        var dummy = ConfigurationManager.AppSettings;
    }
}

Example usage:

typeof(SomeType).ForceNewConfigFile();

System.Configuration.ConfigurationManager.AppSettings.Dump();

SomeType is just a type contained in the assembly, which will be used as a source for location of the config file. Assumption is: configuration file exists beside the DLL file and is named {Assembly.Location}.config.

like image 30
kiziu Avatar answered Sep 22 '22 16:09

kiziu