尝试在Windows服务中使用自定义app.config部分时出现异常。我已经成功地在其他应用程序中编写了自定义部分,所以我确信我的机制正确,并且在涉及Windows服务时会发生一些事情。如果需要,我可以包含app.config Xml。
首次尝试
我在C#windows服务构造函数中有以下代码:
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;
我得到以下异常。基本上,settings变量被赋值为null(没有'配置管理器'异常),因此使用该对象的调用者获得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;
但是我得到了与第三次尝试完全相同的结果。
任何关于如何使这项工作的建议将不胜感激。如果我需要提供任何其他信息,请告诉我。
答案 0 :(得分:0)