【问题标题】:Assembly resources FileNotFoundException in project upgraded to VS 2010 framework 4.0项目中的装配资源 FileNotFoundException 升级到 VS 2010 框架 4.0
【发布时间】:2010-09-17 09:12:08
【问题描述】:

我已将 C# WinForms 应用程序从 Visual C# 2008(框架 3.5)升级到 2010(框架 4.0),但出现运行时错误。

应用程序在运行时使用 Assembly.Load(filename) 动态加载程序集。之所以这样设计,是因为根据用户配置,它应该加载位于不同文件夹中的程序集的不同实现。

一切正常,除非加载的 dll 有嵌入式资源(位图或 xsd 数据集),然后我得到 FileNotFoundException:

{"找不到文件'FF.Fi_Stat.SKA.resources'。":null}

该程序集称为 FF.Fi_Stat.SKA.dll。我不明白这条消息,因为没有外部资源(只有嵌入式资源),并且 VS 在输出目录中没有生成具有该名称的文件。有什么想法吗?

--

更多细节:

这是我加载程序集的方式:

Assembly a = Assembly.LoadFile(assemblyFileName);

程序集的实际加载是有效的,当我尝试在程序集中创建一个类的实例时会发生异常:

Type t = a.GetType("nameofclass");
Activator.CreateInstance(t);  //fails here

这是堆栈跟踪:

Exception has been thrown by the target of an invocation.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   ...

内部异常堆栈跟踪:

   at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
   at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
   at System.Resources.ResourceManager.GetString(String name)
   at FF.Fi_Stat.SKA.RegForm.InitializeComponent()
   at FF.Fi_Stat.SKA.RegForm..ctor()

我现在意识到不起作用的 dll 在 InitializeComponent 中具有共同的这一行:

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RegForm));

但是,正如我提到的,这对于框架 3.5 来说没有问题。

--

找到解决方案

我发现问题与 dotnet 框架如何尝试查找本地化资源有关,在我将以下行添加到程序集中的 AssemblyInfo.cs 后,它又开始工作了。

[assembly: NeutralResourcesLanguageAttribute("sv-SE", UltimateResourceFallbackLocation.MainAssembly)]

【问题讨论】:

  • 改进您的问题文档。至少有一个代码 sn -p 和异常的堆栈跟踪。
  • 我已经添加了更多细节,请再看一下。
  • 找到解决方案,在上面添加注释。
  • 我发现您的解决方案很有帮助。如果它被列在答案中会更容易找到。回答自己的问题并不违反礼仪:meta.stackexchange.com/questions/17845/…
  • @xcud 好建议,我添加了自己的答案。

标签: c# visual-studio-2010


【解决方案1】:

找到解决方案

我发现问题与 dotnet 框架如何尝试查找本地化资源有关,在我将以下行添加到程序集中的 AssemblyInfo.cs 后,它又开始工作了。

[assembly: NeutralResourcesLanguageAttribute("sv-SE", UltimateResourceFallbackLocation.MainAssembly)]

【讨论】:

    【解决方案2】:

    你可以试试enabling logging in the the Fusion loader。我在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion 1 中创建DWORDEnableLog 取得了巨大成功。然后,您将在事件日志中获得详细的加载错误消息。

    【讨论】:

    • 谢谢,我刚试过,但我不知道如何使用这些信息。我可以看到它尝试加载我动态加载的 dll 的 .resources 文件但没有找到它,但它没有给出任何提示为什么在这种情况下它被认为是致命错误,因为我还可以看到它尝试加载非- 我的静态链接程序集的现有 .resources 文件,而不会引发异常。
    • Fuslogvw.exe 工具是更好的捕鼠器,无需破解注册表。
    猜你喜欢
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2011-02-13
    • 2013-04-30
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多