2016-06-02 8 views
0

Ich habe einen Windows-Dienst, der die Standardkonfiguration (servicename.exe.config) verwendet..NET Windows-Dienst - service.exe.config Probleme

Das sieht wie folgt aus:

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

Wenn der Windows-Dienst gestartet wird, erhalte ich einen Fehler, der besagt, dass:

Abschnitts- oder Gruppenname ‚appSettings‘ bereits definiert ist. Updates auf Dies kann nur auf der Konfigurationsebene auftreten, wo es definiert ist. (C: \ root \ lib \ svc \ nwatchd \ nwatchd.exe.Config Linie 6)

Also, ich voran gehen und entfernen Sie die beiden folgenden von der config (da sie beide Wurffehler):

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

Jetzt bekomme ich die folgende Ausnahme:

Konnte nicht das Objekt des Typs 'System.Configuration.DefaultSection' zu Typ 'System.Configuration.AppSettingsSection' werfen.

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

Antwort

1

Ändern von:

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

An:

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

die Ausgabe der folgenden Ausnahme Entschlossen:

Das Objekt des Typs 'System.Configuration.DefaultSection' konnte nicht in Typ 'System.Configuration.AppSettingsSection' umgewandelt werden.

1

es so versuchen:

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

Sie brauchen nicht Standard appSettings und connection in Config-Abschnitte angeben. Sie sind standardmäßig eingerichtet.

Dann Zugriffskonfiguration auf diese Weise:

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

Ich habe bereits genau das getan, was Sie oben erwähnt haben. Die zweite Ausnahme oben ist, was geworfen wird. – blgrnboy

+0

Nein, Sie haben gesagt, dass Sie den Abschnitt EntityFramework entfernt haben. – Fanda