Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET Windows服务-Service.exe.config问题_C#_Configurationmanager - Fatal编程技术网

C# .NET Windows服务-Service.exe.config问题

C# .NET Windows服务-Service.exe.config问题,c#,configurationmanager,C#,Configurationmanager,我有一个使用默认配置(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/fwli

我有一个使用默认配置(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>
<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>
试着这样做:

<?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>
<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>
试着这样做:

<?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>
<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>
修改自:

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

解决了以下异常的问题:

无法将“System.Configuration.DefaultSection”类型的对象强制转换为 键入“System.Configuration.AppSettingsSection”

修改自:

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

解决了以下异常的问题:

无法将“System.Configuration.DefaultSection”类型的对象强制转换为 键入“System.Configuration.AppSettingsSection”


我已经完全按照你上面提到的做了。上面的第二个异常是抛出的内容。我已经完全按照您上面提到的做了。上面的第二个异常是抛出的内容。