blgrnboy
blgrnboy

Reputation: 5157

.NET Windows Service - service.exe.config issues

I have a Windows service that uses the default config (servicename.exe.config).

The looks like this:

<?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>

When the Windows service is started, I get an error that states that:

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)

So, I go ahead and remove both the following from the config (since they both throw errors):

<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"/>

Now I get the following exception:

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();
}

Upvotes: 0

Views: 696

Answers (2)

blgrnboy
blgrnboy

Reputation: 5157

Modifying from:

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

To:

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();
}

Resolved the issue of the following Exception:

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

Upvotes: 1

Fanda
Fanda

Reputation: 3786

Try it like this:

<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>

You don't need to specify standard appSettings and connectionStrings in config sections. They are set up by default.

Then access configuration this way:

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

Upvotes: 1

Related Questions