2015-10-15 29 views
1

我有一個VB.net應用程序使用自定義的可序列化類來保存配置設置。在啓動時,我反序列化配置文件。這在調試器中完美工作。我在Visual Studio中運行應用程序,它調用LoadSettings函數,並實例化對象。但是,一旦我構建可執行文件並運行它,它會生成一個異常。有誰知道它爲什麼在調試器和編譯代碼之間有不同的表現?在EXE中發生XML反序列化異常,但在調試期間發生異常

配置文件的創建方式是通過序列化類Settings對象,然後使用StreamReader對象將其寫入文件。非常簡單,我一直都在這樣做。

這裏的子程序:

Private Sub LoadSettings(FromFile As String) 
    Dim ser As New XmlSerializer(GetType(UtilityClasses.Settings)) 
    Dim reader As New StreamReader(FromFile) 
    Settings = ser.Deserialize(reader) 
    reader.Close() 
End Sub 

而且,在ser.Deserialize()調用,它會引發此異常:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box. 

************** Exception Text ************** 
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Configuration file MyUtility.config does not have root <configuration> tag (C:\Data\Code\MyUtility\bin\Debug\MyUtility.config line 2) 
    at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal) 
    at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors) 
    at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors() 
    at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey) 
    --- End of inner exception stack trace --- 
    at System.Configuration.ConfigurationManager.PrepareConfigSystem() 
    at System.Configuration.ConfigurationManager.GetSection(String sectionName) 
    at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName) 
    at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection() 
    at System.Diagnostics.DiagnosticsConfiguration.Initialize() 
    at System.Diagnostics.DiagnosticsConfiguration.get_SwitchSettings() 
    at System.Diagnostics.Switch.InitializeConfigSettings() 
    at System.Diagnostics.Switch.InitializeWithStatus() 
    at System.Diagnostics.Switch.get_SwitchSetting() 
    at System.Diagnostics.BooleanSwitch.get_Enabled() 
    at System.Xml.Serialization.TempAssembly.LoadGeneratedAssembly(Type type, String defaultNamespace, XmlSerializerImplementation& contract) 
    at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) 
    at System.Xml.Serialization.XmlSerializer..ctor(Type type) 
    at CATPrd2Prt.Form1.LoadSettings(String FromFile) in C:\Data\Code\MyUtility\Form1.vb:line 99 
    at CATPrd2Prt.Form1.Form1_Load(Object sender, EventArgs e) in C:\Data\Code\MyUtility\Form1.vb:line 30 
    at System.EventHandler.Invoke(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.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 


************** Loaded Assemblies ************** 
mscorlib 
    Assembly Version: 2.0.0.0 
    Win32 Version: 2.0.50727.3662 (GDR.050727-3600) 
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll 
---------------------------------------- 
CATPrd2Prt 
    Assembly Version: 1.0.0.0 
    Win32 Version: 1.0.0.0 
    CodeBase: file:///C:/Data/Clients/Inalfa/Code/CATPrd2Prt/CATPrd2Prt/bin/Debug/CATPrd2Prt.exe 
---------------------------------------- 
Microsoft.VisualBasic 
    Assembly Version: 8.0.0.0 
    Win32 Version: 8.0.50727.3053 (netfxsp.050727-3000) 
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll 
---------------------------------------- 
System 
    Assembly Version: 2.0.0.0 
    Win32 Version: 2.0.50727.3662 (GDR.050727-3600) 
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll 
---------------------------------------- 
System.Windows.Forms 
    Assembly Version: 2.0.0.0 
    Win32 Version: 2.0.50727.3667 (GDR.050727-3600) 
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll 
---------------------------------------- 
System.Drawing 
    Assembly Version: 2.0.0.0 
    Win32 Version: 2.0.50727.3667 (GDR.050727-3600) 
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll 
---------------------------------------- 
System.Configuration 
    Assembly Version: 2.0.0.0 
    Win32 Version: 2.0.50727.3654 (GDR.050727-3600) 
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll 
---------------------------------------- 
System.Xml 
    Assembly Version: 2.0.0.0 
    Win32 Version: 2.0.50727.3662 (GDR.050727-3600) 
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll 
---------------------------------------- 
System.Runtime.Remoting 
    Assembly Version: 2.0.0.0 
    Win32 Version: 2.0.50727.3664 (GDR.050727-3600) 
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll 
---------------------------------------- 
Interop.INFITF 
    Assembly Version: 0.0.0.0 
    Win32 Version: 0.0.0.0 
    CodeBase: file:///C:/Data/Clients/Inalfa/Code/CATPrd2Prt/CATPrd2Prt/bin/Debug/Interop.INFITF.DLL 
---------------------------------------- 
System.Core 
    Assembly Version: 3.5.0.0 
    Win32 Version: 3.5.30729.1 built by: SP 
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll 
---------------------------------------- 

************** JIT Debugging ************** 
To enable just-in-time (JIT) debugging, the .config file for this 
application or computer (machine.config) must have the 
jitDebugging value set in the system.windows.forms section. 
The application must also be compiled with debugging 
enabled. 

For example: 

<configuration> 
    <system.windows.forms jitDebugging="true" /> 
</configuration> 

When JIT debugging is enabled, any unhandled exception 
will be sent to the JIT debugger registered on the computer 
rather than be handled by this dialog box. 

回答

1

啊哈,想通了。顯然,命名配置文件「MyUtility.config」導致解串器出現某種問題。但我不知道爲什麼它在調試過程中有所不同。我將名稱更改爲「MyUtility.config.xml」,它工作正常。

+0

我可以猜測,在VSHost調試器下,它必須使用不同的默認配置文件,因此不會嘗試以不同的方式加載自定義文件。而在VSHost之外,它必須嘗試加載自定義文件,因爲它是一個預期的路徑名,而且方式不同。 –