How to simulate ConfigurationManager in LINQPad
Asked Answered
R

3

8

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();
}
Reynaldoreynard answered 29/9, 2016 at 13:50 Comment(0)
F
15

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.

Felon answered 29/9, 2016 at 13:55 Comment(4)
I swear that wasn't there when I looked, but there it is, thank you.Reynaldoreynard
Note, linqpad.config affect all queries. The newest version of LinqPad has an option on the query properties (F4) to specify the path to a custom app.config or even to just paste in an app.config.Manama
@Manama you are indeed correct. I had never seen this optionFelon
Have you looked into this? where-is-app-config-in-linqpad-6 - it describes what has changed in LINQPad 6 for the configuration settings file(s). The tab that used to be in the F4 dialog in LINQPad 5 is gone in LinqPad 6.Drift
B
3

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.

Berserk answered 29/9, 2016 at 14:41 Comment(0)
F
0

I went looking for this as well, but didn't want to create a whole new file, point, have other share that file etc. So I added in the ConfigurationBuilder, created a dictionary and added it to a InMemoryCollection.

    #nullable enable
private IConfiguration _config;
void Main()
{   
    var builder = new ConfigurationBuilder();
    var dictonary = new Dictionary<string, string>
    {
        {"Authentication:SecretKey","SuperSecret"}
    };
    builder.AddInMemoryCollection(dictonary);
    
    _config = builder.Build();
    
    _config.GetValue<string>("Authentication:SecretKey").Dump();
}

Example

Fieldwork answered 10/8, 2022 at 19:40 Comment(1)
Yes, but that's not the question. I'm afraid stack overflow is not a collection of random programmer tricks.Hostler

© 2022 - 2024 — McMap. All rights reserved.