c# - ApplicationSettingsBase 中的 FileNotFoundException

标签 c# application-settings applicationsettingsbase

在调试应用程序时,如果在 Visual Studio 中启用了异常中断,我总是会收到以下错误。这真的很烦我,因为我们使用异常中断。有趣的是,当我继续时它仍然有效(StringCollection 已加载)。

信息是:

Could not load file or assembly 'System.XmlSerializers, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

这是导致异常的代码(设计者生成的)

[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;
        }
    }

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

最佳答案

只是解释为什么抛出这个异常。您可以使用此示例 Windows 窗体应用程序重现异常。首先添加一个名为“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,勾选 CLR 异常的 Thrown 复选框。运行窗体并关闭它,调试器将在抛出异常时停止。调用堆栈的顶部如下所示:

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 上有关此行为的反馈报告都已“按设计”关闭。原来的设计者考虑到防止异常的成本太高而决定只捕获异常。一切正常,确实捕获了异常。您只是碰巧看到了它,因为您在 Debug + Exceptions 对话框中打开了 Thrown 复选框。

让 Xml 序列化代码在此处表现不同不是一种选择。他们可以很容易地过滤掉 System.dll 程序集中的类型,但这可能是一场永无止境的战斗,框架中有更多的程序集。解决方法是使用您自己的类来存储设置,而不是使用 StringCollection。

关于c# - ApplicationSettingsBase 中的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61260117/

相关文章:

c# - Entity Framework : is it possible to make object context internal?

Linux 配置数据 : standard way of storing application settings?

c# - ApplicationSettingsBase ConfigurationErrorsException 多个程序集

c# - 自动将 View 中的元素映射到 dateTime 类型的模型

c# - Owin 声明 - 添加多个 ClaimTypes.Role

design-patterns - 哪些设计模式可以应用于配置设置问题?

c# - ApplicationSettingsBase 中的 FileNotFoundException

c# - 使用 C# 插入字段名称为 TA/TJ 的数据库

linux - 他们是否有任何特定标准来保留 linux 应用程序的设置?