今天碰到了個非常奇怪的問題,而且無法重現。
在一個類構造的時候從 xml 檔案還原序列化一個對象。一般情況下都是好,極少數情況下會出現一下問題。
System.Runtime.InteropServices.ExternalException: Timed out waiting for a program to execute. The command being executed was "c:\windows\microsoft.net\framework\v1.1.4322\csc.exe" /noconfig @"C:\DOCUME~1\montaq~1\LOCALS~1\Temp\prfmes01.cmdline".
at System.CodeDom.Compiler.Executor.ExecWaitWithCaptureUnimpersonated(IntPtr userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
at System.CodeDom.Compiler.Executor.ExecWaitWithCapture(IntPtr userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
at System.CodeDom.Compiler.CodeCompiler.Compile(CompilerParameters options, String compilerDirectory, String compilerExe, String arguments, String& outputFile, Int32& nativeReturnValue, String trueArgs)
at System.CodeDom.Compiler.CodeCompiler.FromFileBatch(CompilerParameters options, String[] fileNames)
at System.CodeDom.Compiler.CodeCompiler.FromSourceBatch(CompilerParameters options, String[] sources)
at System.CodeDom.Compiler.CodeCompiler.FromSource(CompilerParameters options, String source)
at System.CodeDom.Compiler.CodeCompiler.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSource(CompilerParameters options, String source)
at System.Xml.Serialization.Compiler.Compile()
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at XmlState..ctor(String path)
at AutoUpdateingAddin.Updater..ctor(String hostAppExeFullPath)
at AutoUpdateingAddin.Form1.CheckUpdate()
at AutoUpdateingAddin.Form1.Form1_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
xml 檔案不便,程式也不變,有時候就會有這樣的錯誤。
從這個錯誤看出來,對象還原序列化的時候,會調用動態編譯,首先動態產生一些代碼,然後再去編譯執行。hoho,中間結果都放在temp檔案中。執行結束的時候,delete他。
用reflector 驗證了一下,果然如此。(y)
為了測試,我寫一個簡單的還原序列化程式
建立一個類
using System;
using System.Xml;
using System.Xml.Serialization;
namespace XmlDemo
{
[XmlRoot("Person",Namespace="")]
public class Person
{
[XmlElement("PersonName")]
public string Name;
public int Age;
}
}
然後我產生一個該類的執行個體並把它寫道xml 檔案。檔案內容如下
<?xml version="1.0" ?>
- <Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PersonName>Montaque</PersonName>
<Age>30</Age>
</Person>
接下來就是還原序列化這個類,記住要引用 System.runtime.Serialization.dll
代碼如下
System.Xml.Serialization.XmlSerializer xr=new System.Xml.Serialization.XmlSerializer(typeof(Person)) ;
System.IO.FileStream fs=new System.IO.FileStream("c:\\test.xml",System.IO.FileMode.Open ,System.IO.FileAccess.Read);
Person p =(Person)xr.Deserialize(fs);
Console.WriteLine(p.Name);
運行結果沒有問題。
運行過程中,我用檔案監視器堅實了一下csc.exe ,果然他先產生一下中間的cs,然後編譯。。。
如