How to simulate ConfigurationManager in LINQPad

I am trying to check the code in LINQPad. However, the base class calls Configuration Manager. How can I simulate this 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 is the constructor for BaseRepository (from a compiled DLL, therefore not editable in LINQPad):

 protected BaseRepository(string connectionStringName) { var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName]; Connection = new SqlConnection(connectionString.ConnectionString); Connection.Open(); } 
+5
source share
2 answers

The answer can be found on the LinqPad FAQ website.

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

I refer to a custom assembly that reads settings from the application configuration file (app.config). Where should I place the application configuration file so LINQPad requests pick it up?

Create the linqpad.config file in the same folder as LINQPad.exe and place your configuration data there. Do not confuse this with linqpad.exe.config:

• linqpad.exe.config is for the LINQPad GUI

• linqpad.config is for your needs.

+10
source

Something that may be useful to you, I created it a while ago.

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

 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; } } 

Usage example:

 typeof(SomeType).ForceNewConfigFile(); System.Configuration.ConfigurationManager.AppSettings.Dump(); 

SomeType is simply the type contained in the assembly that will be used as the source for the location of the configuration file. It is assumed: the configuration file exists next to the DLL file and is called {Assembly.Location}.config .

+2
source

Source: https://habr.com/ru/post/1257449/


All Articles