2012-06-04 46 views
0

我安装在Windows serivce其获取具有通过OnStart下面的方法来从app.config文件中某些字符串 - ConfigurationManager.AppSettings["stringName"];
里面的应用程序。配置中,我还为我用于写入日志文件的日志文件定义了一个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> 
+0

请发布您的配置,似乎错误在那里。 –

+0

@Adriano:添加了配置文件文本 – user1240679

回答

3

看来你的app.config(或.exe.config)包含无法识别的部分。该错误消息通常说类似于

Unrecognized configuration section 'section name'

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

<configuration> 
    <appSettings> 
    <add key="MyKey" value="MyValue" /> 
    </appSettings> 
    ...some more configuration... 
</configuration> 
+0

在'Windows服务'项目中,如果添加了app.config项目,默认情况下,此部分中没有'AppSettings'部分。 Perhpas可能是问题。检查 – user1240679

+0

我添加了之前丢失的'appSettings'。试图卸载并重新安装服务,但没有运气。再次在eveng查看器中再次获得相同的错误。编辑帖子以在exe.Config中显示'appSettings部分以及 – user1240679

+0

卸载并重新安装不会改变任何内容。你确定exe文件使用的.config文件是你编辑的文件吗? –

0

你在你的config文件有一个语法错误,每个错误:

Unrecognized configuration section add. 

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

<section> 
    <add ... > 
</section> 
+0

在文章 – user1240679

+0

中增加了'app.Config'文件,你确定你得到的错误是完全一样的吗? – SteveM

相关问题