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

时间:2016-10-01 18:55:03

标签: c# windows-services app-config

尝试在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;

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

任何关于如何使这项工作的建议将不胜感激。如果我需要提供任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

嗯,有点尴尬。我根据建议使用相同的代码和设置创建了一个新的控制台应用程序,它工作所以重新检查我的Windows服务,我试图使用自定义程序集解析器来找到配置文件/部分。我假设当我最初编写服务时,我遇到了问题而且我尝试了这个自定义解析器(它仍然无效)。那是我发布这个问题的时候。但是对于它,我只回放了标准的ConfigurationManager.GetSection(上面的第二种方法)机制来获取自定义部分并且它有效。无法解释