C# 在C中实例化System.Xml.Serialization.XmlSerializer时崩溃#

C# 在C中实例化System.Xml.Serialization.XmlSerializer时崩溃#,c#,.net,serialization,C#,.net,Serialization,在C#library中实例化System.Xml.Serialization.XmlSerializer类的实例时,我们看到了崩溃。当构造函数试图向字典中添加重复键时,会发生崩溃。我在下面包含了一个堆栈跟踪 这次崩溃只发生在一台机器上,修复我们的.NET3.5安装没有帮助。还有其他人看到过类似的问题吗 System.ArgumentException was unhandled Message="Item has already been added. Key in dictionary:

在C#library中实例化
System.Xml.Serialization.XmlSerializer
类的实例时,我们看到了崩溃。当构造函数试图向字典中添加重复键时,会发生崩溃。我在下面包含了一个堆栈跟踪

这次崩溃只发生在一台机器上,修复我们的.NET3.5安装没有帮助。还有其他人看到过类似的问题吗

System.ArgumentException was unhandled
  Message="Item has already been added. Key in dictionary: 'mainbuild'  Key being added: 'mainbuild'"
  Source="mscorlib"
  StackTrace:
       at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
       at System.Collections.Hashtable.Add(Object key, Object value)
       at System.Collections.Specialized.StringDictionary.Add(String key, String value)
       at System.CodeDom.Compiler.Executor.ExecWaitWithCaptureUnimpersonated(SafeUserTokenHandle userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
       at System.CodeDom.Compiler.Executor.ExecWaitWithCapture(SafeUserTokenHandle userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
       at Microsoft.CSharp.CSharpCodeGenerator.Compile(CompilerParameters options, String compilerDirectory, String compilerExe, String arguments, String& outputFile, Int32& nativeReturnValue, String trueArgs)
       at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
       at Microsoft.CSharp.CSharpCodeGenerator.FromSourceBatch(CompilerParameters options, String[] sources)
       at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSourceBatch(CompilerParameters options, String[] sources)
       at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromSource(CompilerParameters options, String[] sources)
       at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
       at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
       at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
       at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
       at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
       at System.Xml.Serialization.XmlSerializer..ctor(Type type)
       at OurTools.Tools.Common.XML.DataAccess`1.DeserializeFromXml(String strFilePath) in c:\AutomatedBuild\projects\1.0\OurTools.Tools.Common\OurTools.Tools.Common\XML\DataAcess.cs:line 100
       at OurTools.Tools.Common.ProjectFileManager.GetProjectInfoModel() in c:\AutomatedBuild\projects\1.0\OurTools.Tools.Common\OurTools.Tools.Common\ProjectFileManager.cs:line 252
       at OurTools.Tools.Common.ProjectFileManager.GetAvailableCultures() in c:\AutomatedBuild\projects\1.0\OurTools.Tools.Common\OurTools.Tools.Common\ProjectFileManager.cs:line 299
       at OurAppLib.GeneratorOptions.DefaultCultures() in c:\AutomatedBuild\projects\1.0\OurApp\OurAppLib\GeneratorOptions.cs:line 192
       at OurAppLib.GeneratorOptions.ReadCulturesFromArgs(List`1 arglist, String& errormsg) in c:\AutomatedBuild\projects\1.0\OurApp\OurAppLib\GeneratorOptions.cs:line 358
       at OurAppLib.GeneratorOptions.ReadFromArgs(String[] args, String& errormsg) in c:\AutomatedBuild\projects\1.0\OurApp\OurAppLib\GeneratorOptions.cs:line 261
       at OurApp.Program.Main(String[] args) in c:\AutomatedBuild\projects\1.0\OurApp\OurApp\Program.cs:line 76`print("code sample");`

找到此链接,该链接解释了问题:

快速总结:序列化时,对象会使用所有环境变量生成一个字典,但似乎会对所有条目运行ToLower()。所以,如果你有两个环境变量除了外壳外都是相同的,你会得到崩溃

这只会在从cygwin这样的系统内部运行时出现问题,cygwin强制变量区分大小写。在我们的例子中,我们使用make


有两种解决方案,但它们都围绕着确保您的环境在运行c#app时没有任何重复的变量而展开。

我也遇到了完全相同的问题(同样来自Cygwin),因此您的答案非常有用!很高兴我能为你节省一些时间。为了弄清楚这一点,我已经把头撞在墙上好一阵子了。这个错误已经在.Net运行时的最新版本中修复了,在4.0和4.7之间有一个“最近”值。它仍然存在于2.0-3.5运行时中。