【问题标题】:XmlSerializer deserializing bug with CSC.EXEXmlSerializer 使用 CSC.EXE 反序列化错误
【发布时间】:2011-07-20 18:42:27
【问题描述】:

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

System.Runtime.InteropServices.ExternalException (0x80004005): 不可能的 eseguire un programma。 Il comando in esecuzione 时代 "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\c sc.exe" /noconfig /fullpaths @"C:\Users\Andry\AppData\Local\Temp\dot0eqxi.cmdline"。 ---> System.ComponentModel.Win32Exception (0x80004005): 未知错误 (0x36b1) 在 System.CodeDom.Compiler.Executor.ExecWaitWithCaptu reUnimpersonated(SafeUserTokenHandle userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine) in System.CodeDom.Compiler.Executor.ExecWaitWithCaptu re(SafeUserTokenHandle userToken, 字符串 cmd, 字符串 currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, 字符串 trueCmdLine) 在 Microsoft.CSharp.CSharpCodeGenerator.Compile(编译器参数 选项,字符串编译器目录,字符串编译器Exe,字符串 参数,字符串& outputFile,Int32& nativeReturnValue,字符串 trueArgs) 在 Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch (CompilerParameters 选项,String[] 文件名)在 Microsoft.CSharp.CSharpCodeGenerator.FromSourceBat ch(CompilerParameters options, String[] sources) in Microsoft.CSharp.CSharpCodeGenerator.System.CodeDo m.Compiler.ICodeCompiler.CompileAssemblyFromSource 批处理(编译器参数选项,字符串 [] 来源)在 System.Xml.Serialization.Compiler.Compile(程序集父,字符串 ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence) 中 System.Xml.Serialization.TempAssembly.GenerateAsse mbly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters 参数,Assembly 程序集, 哈希表程序集)在 System.Xml.Serialization.TempAssembly..ctor(XmlMap ping[] xmlMappings, Type[] 类型、字符串默认命名空间、字符串位置、证据 证据)在 System.Xml.Serialization.XmlSerializer..ctor(类型类型, String 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?

我显然无法复制该错误。 以下是涉及的代码:

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>

我不知道操作系统。的用户,但我认为它是 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

【问题讨论】:

  • 你能在他的机器上设置远程调试吗?
  • 我不知道怎么做,但我可以尝试,但不是“今天”,我需要他的 Skype 第一
  • Mhh 远程调试需要局域网连接之类的东西吗?因为我不知道怎么看他的windows账号

标签: c# xml serialization csc


【解决方案1】:

C# 编译器 (csc.exe) 无法在该计算机上启动。错误代码很糟糕,E_FAIL 仅表示“它不起作用,不知道为什么”。这里需要 C# 编译器来生成 XML 序列化程序集。

这是环境问题,与你的代码无关。鉴于糟糕的错误代码,它可能是任何东西。恶意软件扫描程序通常会导致诸如此类的麻烦。这是用户的 IT 团队需要解决的问题,您对此无能为力。虽然您可以使用 sgen.exe 预先生成序列化程序集,这样就不必在用户的机器上完成。

【讨论】:

  • 这似乎是对错误的公平评估。 +1 对于 sgen.exe 解决方案。它还将提高应用程序(启动)性能
  • 如果我了解如何使 sgen 工作,我会尝试:P 顺便感谢您的建议。我让他重新安装.NET 4,也许会工作
  • 是的,问题在于,他卸载并重新安装了 .NET Framework 4,一切正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多