XmlSerializer使用CSC.EXE反序列化错误

时间:2011-07-20 18:42:27

标签: c# xml serialization csc

我创建的程序在我的计算机上运行良好,通常也在其他计算机上运行。 但是有一个人在运行时遇到问题我真的不明白为什么,Stacktrace是:

  

System.Runtime.InteropServices.ExternalException(0x80004005):   Impossibile eseguire un programma。在esecuzione时代的Il comando   “C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ c sc.exe”/ noconfig   / fullpaths @“C:\ Users \ Andry \ AppData \ Local \ Temp \ dot0eqxi.cmdli ne”。   ---> System.ComponentModel.Win32Exception(0x80004005):未知错误   System.CodeDom.Compiler.Executor.ExecWaitWithCaptu中的(0x36b1)   reUnimpersonated(SafeUserTokenHandle userToken,String cmd,String   currentDir,TempFileCollection tempFiles,String& outputName,String&   errorName,String trueCmdLine)in   System.CodeDom.Compiler.Executor.ExecWaitWithCaptu   re(SafeUserTokenHandle userToken,String cmd,String currentDir,   TempFileCollection tempFiles,String& outputName,String& errorName,   字符串trueCmdLine)in   Microsoft.CSharp.CSharpCodeGenerator.Compile(Compi lerParameters   options,String compilerDirectory,String compilerExe,String   参数,字符串& outputFile,Int32& nativeReturnValue,String   true.CSharp.CSharpCodeGenerator.FromFileBatch中的trueArgs)   (CompilerParameters选项,String [] fileNames)中   Microsoft.CSharp.CSharpCodeGenerator.FromSourceBat   ch(CompilerParameters options,String [] sources)in   Microsoft.CSharp.CSharpCodeGenerator.System.CodeDo   m.Compiler.ICodeCompiler.CompileAssemblyFromSource   批处理(CompilerParameters选项,String []源)   System.Xml.Serialization.Compiler.Compile(程序集父级,字符串ns,   XmlSerializerCompilerParameters xmlParameters,Evidence evidence)in   mly的System.Xml.Serialization.TempAssembly.GenerateAsse(XmlMapping []   xmlMappings,Type [] types,String defaultNamespace,Evidence evidence,   XmlSerializerCompilerParameters参数,Assembly assembly,   Hashtable程序集)   System.Xml.Serialization.TempAssembly..ctor(XmlMap ping [] xmlMappings,   键入[]类型,字符串defaultNamespace,字符串位置,证据   证据)在System.Xml.Serialization.XmlSerializer..ctor(类型类型,   字符串defaultNamespace)在SpellCaster3.Program.LoadBaseRange()中   SpellCaster3.Program.Main()

正如您所看到的,该问题与反序列化有关(该对象仅被反序列化),并且它发生在XmlSerializer构造函数中。

问题可能与此问题有某种关联:Why is my windows service launching instances of csc.exe?Why is my windows service launching instances of csc.exe?

我很不能复制这个bug。 以下是涉及的代码:

Program.cs的

    private static void LoadBaseRange()
    {
        string fileIconImageIndices = Application.StartupPath + Path.DirectorySeparatorChar + "ValidIconImageIndices.xml";
        if (!File.Exists(fileIconImageIndices)) throw new FileNotFoundException("Attenzione, il file degli indici delle immagini non è stato trovato");

        using (StreamReader reader = new StreamReader(fileIconImageIndices, Encoding.UTF8))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(RangeCollection));
            Forms.GumpPicker.BaseRange = serializer.Deserialize(reader) as RangeCollection;
        }
    }

    /// <summary>
    /// Punto di ingresso principale dell'applicazione.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
        try
        {
            LoadBaseRange();
        }
        catch (FileNotFoundException fileException)
        {
            ErrorForm.Show(fileException.Message + "\nL'applicazione verrà terminata", fileException);
            return;
        }
        catch (Exception exception)
        {
            ErrorForm.Show("L'applicazione verrà terminata", exception);
            return;
        }

RangeCollection.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Expand.Linq
{
    public class RangeCollection : IXmlSerializable
    {
        public static readonly Version Version = new Version(1, 0, 0, 0);

        internal static Version FoundVersion { get; private set; }

        /// <summary>
        /// Used for xml deserialization
        /// </summary>
        public RangeCollection() { }

        public RangeCollection(IEnumerable<IEnumerable<int>> ranges)
        {
            Range = ConvertToListInt(ranges);
        }

        private List<int> ConvertToListInt(IEnumerable<IEnumerable<int>> ranges)
        {
            IEnumerable<int> tmpRange;
            tmpRange = Enumerable.Empty<int>();

            foreach (IEnumerable<int> range in ranges)
                tmpRange = tmpRange.Union(range);

            tmpRange = tmpRange.OrderBy(number => number);
            return new List<int>(tmpRange);
        }


        public static implicit operator RangeCollection(List<IEnumerable<int>> ranges)
        {
            return new RangeCollection(ranges);
        }

        public List<int> Range { get; private set; }

        public int Maximum
        {
            get
            {
                return Range.Max();
            }
        }

        public int Minimum
        {
            get
            {
                return Range.Min();
            }
        }

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            List<IEnumerable<int>> ranges = new List<IEnumerable<int>>(100);
            string elementName = "Range";
            Version version;

            version = Version.Parse(reader.GetAttribute("Version"));
            FoundVersion = version;

            if (reader.IsEmptyElement) return;

            reader.ReadStartElement(GetType().Name);
            while (true)
            {
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == elementName)
                {
                    reader.ReadStartElement(elementName);
                    int start = reader.ReadElementContentAsInt();
                    int end = reader.ReadElementContentAsInt();
                    reader.ReadEndElement();
                    if (start == end)
                        ranges.Add(ExEnumerable.Range(start));
                    else
                        ranges.Add(ExEnumerable.RangeBetween(start, end));
                }
                else
                    break;
            }
            reader.ReadEndElement();
            Range = ConvertToListInt(ranges);
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            throw new NotSupportedException();
        }
    }
}

ValidIconImageIndices.xml (涉及XML文件)

<?xml version="1.0" encoding="utf-8" ?>
<RangeCollection Version="1.0.0.0">
  <Range>
    <Start>1236</Start>
    <End>1246</End>
  </Range>
  <Range>
    <Start>1260</Start>
    <End>1260</End>
  </Range>
  <Range>
    <Start>1263</Start>
    <End>1287</End>
  </Range>
  <Range>
    <Start>1300</Start>
    <End>1309</End>
  </Range>
  <Range>
    <Start>1311</Start>
    <End>1312</End>
  </Range>
  <Range>
    <Start>1401</Start>
    <End>1415</End>
  </Range>
  <Range>
    <Start>1782</Start>
    <End>1782</End>
  </Range>
  <Range>
    <Start>1789</Start>
    <End>1795</End>
  </Range>
  <Range>
    <Start>2240</Start>
    <End>2303</End>
  </Range>
  <Range>
    <Start>2406</Start>
    <End>2408</End>
  </Range>
  <Range>
    <Start>2410</Start>
    <End>2419</End>
  </Range>
  <Range>
    <Start>20480</Start>
    <End>20496</End>
  </Range>
  <Range>
    <Start>20736</Start>
    <End>20745</End>
  </Range>
  <Range>
    <Start>20992</Start>
    <End>21020</End>
  </Range>
  <Range>
    <Start>21248</Start>
    <End>21248</End>
  </Range>
  <Range>
    <Start>21251</Start>
    <End>21257</End>
  </Range>
  <Range>
    <Start>21280</Start>
    <End>21287</End>
  </Range>
  <Range>
    <Start>21504</Start>
    <End>21507</End>
  </Range>
  <Range>
    <Start>21536</Start>
    <End>21542</End>
  </Range>
  <Range>
    <Start>21632</Start>
    <End>21632</End>
  </Range>
  <Range>
    <Start>23000</Start>
    <End>23015</End>
  </Range>
</RangeCollection>

我不知道O.S.用户但我认为这是Windows 7家64位。 他有.NET 4.0,该应用程序是.net 4.0的Winforms应用程序

如果您想测试它,请链接到该应用程序: http://dl.dropbox.com/u/762638/Files/Documents/My%20Programs/SpellCaster3/SpellCaster3.zip

与安装人员联系: http://dl.dropbox.com/u/762638/Files/Documents/My%20Programs/SpellCaster3/setup.zip

1 个答案:

答案 0 :(得分:6)

C#编译器(csc.exe)无法在该计算机上启动。错误代码是悲惨的,E_FAIL并不意味着“它不起作用,不知道为什么”。这里需要C#编译器来生成XML序列化程序集。

这是一个环境问题,它与您的代码无关。鉴于糟糕的错误代码,它可能是任何东西。恶意软件扫描程序通常会引发这样的问题。这是用户的IT团队需要解决的问题,您可以做的很少。虽然您可以使用sgen.exe预先生成序列化程序集,但不必在用户的计算机上完成。