简体   繁体   English

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

[英]Crash Instantiating System.Xml.Serialization.XmlSerializer in C#

We're seeing a crash when instantiating an instance of the System.Xml.Serialization.XmlSerializer class in a C# library. 在C#库中实例化System.Xml.Serialization.XmlSerializer类的实例时,我们看到崩溃。 The crash occurs in the constructor, when it tries to add a duplicate key to a dictionary. 当尝试向字典添加重复键时,崩溃发生在构造函数中。 I've included a stack trace below. 我在下面包括了堆栈跟踪。

This crash is only occurring on one machine, and repairing our installation of .NET 3.5 didn't help. 此崩溃仅在一台计算机上发生,并且修复我们的.NET 3.5安装无济于事。 Has anyone else seen any similar issues? 有没有其他人看到过类似的问题?

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");`

Found this link, which explains the issue: http://social.msdn.microsoft.com/forums/en-US/asmxandxml/thread/4476f044-bab9-492d-bb94-4e0960bd2d26 找到此链接,此链接解释了该问题: http : //social.msdn.microsoft.com/forums/zh-CN/asmxandxml/thread/4476f044-bab9-492d-bb94-4e0960bd2d26

A quick summary: When serializing, the object makes a dictionary out of all environment variables, but appears to run a ToLower() on all entries. 快速总结:序列化时,该对象从所有环境变量中创建一个字典,但似乎在所有条目上运行ToLower()。 So, if you have two environment variables that are the same except for casing, you'll get a crash. 因此,如果您有两个除了大小写相同的环境变量,则会崩溃。

This is only going to be a problem when running from inside a system like cygwin which enforces case sensitivity for variables. 从像cygwin这样的系统内部运行时,这只会是一个问题,该系统对变量强制区分大小写。 In our case, we're using make. 就我们而言,我们正在使用make。

There are a couple solutions, but they all revolve around making sure that your environment doesn't have any duplicated variables when your c# app runs. 有几种解决方案,但是它们都围绕确保运行c#应用程序时您的环境没有任何重复的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM