首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConfigurationManager.OpenMappedExeConfiguration.GetSection()失败(最近才开始发生)

ConfigurationManager.OpenMappedExeConfiguration.GetSection()失败(最近才开始发生)
EN

Stack Overflow用户
提问于 2011-07-29 19:40:30
回答 1查看 8.4K关注 0票数 2

首先,让我提前为这个非常长的问题道歉

我有一个C#/.NET 3.5类库,它没有安装在GAC中,因为类库是另一个程序(ArcMap.exe)的外接程序。实际上,该程序在启动时将.dll从一个归档文件中提取到用户的location \Application数据文件夹下的一个文件夹中,但我可以使用this.GetType().Assembly.Location在运行时获取它的位置。

在库中,我有一个名为CustomConfigSection的类,它在.config文件中被引用。

库有一个具有LoadConfigSection()方法的类,它使用ConfigurationManager.OpenMappedExeConfiguration()从.config文件(存储在用户的Application文件夹中)中读取,并返回CustomConfigSection --或者应该这样做。结果是GetSection()抛出了一个讨厌的ConfigurationErrorsException,说它找不到包含我的CustomConfigSection的程序集。

烦人的是这两天前起作用了。我想是Windows Update破坏了这一点。我已经回去尝试了我以前的代码版本,我知道这些版本工作得很好,但没有效果。有些东西在环境中改变了,不是在我的代码中,而是现在在其他机器上也发生了什么,所以不仅仅是我!

下面是一个示例.config文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="customConfigSection" type="TestExeConfigurationFileMapAddin.CustomConfigSection, TestExeConfigurationFileMapAddin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </configSections>
    <customConfigSection>
        <customConfigCollection>
            <customConfig name="asdf" />
            <customConfig name="1234" />
            <customConfig name="jlkjjkljj" />
        </customConfigCollection>
    </customConfigSection>
</configuration>

以及我用来阅读它的代码(正如我在两天前提到的那样):

代码语言:javascript
复制
    private CustomConfigSection LoadConfigSection()
    {
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
        configFileMap.ExeConfigFilename = m_userConfigFilePath;
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
        CustomConfigSection configSection = config.GetSection("customConfigSection") as CustomConfigSection;
        return configSection;
    }

m_userConfigFilePath是指向.config文件的有效路径,Configuration对象的HasFile属性为true。

以下是我在启用聚变程序集绑定日志之后遇到的错误:

代码语言:javascript
复制
System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for customConfigSection: Could not load file or assembly 'TestExeConfigurationFileMapAddin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. (C:\Documents and Settings\...\Application Data\TestExeConfigurationFileMapAddin\test.config line 4) ---> System.IO.FileNotFoundException: Could not load file or assembly 'TestExeConfigurationFileMapAddin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'TestExeConfigurationFileMapAddin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
   at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
   at System.Configuration.MgmtConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord)
   at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)

Assembly manager loaded from:  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
Running under executable  C:\Program Files\ArcGIS\Desktop10.0\Bin\ArcMap.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = ...
LOG: DisplayName = TestExeConfigurationFileMapAddin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files\ArcGIS\Desktop10.0\Bin\ArcMap.exe.Config
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/TestExeConfigurationFileMapAddin.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/TestExeConfigurationFileMapAddin/TestExeConfigurationFileMapAddin.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/TestExeConfigurationFileMapAddin.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/TestExeConfigurationFileMapAddin/TestExeConfigurationFileMapAddin.EXE.

   --- End of inner exception stack trace ---
   at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission)
   at System.Configuration.Configuration.GetSection(String sectionName)
   at TestExeConfigurationFileMapAddin.Form1.LoadConfigSection() in C:\CSProjects\TestExeConfigurationFileMapAddin\TestExeConfigurationFileMapAddin\Form1.cs:line 46
   at TestExeConfigurationFileMapAddin.Form1.LoadConfig() in C:\CSProjects\TestExeConfigurationFileMapAddin\TestExeConfigurationFileMapAddin\Form1.cs:line 28

而融合失败的绑定日志本身:

代码语言:javascript
复制
*** Assembly Binder Log Entry  (7/29/2011 @ 12:46:02 PM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
Running under executable  C:\Program Files\ArcGIS\Desktop10.0\Bin\ArcMap.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = ...
LOG: DisplayName = TestExeConfigurationFileMapAddin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files\ArcGIS\Desktop10.0\Bin\ArcMap.exe.Config
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/TestExeConfigurationFileMapAddin.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/TestExeConfigurationFileMapAddin/TestExeConfigurationFileMapAddin.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/TestExeConfigurationFileMapAddin.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/ArcGIS/Desktop10.0/Bin/TestExeConfigurationFileMapAddin/TestExeConfigurationFileMapAddin.EXE.
LOG: All probing URLs attempted and failed.

显然,ConfigurationManager正在从错误的位置寻找包含我的CustomConfigSection的程序集--为什么这种情况最近才开始发生,我不知道,但我在2-3天前确实安装了一批CustomConfigSection更新。我正在运行Windows 32位SP3 .

在不完全理解所有这些术语的含义的情况下,融合日志给了我希望,我可能会改变加载上下文或策略,迫使它从正确的位置加载程序集。这是真的吗?如果是的话,怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-02 08:30:36

使用Assembly.LoadFrom()和处理AssemblyResolve事件是关键-- 这个答案中的代码示例为我解决了这个问题。我很想听听专家们对这件事的想法。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6878194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档