我在尝试使用自定义应用程序时遇到异常。我的Windows服务中的配置节。我已经成功地在其他应用程序中编写了自定义部分,所以我非常确定我的机制是正确的,并且当涉及到Windows服务时,会发生一些事情。我可以包括应用程序。如果需要,配置Xml。
第一次尝试
我在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(没有“配置管理器”异常),因此使用该对象的调用者得到一个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构造函数以尝试使用程序集解析程序。
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;
但我得到了与第三次尝试完全相同的结果。
如能就如何开展这项工作提出任何建议,我们将不胜感激。如果我需要提供任何其他信息,请告诉我。