ConfigurationManager.OpenMappedExeConfiguration.GetSection()失败(刚刚开始发生)

时间:2011-07-29 19:40:31

标签: c# .net configurationmanager assemblybinding

首先,请允许我提前为这个极长的问题道歉 - 对任何使其结束的人都感到荣幸!


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

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

该库有一个方法LoadConfigSection()的类,它使用ConfigurationManager.OpenMappedExeConfiguration()从.config文件(存储在用户的Application Data文件夹中)读取并返回CustomConfigSection - 或者它应该。发生了什么GetSection()会引发一个令人讨厌的ConfigurationErrorsException,说它无法找到包含我CustomConfigSection的程序集。

令人讨厌的是,这是两天前的工作。我认为Windows Update破坏了这一点。我已经回去尝试我的代码的早期版本,我知道它工作正常,但无济于事。环境发生了变化,而不是我的代码,但现在其他机器也发生了变化,所以不仅仅是我!

这是一个示例.config文件:

<?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>

我正在使用的代码来阅读它(正如我之前提到的那样,2天前工作完美):

    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。

启用Fusion程序集绑定日志记录后,我得到的错误是:

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

Fusion无法绑定日志本身:

*** 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的程序集 - 至于为什么这种情况最近才开始发生我不知道,但我确实安装了一堆Windows更新2-3天前我正在运行Windows XP Pro SP3 32位。

如果不完全理解所有这些术语的含义,Fusion日志会让我希望我能够更改加载上下文或策略以强制它从正确的位置加载程序集。这是真的,如果是的话,怎么样?

1 个答案:

答案 0 :(得分:3)

使用Assembly.LoadFrom()并处理AssemblyResolve事件是关键 - this answer中的代码示例为我解决了这个问题。我很想听听专家对这是如何运作的想法。