【问题标题】:FileLoadException At InitializeComponent or x:Class=FileLoadException 在 InitializeComponent 或 x:Class=
【发布时间】:2015-04-15 21:16:56
【问题描述】:

我在InitializeComponent-方法处遇到文件加载器异常(第一次机会),或者调试器在多个 WPF 用户控件的 xaml-root 的 x:Class 属性处中断。尽管异常会大大减慢导航速度,但一切正常。

这是异常消息:

无法加载文件或程序集“Company.Solution.UserInterface, Version=0.1.5568.25577, Culture=neutral, PublicKeyToken=45069ab0c15881ce”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)

这是融合日志:

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  D:\Development\Product\Main\src\Company.Product \bin\Debug\Product.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = Company.Product .UserInterface, Version=0.1.5568.25577, Culture=neutral, PublicKeyToken=45069ab0c15881ce
(Fully-specified)
LOG: Appbase = file:///D:/Development/Product/Main/src/Company.Product/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.     
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\Development\Product \Main\src\Company.Product \bin\Debug\Product .vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Company.Product .UserInterface, Version=0.1.5568.25577, Culture=neutral, PublicKeyToken=45069ab0c15881ce
LOG: Attempting download of new URL file:///D:/Development/Product/Main/src/Company.Product/bin/Debug/Company.Product.UserInterface.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Revision Number
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

我的项目结构有一个引用模块项目的根项目(其中发生异常)。模块项目本身引用了作为上述探测“Company.Product.UserInterface.dll”目标的项目,其中包含一些资源/控件/样式/原语/转换器等。

我怎样才能摆脱FileLoadExceptions

另一个更完整的 Fusion-log:

=== Pre-bind state information ===
LOG: DisplayName = Company.Product.UserInterface, Version=0.1.5577.18122,      Culture=neutral, PublicKeyToken=45069ab0c15881ce
(Fully-specified)
LOG: Appbase = file:///D:/Development/Product/Main/src/Company.Product/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = Product.vshost.exe
Calling assembly : PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\Development\Product\Main\src\Company.Product\bin\Debug\Product.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Company.Product.UserInterface, Version=0.1.5577.18122, Culture=neutral, PublicKeyToken=45069ab0c15881ce
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///D:/Development/Product/Main/src/Company.Product/bin/Debug/Company.Product.UserInterface.DLL.
LOG: Assembly download was successful. Attempting setup of file: D:\Development\Product\Main\src\Company.Product\bin\Debug\Company.Product.UserInterface.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Company.Product.UserInterface, Version=0.1.5577.18123, Culture=neutral, PublicKeyToken=45069ab0c15881ce
WRN: Comparing the assembly name resulted in the mismatch: Revision Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

发生异常时,SolutionExplorer 中引用的程序集版本是 0.1.5577.18123(在所有引用 ..UserInterface.dll 的解决方案中。我不知道是谁查找了 0.1.5577.18122,这个版本从来没有存在)

如果我运行一个新的重建,我得到同样的错误,Fusion 会寻找(我从来没有这个版本号):

LOG: Post-policy reference: Company.Product.UserInterface, Version=0.1.5577.18465, Culture=neutral, PublicKeyToken=45069ab0c15881ce

找到的版本是:

LOG: Assembly Name is: Company.Product.UserInterface, Version=0.1.5577.18466, Culture=neutral, PublicKeyToken=45069ab0c15881ce

Visual Studio 版本为 2013 Ultimate,项目基于 .net4.5 构建,程序集版本在构建过程中自动生成。 我上传了构建日志to tinyupload,因为它太大了。 完整的 Fusion-log 可以在here at pastebin找到。

【问题讨论】:

  • 你检查你的 Fusion 日志了吗?
  • UserInterface.dll是否只被一个项目引用?
  • No UserInterface 被 3 个项目使用,然后它们都由依赖根放在一起。如果有帮助,我可以发布依赖关系图
  • 这三个项目中的一个是否引用了旧版本?
  • 您是如何生成版本号的?我假设它是自动生成的,因为版本号经常从您的日志中更改。如果是这种情况,看起来您的项目之一可能是在您的 UserInterface 项目之前构建的。我相当确定这只有在其中一个项目引用 dll 而不是创建 dll 的项目时才有可能。

标签: c# wpf probing assemblybinding


【解决方案1】:
   Version=0.1.5577.18122

这个自动生成的版本号说明了一个故事,版本号的最后两部分不是任意的。它们基于装配体的构建日期和时间。内部版本号是根据自 2000 年 1 月 1 日以来的天数生成的。修订号是自午夜以来的秒数 *2,没有夏令时校正。

所以我们知道的事实是 18122 装配体是在 3 月 30 日下午 2:12:34 建造的。然后它在 2 秒后的下午 2 点 12 分 36 秒再次构建。在它被用作参考程序集来构建另一个项目之后,这让 CLR 吐了口水。

这应该发生,一个项目只能在一个构建会话中构建一次。找出发生这种情况的原因需要深入研究 MSBuild 跟踪。您可以使用工具 + 选项、项目和解决方案、构建和运行生成所需的内容。将“MSBuild 项目构建输出详细程度”设置更改为详细。 MSBuild 现在变得非常健谈,并告诉您它决定构建项目的原因。如果你在试图解码它的输出时迷路了(有很多),那么将它复制/粘贴到粘贴箱中并在你的问题中链接到它。

对于这样的事故,没有太多很好的解释。较旧的 VS 版本很容易意外地在项目之间创建循环依赖关系。您可以使用 Build + Clean 将其清除。重建解决方案现在失败,并告诉您哪个参考程序集是麻烦制造者。但是,您使用的是 .NET 4,因此至少是 VS2010。所以不是一个很好的领先优势,微软增加了更多的检查来防止这种情况在没有警告的情况下发生。不确定它是否在所有情况下都可靠,例如,如果您不单独依赖 MSBuild,它可能会被愚弄。在具有“持续集成”功能的构建服务器上并不少见。

我们需要构建跟踪来为您提供可靠的诊断。

【讨论】:

  • 我查看了 MSBuild 日志,但找不到任何表明问题的信息。该项目仅构建一次。所有其他项目对 UserInterface.dll 的引用显示相同的版本。
  • 版本号非常强烈地暗示你忽略了一些东西。您需要想出一种方法,使该组件在您的机器上在 2 秒内构建两次。我想在 2 秒内打两次 Build + Build 在技术上是可行的。如果您不向我们展示某事,就没有任何线索。就像那个构建跟踪一样。
  • 我将构建跟踪添加到我的问题中。
  • 我看到了相当多的事故,但没有大声喊叫。有一个细节很有趣,您的项目存储在 D: 驱动器上。这是什么驱动? 2 秒是文件系统的一个神奇数字,它是 FAT32 驱动器的时间戳分辨率。
  • 它是一个 NTFS 格式的 SSD(不是物理的,只是一个分区)
【解决方案2】:

建议 1

是否存在导致加载旧版本 dll 的循环引用? (这被证明不是重点,但由于历史原因我已经离开了)。 Relating to this answer

建议 2

您可以尝试创建发布商政策吗? 这是一个需要添加到您的 app.config 文件中的示例。

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Company.Solution.UserInterface"
                        publicKeyToken="45069ab0c15881ce"
                        culture="en-us" />
      <!-- Redirecting to version 0.1.5568.25577 of the assembly. -->
      <bindingRedirect oldVersion="0.0.0.0-0.1.5568.25577"
                      newVersion="0.1.5568.25577"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>

Explained in further detail here

建议 3

用于签署程序集的密钥有什么变化吗?

建议 4

作为对我最初建议的轻微改编。您已声明有 3 个项目引用了 Company.Solution.UserInterface。您能否确认所有 3 个项目都引用了该程序集的同一版本?

【讨论】:

  • 我没有循环引用,循环引用会失败,而我的应用程序确实按预期运行,只是在创建用户控件/页面时速度很慢
  • @Console 它可能不一定是循环引用,但从错误消息看来,您正在加载的版本与您的项目预期不符。如果您在 VS 中检查您的参考,它是否将特定版本设置为 True?
  • @Console 失败了,您是否尝试过设置发布者策略? msdn.microsoft.com/en-us/library/dz32563a(v=VS.90).aspx
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • @bit 我已经改进了我的答案
猜你喜欢
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多