代码之家  ›  专栏  ›  技术社区  ›  Terry

C#Windows服务无法将GetSection()与自定义ConfigurationSection一起使用

  •  1
  • Terry  · 技术社区  · 9 年前

    我在尝试使用自定义应用程序时遇到异常。我的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;
    

    但我得到了与第三次尝试完全相同的结果。

    如能就如何开展这项工作提出任何建议,我们将不胜感激。如果我需要提供任何其他信息,请告诉我。

    1 回复  |  直到 9 年前
        1
  •  0
  •   Terry    8 年前

    嗯,有点尴尬。我按照建议使用相同的代码和设置制作了一个新的控制台应用程序,并且它工作正常。因此,我重新检查了我的Windows服务,并尝试使用自定义程序集解析器来定位配置文件/节。我假设,当我最初编写服务时,我遇到了问题,我尝试了这个自定义解析器(它仍然不起作用)。就在那时,我发布了这个问题。但更糟糕的是,我只放回了标准的ConfigurationManager。GetSection(上面的第二个方法)机制来获取自定义节,它就可以工作了。无法解释