ApplicationSettingsBase中的FileNotFoundException

调试应用程序时,在Visual Studio中启用了中断exception时,我总是会收到以下错误。 这真是让我感到烦恼,因为我们在中断exception时工作。 有趣的是,当我继续(加载StringCollection)时它仍然有效。

消息是:

无法加载文件或程序集’System.XmlSerializers,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089’或其依赖项之一。 该系统找不到指定的文件。

这是导致exception的代码(设计器生成)

[global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] public global::System.Collections.Specialized.StringCollection Mru { get { return ((global::System.Collections.Specialized.StringCollection)(this["Mru"])); } set { this["Mru"] = value; } } 

我试图创建一个显示错误的空测试应用程序,但没有发生exception。 我们的项目非常庞大,因此很难找到原因。 也许这个网站上有人知道如何解决这个问题。

只是解释为什么抛出这个exception。 您可以使用此示例Windows窗体应用程序重新生成exception。 首先添加一个名为“Setting”的StringCollection类型的设置。 单击“值”列中的点,然后输入几个字符串。 使表单类代码如下所示:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void OnFormClosing(FormClosingEventArgs e) { Properties.Settings.Default.Setting[0] = DateTime.Now.ToString(); Properties.Settings.Default.Save(); base.OnFormClosing(e); } } 

Debug + Exceptions,勾选CLRexception的Thrown复选框。 运行该表单并关闭它,调试器将在抛出exception时停止。 调用堆栈的顶部如下所示:

 mscorlib.dll!System.Reflection.Assembly.nLoad(System.Reflection.AssemblyName fileName, string codeBase, System.Security.Policy.Evidence assemblySecurity, System.Reflection.Assembly locationHint, ref System.Threading.StackCrawlMark stackMark, bool throwOnFileNotFound, bool forIntrospection) + 0x2c bytes mscorlib.dll!System.Reflection.Assembly.InternalLoad(System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity, ref System.Threading.StackCrawlMark stackMark, bool forIntrospection) + 0x80 bytes mscorlib.dll!System.Reflection.Assembly.Load(System.Reflection.AssemblyName assemblyRef) + 0x1d bytes System.Xml.dll!System.Xml.Serialization.TempAssembly.LoadGeneratedAssembly(System.Type type = {Name = "StringCollection" FullName = "System.Collections.Specialized.StringCollection"}, string defaultNamespace = null, out System.Xml.Serialization.XmlSerializerImplementation contract = null) + 0xcd bytes System.Xml.dll!System.Xml.Serialization.XmlSerializer.XmlSerializer(System.Type type = {Name = "StringCollection" FullName = "System.Collections.Specialized.StringCollection"}, string defaultNamespace = null) + 0x105 bytes 

您可以看到XmlSerializer类搜索包含StringCollection类的XML序列化程序的程序集。 LoadGeneratedAssembly方法看起来像这样,删除了无聊的位:

 internal static Assembly LoadGeneratedAssembly(Type type, string defaultNamespace, out XmlSerializerImplementation contract) { ... AssemblyName parent = GetName(type.Assembly, true); partialName = Compiler.GetTempAssemblyName(parent, defaultNamespace); parent.Name = partialName; parent.CodeBase = null; parent.CultureInfo = CultureInfo.InvariantCulture; try { serializer = Assembly.Load(parent); // <=== here } catch (Exception exception) { ... } .... } 

和Compiler.GetTempAssemblyName():

 internal static string GetTempAssemblyName(AssemblyName parent, string ns) { return (parent.Name + ".XmlSerializers" + (((ns == null) || (ns.Length == 0)) ? "" : ("." + ns.GetHashCode()))); } 

在这种情况下,此GetTempAssemblyName是邪恶的行为者。 StringCollection类存在于System.dll程序集中,该方法生成名称“System.XmlSerializers”。 此方法旨在查找您自己的类的程序集,即Sgen.exe生成的程序集。 就像您的示例程序的WindowsApplication1.XmlSerializers.dll一样。 但是StringCollection是.NET Framework中的一个类,它生成的程序集名称无效。 框架中实际上没有“System.XmlSerializers.dll”程序集。

有关connect.microsoft.com上此行为的反馈报告已全部以“按设计”关闭。 原来,设计师认为防止exception的成本太高,并决定抓住exception。 哪一切都运作正常,这个例外确实被抓住了。 您恰好看到它,因为您在Debug + Exceptions对话框中打开了Thrown复选框。

在这里使Xml序列化代码行为不同是不可取的。 他们很容易过滤出System.dll程序集中的类型,但这可能是永无止境的战斗,框架中有更多的程序集。 解决方法是使用您自己的类来存储设置,而不是使用StringCollection。

因为这似乎是正常操作的一部分(另请参阅: XmlSerializer在构造函数中给出FileNotFoundException ),我只能提供两种解决方法:

禁用此特定exception:转到Debug / Exceptions,单击Add,键入:C ++ Exceptions,Name:EEFileLoadException(如果这是您看到的exception),取消选中此exception的Thrown复选框。

将设置的类型更改为字符串并访问它,例如:

 var mru = Settings.Default.Mru.Split('|'); Settings.Default.Mru = string.Join("|", mru.ToArray()); 

您捕获太多exception,System.XmlSerializer将始终抛出此exception作为其正常操作的一部分,它由类本身捕获和处理。 将调试选项更改为仅捕获exception,而不是在.net farmework类中捕获和处理的exception。