2011-07-29 44 views
2

首先,让我对超长的问题提前致歉 - 任何人都会让它结束!ConfigurationManager.OpenMappedExeConfiguration.GetSection()失败(最近刚刚开始发生)


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

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

库有与方法LoadConfigSection(),使用ConfigurationManager.OpenMappedExeConfiguration()从.config文件(存储在用户的应用程序数据文件夹)读取并返回一个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> 

,我使用读它(这正如我前面提到完美3天前的工作)的代码:

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 

和融合失败结合自身登录:

*** 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天前安装了一堆Windows Update。我正在运行Windows XP专业版SP3 32位。

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

+0

您的Windows帐户权限最近有变化吗?我知道这可能听起来像一个愚蠢的问题,因为该DLL在您的文档和设置区域,但我最近发现(Win7),我不是我的文档和设置的所有者 – tomfumb

+0

据我所知,不,我是仍然是机器上的管理员。 – blah238

+0

嗯,我在Win7 x64机器上得到了同样的东西,我知道我拥有完全的管理权限等等。根据[本文](http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration。 aspx?msg = 3307466#xx3307466xx)Jon Rista从2009年开始,这是它的工作原理 - 由于.NET中的安全限制,程序集需要位于主机应用程序的专用bin搜索路径中。为什么它以前工作,不再是任何人的猜测,但如果属实,我猜测Windows更新会加强安全性。请让我知道,如果你发现你的加载项相同。 – blah238

回答

3

使用Assembly.LoadFrom()和处理AssemblyResolve事件是关键 - this answer中的代码示例为我解决了这个问题。我很乐意听到专家对这个工作原理的看法。