Problem loading assembly via Reflection and accessing configuration

坚强是说给别人听的谎言 提交于 2019-12-10 09:56:40

问题


I'm trying to load a .NET assembly with Reflection (using the Assembly.LoadFrom method), and instantiate some types within this assembly.

This all seems to work fine, but one type I'm trying to instantiate accesses the assembly's configuration within its type initializer: it does ConfigurationManager.GetSection(sectionName). The assembly is then throwing an exception because the configuration section cannot be found.

The config file for the assembly I'm loading is in the same directory, and has the standard name (i.e. AssemblyName.dll.config), and the config definitely has the section being requested. Why can't the configuration section be found? Is there something extra I need to do when loading the assembly?


回答1:


Because the configuration file being read is the configuration file of the hosting executable. So for example if you are running you code from Foo.exe your config file should be named Foo.exe.config. The AssemblyName.dll.config is never used. If you are running this in a web site then you should use web.config.

You could try using the OpenMappedExeConfiguration method:

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



回答2:


The following maps 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);

You can now retrieve values from the assemblies configuration file much like you would through the configuration manager, however you must be a little bit more explicit in your requests.

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

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

However when using the Configuration variable LocalConfigurationManager (from the code above) a call like the normal ConfigurationManager call such as:

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

You would get the following error when you attempt to run the code:

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

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

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

For the Connection Strings Secion, the following syntax would be used

string ConnectionString =  LocalConfigurationManager.ConnectionStrings.ConnectionStrings["connectionStringName"].ConnectionString;


来源:https://stackoverflow.com/questions/3351604/problem-loading-assembly-via-reflection-and-accessing-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!