记一次windows服务开发中遇到的问题

  最近在研究windows service和quartz.net,所以迅速在园子大神那里扒了一个demo,运行,安装一切顺利。

  但在在App.config配置中增加了数据库连接字符串配置后,服务安装后无法启动。把这个配置去掉,连接串直接写入代码则成功启动成功。接着试了几次都是无功而返。找了各种原因:更换.net framework版本、更换服务器.....

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="xxxx" value="数据库连接串"/>
  </appSettings>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213">
        <arg key="configType" value="FILE-WATCH" />
        <arg key="configFile" value="~/Conf/log4net.config" />
        <arg key="level" value="INFO" />
      </factoryAdapter>
    </logging>
  </common>

 

  为什么增加了<appSettings>配置就会报错呢?这个问题一直困扰了我整整2天时间,真是郁闷之极。

  最后在服务器错误日志中发现无法启动的原因,确实是由于config配置问题,但具体是什么原因还是无解。

应用程序: QuartzNETWinService.exe
Framework 版本: v4.0.30319
说明: 由于未经处理的异常,进程终止。
异常信息: System.Configuration.ConfigurationErrorsException
   在 System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean)
   在 System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(System.Configuration.ConfigurationSchemaErrors)
   在 System.Configuration.ClientConfigurationSystem.EnsureInit(System.String)

异常信息: System.Configuration.ConfigurationErrorsException
   在 System.Configuration.ClientConfigurationSystem.EnsureInit(System.String)
   在 System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(System.String)
   在 System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(System.String)
   在 System.Configuration.ConfigurationManager.GetSection(System.String)
   在 Common.Logging.LogManager+<>c__DisplayClass6.<BuildLoggerFactoryAdapter>b__3()
   在 Common.Logging.Configuration.ArgUtils+<>c__DisplayClass5.<Guard>b__4()
   在 Common.Logging.Configuration.ArgUtils.Guard[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Function`1<Int32>, System.String, System.Object[])

异常信息: Common.Logging.ConfigurationException
   在 Common.Logging.Configuration.ArgUtils.Guard[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Function`1<Int32>, System.String, System.Object[])
   在 Common.Logging.LogManager.BuildLoggerFactoryAdapter()
   在 Common.Logging.LogManager.get_Adapter()
   在 Common.Logging.LogManager.GetLogger(System.Type)
   在 Wlitsoft.ProjectSample.QuartzNETWinService.MainService..ctor()
   在 Wlitsoft.ProjectSample.QuartzNETWinService.Program.Main()

  异常信息: Common.Logging.ConfigurationException?????

  难道和这个组件有关系?于是百度,关键词为“Common.Logging config”,果然发现一篇文章

  https://stackoverflow.com/questions/6779711/common-logging-config-exception

  在回复中有这么条信息

  If this element is in a configuration file, it must be the first child element of the <configuration> element

Whoa! Indeed, remove that and it works. extra points if you state 'why'. – Jahmic Jul 21 '11 at 17:18
1
I believe it's because of how the configuration schema is validated or loaded. – Klinger Jul 21 '11 at 17:33
nice one! This answer is still valid for VS2012/.Net 4.5, Common.Logging 2.1.1, Common.Logging.Log4Net 2.0.0 and log4net 1.2.10 – Lin-Art Oct 16 '12 at 11:01
3
From the docs on the configSections element: "If this element is in a configuration file, it must be the first child element of the <configuration> element." – stuartd Sep 11 '14 at 13:00
what a tricky change, took me a while to find out! – Allen Jul 26 '15 at 13:58

 

  而且还有一个指向微软官方文档,https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-1.1/aa903350(v=vs.71),终于找到了问题的症结所在!!!!

  <configSections>元素一定要作为<configuration>中的第一个元素!!!于是把<appSettings>移到了configSections节点的下方,问题解决!

  是微软给我们挖的坑,还是自己功力太潜了,不管怎么样折腾2天时间终于把问题解决了。

posted @ 2018-10-10 21:03  无聊的蚂蚁  阅读(509)  评论(0编辑  收藏  举报