自定义配置部分:无法加载文件或程序 [英] Custom config section: Could not load file or assembly

查看:278
本文介绍了自定义配置部分:无法加载文件或程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常困难的时间来访问我的配置文件自定义配置节。

I'm having a very hard time trying to access a custom configuration section in my config file.

配置文件正在从一个.dll是阅读加载为插件。我使用配置截面设计 VS插件创建的配置和必要的代码。

The config file is being read from a .dll that is loaded as a plug-in. I created the Configuration and necessary code using the Configuration Section Designer VS addin.

该命名空间是'ImportConfiguration。该的ConfigurationSection类是ImportWorkflows。该大会是ImportEPDMAddin

The namespace is 'ImportConfiguration'. The ConfigurationSection class is 'ImportWorkflows'. The assembly is ImportEPDMAddin.

XML的:

  <configSections>
    <section name="importWorkflows" type="ImportConfiguration.ImportWorkflows, ImportEPDMAddin"/>
  </configSections>



每当我尝试在配置看,我得到的错误:

Whenever I try to read in the config, I get the error:

出错创建importWorkflows配置节处理程序:无法加载文件或程序集ImportEPDMAddin.dll或它的一个依赖。该系统找不到指定的文件。

An error occurred creating the configuration section handler for importWorkflows: Could not load file or assembly 'ImportEPDMAddin.dll' or one of its dependencies. The system cannot find the file specified.

该DLL不会驻留在同一个目录下的可执行文件的加载插件软件放在DLL和它的依赖在它的自己的目录。 (我无法控制。)

The dll will not reside in the same directory as the executable as the software that loads the plugin places the dll and it's dependencies in it's own directory. (I can't control that.)

我编辑的单实例下面的代码:

I edited the code for the singleton instance to the following:

string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
path = path.Replace("file:///", "");
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(path);
return configuration.GetSection(ImportWorkflowsSectionName) as ImportConfiguration.ImportWorkflows;



我也尝试使用简单NameValueFileSectionHandler为好,但我得到一个异常说,它可以 T加载文件或程序集系统。

I have also tried using a simple NameValueFileSectionHandler as well, but I get an exception saying that it can't load file or assembly 'System'.

我看过无数的博客帖子和文章,这听起来像它可以读取在DLL中的配置文件,但我只是无法得到它的工作。有任何想法吗?谢谢你。

I have read numerous blog posts and articles and it sounds like it is possible to read a config file in for a dll, but I just can't get it to work. Any ideas? Thanks.

推荐答案

不幸的是,你需要或者有 ImportEPDMAddin 装配居住在同一文件夹中可执行文件,驻留在与.NET Framework中的Net框架文件夹,您正在使用(即C:\Windows\Microsoft.NET\Framework\v2.0.50727),或在全局程序集缓存注册。

Unfortunately, you will need to either have the ImportEPDMAddin assembly residing in the same folder as your executable, residing in the .Net framework folder related to the .Net framework you are using (i.e., C:\Windows\Microsoft.NET\Framework\v2.0.50727), or registered in the Global Assembly Cache.

唯一的另一种选择是,如果你知道的路径,包含配置处理程序的定义类的程序集,可以加载它没有像这样的引用:

The only other option is, if you know the path to the assembly that contains the configuration handler's defining class, you can load it without a reference with something like this:

//Class global
private Assembly configurationDefiningAssembly;

protected TConfig GetCustomConfig<TConfig>(string configDefiningAssemblyPath, 
    string configFilePath, string sectionName) where TConfig : ConfigurationSection
{
    AppDomain.CurrentDomain.AssemblyResolve += new 
        ResolveEventHandler(ConfigResolveEventHandler);
    configurationDefiningAssembly = Assembly.LoadFrom(configDefiningAssemblyPath);
    var exeFileMap = new ExeConfigurationFileMap();
    exeFileMap.ExeConfigFilename = configFilePath;
    var customConfig = ConfigurationManager.OpenMappedExeConfiguration(exeFileMap, 
        ConfigurationUserLevel.None);
    var returnConfig = customConfig.GetSection(sectionName) as TConfig;
    AppDomain.CurrentDomain.AssemblyResolve -= ConfigResolveEventHandler;
    return returnConfig;
}

protected Assembly ConfigResolveEventHandler(object sender, ResolveEventArgs args)
{
    return configurationDefiningAssembly;
}

请确保您处理AssemblyResolve事件,因为这将抛出一个异常,没有它

Make sure you handle the AssemblyResolve event, as this will throw an exception without it.

这篇关于自定义配置部分:无法加载文件或程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆