Search code examples
c#xmlserializationcsc

XmlSerializer deserializing bug with CSC.EXE


I created a program that works well on my computer(s) and normally also on other computers. However there is one person which is having a problem when run it and I really don't understand why, the Stacktrace is:

System.Runtime.InteropServices.ExternalException (0x80004005): Impossibile eseguire un programma. Il comando in esecuzione era "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): Unknown error (0x36b1) in 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, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine) in Microsoft.CSharp.CSharpCodeGenerator.Compile(Compi lerParameters options, String compilerDirectory, String compilerExe, String arguments, String& outputFile, Int32& nativeReturnValue, String trueArgs) in Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch (CompilerParameters options, String[] fileNames) in Microsoft.CSharp.CSharpCodeGenerator.FromSourceBat ch(CompilerParameters options, String[] sources) in Microsoft.CSharp.CSharpCodeGenerator.System.CodeDo m.Compiler.ICodeCompiler.CompileAssemblyFromSource Batch(CompilerParameters options, String[] sources) in System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence) in System.Xml.Serialization.TempAssembly.GenerateAsse mbly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies) in System.Xml.Serialization.TempAssembly..ctor(XmlMap ping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence) in System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) in SpellCaster3.Program.LoadBaseRange() in SpellCaster3.Program.Main()

As you can see the problem is connected with deserialization (that object is only deserialized) and it happens in XmlSerializer constructor.

The problem may be connected in some way with this question: Why is my windows service launching instances of csc.exe? and Why is my windows service launching instances of csc.exe?

I obviusly can't replicate the bug. Here is the code involved:

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 File involved)

<?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>

I don't know the O.S. of the user but I think it's Windows 7 home 64 bit. He has .NET 4.0 and the application is a Winforms application for .net 4.0

Link to the application if you want test it: http://dl.dropbox.com/u/762638/Files/Documents/My%20Programs/SpellCaster3/SpellCaster3.zip

Link with installer: http://dl.dropbox.com/u/762638/Files/Documents/My%20Programs/SpellCaster3/setup.zip


Solution

  • The C# compiler (csc.exe) cannot get started on that machine. The error code is miserable, E_FAIL which doesn't mean anything more than "it didn't work, don't know why". The C# compiler is needed here in order to generate the XML serialization assembly.

    This is an environmental problem, it has nothing to do with your code. Given the lousy error code, it could be anything. Malware scanners usually leads the pack in causes for trouble like that. This is a problem that the user's IT team needs to solve, there is very little that you can do about it. Although you could use sgen.exe to pre-generate the serialization assembly up front so that it doesn't have to be done on the user's machine.