Assembly loading failure through Reflection and configuration access

I am trying to load a .NET assembly using Reflection (using the Assembly.LoadFrom method) and instantiate some types in this assembly.

This all works fine, but the one type I'm trying to execute allows you to access the assembly configuration in its type initializer: it does ConfigurationManager.GetSection(sectionName) . The assembly then throws an exception because the configuration section was not found.

The configuration file for the assembly that I am loading is in the same directory and has a standard name (i.e. AssemblyName.dll.config ), and the request is defined in the config. Why cannot the configuration section be found? Is there anything extra I need to do when loading the assembly?

+3
source share
2 answers

Since the read configuration file is the configuration file of the hosting executable. For example, if you are using code from Foo.exe , your configuration file should be named Foo.exe.config . AssemblyName.dll.config never used. If you use this on a website, you should use web.config .

You can try using the OpenMappedExeConfiguration method:

 var configFileMap = new ExeConfigurationFileMap(); configFileMap.ExeConfigFilename = "AssemblyName.dll.config"; var section = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None).GetSection(sectionName); 
+6
source

The following file displays the file and then opens it as a System.Configuration variable:

 string FilePath = System.Reflection.Assembly.GetAssembly(typeof(EncryptDecryptViewModel)).Location FilePath += @".config"; var ConfigFileMap = new ExeConfigurationFileMap(); ConfigFileMap.ExeConfigFilename = FilePath; Configuration LocalConfigurationManager = ConfigurationManager.OpenMappedExeConfiguration(ConfigFileMap, ConfigurationUserLevel.None); 

Now you can extract values ​​from the assembly configuration file in the same way as through the configuration manager, however you should be a little more explicit in your requests.

When using System.Configuration.ConfigurationManager, the following value will be valid and return a value:

 string s = System.Configuration.ConfigurationManager.AppSettings["SomeSetting"]; 

However, when using the LocalConfigurationManager configuration parameter (from the above code), a call, such as a regular ConfigurationManager call, for example:

 string s = LocalConfigurationManager.AppSettings["ConfigurationSections"]; 

When you try to run the code, you get the following error:

'System.Configuration.ConfigurationElement.this [System.Configuration.ConfigurationProperty]' is unavailable due to its level of protection

This is because in the ConfigurationManager, the AppSettings property is NameValueCollection. In the configuration variable, AppSettings is actually a System.Configuration.AppSettingsSection that contains the Parameters property, which is KeyValueConfigurationCollection, so the call will look like this to access the property:

 string s = LocalConfigurationManager.AppSettings.Settings["SomeSetting"].Value; 

The section "String join strings" uses the following syntax

 string ConnectionString = LocalConfigurationManager.ConnectionStrings.ConnectionStrings["connectionStringName"].ConnectionString; 
+1
source

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


All Articles