我安装了一个 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>