2016-10-01 17 views
1

我试图在我的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; 

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

任何意见,如何得到这个工作将不胜感激。让我知道是否需要提供任何其他信息。

+0

进程监视器可能是一个有用的诊断工具,看看是否。NET打开正确的配置文件,如果不是它做错了。您可以从Microsoft网站下载它。 –

+0

发生了灾难......我的电脑崩溃了...对不起延迟。最后回到这个。 Process Monitor正在显示它正在打印正确的文件,但它不能正确读取应用的设置。它为文件C:\ BTR \ Source \ Evolution.Service \ BTR.Evolution.Service \ bin \ Debug \ BTR.Evolution.Service.exe.config创建,读取,读取和关闭,但是我放入其中的任何设置更改没有阅读。这是你想要我确认的吗? – Terry

+1

我不确定我在想什么,但是很高兴知道它确实正在读取您期望的文件。您是否尝试过使用相同的配置文件运行相同的构造函数,但是作为普通程序而不是服务运行?这可能与配置文件中的语法错误一样简单。 –

回答

0

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

相关问题