【问题标题】:InitializeComponent throws exception .resources not foundInitializeComponent 抛出异常.resources not found
【发布时间】:2016-11-11 19:51:06
【问题描述】:

从 C# 3.5 迁移到 4 后,我们开始遇到 InitializeComponent 上找不到 .resources 文件的异常:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in PresentationCore.dll

Additional information: Could not load file or assembly 'MYASSEMBLY.resources, Version=1.0.0.0, Culture=en-US, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

这是一个 WPF 程序集,按需加载,并且没有本地化 - .resources 不存在也不应该存在。该问题在迁移之前并未发生,仅在应用程序中执行一组特定操作时才会发生。在另一个操作中,程序集已正确加载,Fusion Logs 显示尝试加载 .resources 失败,但未引发异常。

另外,一篇关于调试程序集加载的旧博客文章特别提到:

注意:除非您明确调试资源加载失败,否则您可能希望忽略查找具有“.resources”扩展名且文化设置为“中性”以外的程序集的失败。当 ResourceManager 探测附属程序集时,这些是预期的失败。 https://blogs.msdn.microsoft.com/suzcook/2003/05/29/debugging-assembly-loading-failures/

我已尝试在 AssemblyInfo.cs 中应用 NeutralResourcesLanguage 属性,但这只会使异常引用另一个第三方程序集。

这个问题的原因可能是什么?

【问题讨论】:

    标签: c# .net wpf .net-assembly


    【解决方案1】:

    该问题是由另一个动态加载的程序集中的 AppDomain.AssemblyResolve 事件处理程序编写不当以及 .Net 行为的更改引起的。这些程序集的动态负载性质解释了有时看不到错误的原因。

    在 InitializeComponent 上添加 try-catch 并检查异常表明内部异常在堆栈跟踪中具有尝试加载 .resources dll 的调用 - 编写错误的事件处理程序。

    这在 .Net 3.5 之前已经存在,但在 .Net 4 中发生了变化:

    从 .NET Framework 4 开始,为所有程序集(包括资源程序集)引发 ResolveEventHandler 事件。在早期版本中,不会为资源程序集引发事件。如果操作系统是本地化的,则处理程序可能会被调用多次:对于回退链中的每种区域性调用一次。

    https://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多