0

我安装了一个 Windows 服务,它必须通过以下方法从 App.Config 文件中获取一些字符串OnStart-ConfigurationManager.AppSettings["stringName"];
在 App.Config 中,我还source为用于写入日志文件的日志文件定义了一个。

但是,当我成功安装我的服务并尝试从Serivce Control Explorer内部启动它时My Computer -> Manage - > Device Manager,我收到一条消息说

The service on the local computer and then stopped. Some services stop automatically if not used by toher applications

在 Windows 事件查看器中,我收到以下错误详细信息(可能在启动服务时引发了异常)

这是相同的错误详细信息:

Service cannot be started. System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section add. (C:\Program Files (x86)\Default Company Name\ServiceSetup\ServiceChecker.exe.Config line 3)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Dia...

以上详细信息显示了与服务文件相关的错误详细信息.exe.config。我已尝试卸载并重新安装该服务,并且该服务的安装帐户设置为Local system具有广泛的系统权限。

我在项目中的 App.Config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
<appSettings>
      <add key="UtilName" value="sample.exe"/>
      <add key="App1" value="MyApp"/>
      <add key="App1E" value="MyApp.exe"/>
      <add key="App2" value="MyApp2"/>
      <add key="App2E" value="MyApp2.exe" />
      <add key="AppDirectory" value="Company\MyProject" />
      <add key="CMDArgs" value="\start"/>
</appSettings>    
      <system.diagnostics>
        <sources>
          <source name="ServiceTrace" switchName="ServiceTraceSwitch" switchType="System.Diagnostics.SourceSwitch">
            <listeners>
              <add name="ServiceLog" type="System.Diagnostics.TextWriterTraceListener" initializeData="servicetrace.log"/>
            </listeners>
          </source>
        </sources>
        <switches>
          <add name="ServiceTraceSwitch" value="Information" />
        </switches>
        <trace autoflush="true" indentsize="4"></trace>
      </system.diagnostics>

    </configuration>
4

2 回答 2

3

似乎您的 app.config(或 .exe.config)包含无法识别的部分。错误消息通常会说类似

无法识别的配置部分“部分名称”

所以我假设您只是添加了配置值而没有所需的父元素。该元素必须作为该部分的子元素添加。确保它看起来像这样:

<configuration>
   <appSettings>
     <add key="MyKey" value="MyValue" />
   </appSettings>
   ...some more configuration...
</configuration>
于 2012-06-04T09:59:07.557 回答
0

根据错误,您的 .config 文件中有语法错误:

Unrecognized configuration section add.

这表明您可能错过了配置文件中的外部部分。应该看起来像:

<section>
    <add ... >
</section>
于 2012-06-04T09:59:59.973 回答