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

本文详细记录了解决在Windows Service中使用Quartz.NET时遇到的配置异常问题,特别是在App.config中加入数据库连接字符串后导致服务无法启动的情况。通过调整<configSections>元素的位置,最终解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  最近在研究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天时间终于把问题解决了。

转载于:https://www.cnblogs.com/gougou1981/p/9769036.html

<think>好的,我现在需要帮助用户解决clientUpload.exe程序因为System.FormatException而崩溃的问题,特别是涉及v4.0.30319框架下的Int32.Parse方法。首先,我要回忆一下System.FormatException通常是什么原因引起的。这个异常通常发生在将字符串转换为整数时,输入的字符串格式不正确,比如包含非数字字符、小数点,或者有空格等。 接下来,用户提到问题发生在Int32.Parse方法中。所以,我应该先检查输入到Int32.Parse的字符串是否有效。可能的情况包括空字符串、null值、格式不正确的数字字符串,或者是带有千位分隔符的数值。例如,像"123,456"这样的字符串如果直接转换就会出错,因为逗号不是数字的一部分。 然后,我需要考虑可能的解决方案。第一步应该是验证输入数据,确保传递给Int32.Parse的字符串是有效的整数格式。可能使用Int32.TryParse方法代替,因为它不会抛出异常,而是返回一个布尔值表示转换是否成功。这样程序可以更优雅地处理无效输入。 另外,检查数据来源也很重要。如果数据来自用户输入、文件或网络,可能存在不可控的因素,比如用户输入了字母,或者文件中有损坏的数据。这时候需要进行更严格的数据清洗和验证,比如使用正则表达式来确保字符串只包含数字,或者去除空格和其他非数字字符。 还有,异常处理也是关键。在调用Int32.Parse的地方添加try-catch块,捕获FormatException,并录详细的错误信息,比如出错的字符串内容和堆栈跟踪,这样有助于快速定位问题所在。 对于框架版本v4.0.30319,可能需要确认是否有相关的补丁或更新,但更可能的问题还是出在数据本身。此外,检查代码中的区域性设置,特别是当处理带有不同格式的数字时,比如有些文化中使用逗号作为小数点,这时候可能需要指定InvariantCulture来确保解析的一致性。 最后,建议用户进行单元测试或添加日志录,以便在出现问题时能够捕获具体的输入数据,从而更容易复现和修复问题。</think>### 问题分析 `System.FormatException`通常由字符串转换为数值类型时格式不匹配触发。在`Int32.Parse`中,若输入字符串包含非数字字符(如字母、多余小数点、空格)或为空,则会抛出此异常[^1]。崩溃的根本原因是代码未对输入数据做有效性校验。 --- ### 分步解决方案 #### 1. 验证输入数据格式 在调用`Int32.Parse`前,检查字符串是否满足整数格式: ```csharp string input = GetInput(); // 假设从外部获取输入 if (!string.IsNullOrEmpty(input) && input.All(char.IsDigit)) { int number = int.Parse(input); } else { // 录错误或设置默认值 } ``` #### 2. 使用`Int32.TryParse`替代 此方法安全返回转换状态,避免异常: ```csharp string input = GetInput(); if (int.TryParse(input, out int number)) { // 转换成功 } else { Console.WriteLine($"无效的整数格式: {input}"); } ``` #### 3. 数据清洗预处理 若输入可能包含隐藏字符(如空格、换行符): ```csharp string cleanedInput = input.Trim().Replace(" ", ""); // 去除首尾空格和中间空格 ``` #### 4. 异常捕获与日志录 添加`try-catch`块定位问题源头: ```csharp try { int number = int.Parse(input); } catch (FormatException ex) { File.WriteAllText("error.log", $"输入值'{input}'格式错误,详情: {ex.StackTrace}"); // 可选择重启服务或降级处理 } ``` #### 5. 检查区域性设置(Culture) 若输入包含区域性特定符号(如千分位分隔符`1,234`): ```csharp int number = int.Parse(input, CultureInfo.InvariantCulture); // 强制使用无区域性格式 ``` --- ### 关键排查点 1. **输入来源**:检查数据来自文件、网络还是用户输入,需确保传输过程无数据损坏 2. **日志分析**:通过崩溃日志确认具体触发异常的代码位置 3. **单元测试**:构造包含特殊字符、空值的测试用例验证鲁棒性 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值