我試圖在我的Windows服務中使用自定義app.config部分時出現異常。我已經成功地在其他應用程序中編寫了自定義部分,所以非常確定我擁有正確的機制,並且在涉及Windows服務時只會發生一些事情。如果需要,我可以包含app.config Xml。C#Windows服務無法使用GetSection()與自定義ConfigurationSection
第一次嘗試
我有一個C#窗口服務構造如下代碼:
int systemErrorWindowSeconds =
ServiceSettings.Current.ExceptionHandling.SystemErrorWindowSeconds;
而且ServiceSettings(重要位)如下:
public class ServiceSettings : ConfigurationSection
{
private static ServiceSettings settings;
static ServiceSettings()
{
var config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
settings = config.GetSection("serviceSettings") as ServiceSettings;
}
public static ServiceSettings Current { get { return settings; } }
當我試圖訪問ServiceSettings.Current
我收到以下異常:
Application: BTR.Evolution.Service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Configuration.ConfigurationErrorsException
at System.Configuration.BaseConfigurationRecord.EvaluateOne(System.String[], System.Configuration.SectionInput, Boolean, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object)
at System.Configuration.BaseConfigurationRecord.Evaluate(System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(System.String, Boolean, Boolean, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.Configuration.GetSection(System.String)
at BTR.Evolution.Service.ServiceSettings..cctor() Exception Info: System.TypeInitializationException
at BTR.Evolution.Service.ServiceSettings.get_Current()
at BTR.Evolution.Service.EvolutionService..ctor(System.String[])
at BTR.Evolution.Service.Program.Main(System.String[])
第二次嘗試:
我改變了我的ServiceSettings類使用GetSection像:
static ServiceSettings()
{
settings = ConfigurationManager.GetSection("serviceSettings") as ServiceSettings;
}
我得到下面的異常(主要是與上面相同,但使用ConfigurationManager中和幾個GetSectionRecursive電話)
Application: BTR.Evolution.Service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info:
System.Configuration.ConfigurationErrorsException
at System.Configuration.BaseConfigurationRecord.EvaluateOne(System.String[], System.Configuration.SectionInput, Boolean, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object)
at System.Configuration.BaseConfigurationRecord.Evaluate(System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(System.String, Boolean, Boolean, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(System.String, Boolean, Boolean, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(System.String, Boolean, Boolean, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSection(System.String)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(System.String)
at System.Configuration.ConfigurationManager.GetSection(System.String)
at BTR.Evolution.Service.ServiceSettings..cctor() Exception Info: System.TypeInitializationException
at BTR.Evolution.Service.ServiceSettings.get_Current()
at BTR.Evolution.Service.EvolutionService..ctor(System.String[])
at BTR.Evolution.Service.Program.Main(System.String[])
第三次嘗試
我改變了我的ServiceSettings構造嘗試:
var customConfig =
ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
settings = customConfig.GetSection("serviceSettings") as ServiceSettings;
而且我得到了以下異常。基本上,設置變量被賦值爲null(沒有'配置管理器'異常),所以使用該對象的調用者得到空的異常。
Application: BTR.Evolution.Service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at BTR.Evolution.Service.EvolutionService..ctor(System.String[])
at BTR.Evolution.Service.Program.Main(System.String[])
最後一次嘗試
我改變了我的ServiceSettings構造函數來嘗試使用裝配resovler。
Func<object, ResolveEventArgs, System.Reflection.Assembly> getAssembly =
(sender, args) => typeof(ServiceSettings).Assembly;
var resolveHandler = new System.ResolveEventHandler(getAssembly);
AppDomain.CurrentDomain.AssemblyResolve += resolveHandler;
var customConfig =
ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
settings = customConfig.GetSection("serviceSettings") as ServiceSettings;
AppDomain.CurrentDomain.AssemblyResolve -= resolveHandler;
但是我得到了與第三次嘗試完全相同的結果。
任何意見,如何得到這個工作將不勝感激。讓我知道是否需要提供任何其他信息。
進程監視器可能是一個有用的診斷工具,看看是否。NET打開正確的配置文件,如果不是它做錯了。您可以從Microsoft網站下載它。 –
發生了災難......我的電腦崩潰了...對不起延遲。最後回到這個。 Process Monitor正在顯示它正在打印正確的文件,但它不能正確讀取應用的設置。它爲文件C:\ BTR \ Source \ Evolution.Service \ BTR.Evolution.Service \ bin \ Debug \ BTR.Evolution.Service.exe.config創建,讀取,讀取和關閉,但是我放入其中的任何設置更改沒有閱讀。這是你想要我確認的嗎? – Terry
我不確定我在想什麼,但是很高興知道它確實正在讀取您期望的文件。您是否嘗試過使用相同的配置文件運行相同的構造函數,但是作爲普通程序而不是服務運行?這可能與配置文件中的語法錯誤一樣簡單。 –