.NET Windows 服务 - service.exe.config 问题

.NET Windows Service - service.exe.config issues

我有一个使用默认配置 (servicename.exe.config) 的 Windows 服务。

看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="appSettings" type="System.Configuration.AppSettingsSection"/>
    <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection"/>
  </configSections>
  <connectionStrings>
    <!-- Omitted -->
  </connectionStrings>
  <appSettings>
    <!-- Omitted -->
  </appSettings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <contexts>
      <context type="NWatch.NWatchDbContext, NWatch.Entities">
        <databaseInitializer type="NWatch.NWatchDbContextInitializer, NWatch.Entities" />
      </context>
    </contexts>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

启动 Windows 服务时,我收到一条错误消息:

Section or group name 'appSettings' is already defined. Updates to this may only occur at the configuration level where it is defined. (C:\root\lib\svc\nwatchd\nwatchd.exe.Config line 6)

所以,我继续从配置中删除以下两个(因为它们都抛出错误):

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="appSettings" type="System.Configuration.AppSettingsSection"/>

现在我得到以下异常:

Unable to cast object of type 'System.Configuration.DefaultSection' to type 'System.Configuration.AppSettingsSection'.

public NWatchSystemConfiguration(string fileName)
{
    var fileMap = new ConfigurationFileMap(fileName);
    var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
    Config = configuration;
    AppSettingsSection AppSettings = configuration.AppSettings;  // Exception occurs here
    ConnectionStrings = configuration.ConnectionStrings;
    Initialize();
}

这样试试:

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>

您不需要在配置部分指定标准的 appSettings 和 connectionStrings。它们是默认设置的。

然后这样访问配置:

 var value = ConfigurationManager.AppSettings["reguiredValueKey"];

修改自:

public NWatchSystemConfiguration(string fileName)
{
    var fileMap = new ConfigurationFileMap(fileName);
    var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
    Config = configuration;
    AppSettingsSection AppSettings = configuration.AppSettings;
    ConnectionStrings = configuration.ConnectionStrings;
    Initialize();
}

收件人:

public NWatchSystemConfiguration(string fileName)
{
    var fileMap = new ExeConfigurationFileMap();
    fileMap.ExeConfigFilename = fileName;
    var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    Config = configuration;
    AppSettingsSection AppSettings = configuration.AppSettings;
    ConnectionStrings = configuration.ConnectionStrings;
    Initialize();
}

解决了以下异常问题:

Unable to cast object of type 'System.Configuration.DefaultSection' to type 'System.Configuration.AppSettingsSection'.