WCF服务无法看到Web.配置文件

本文关键字:Web 配置文件 服务 WCF | 更新日期: 2023-09-27 18:08:51

这是一个新手问题,因为这是我的第一个WCF服务:在开发环境中运行的服务可以看到Web。配置文件。

当它被部署到服务器上的IIS时,它无法看到这个文件。

访问这个文件的类是:

使用System.Configuration;

namespace DBService
{
public static class clsSettings
{
    public static string DBServer {get; set;}
    public static string DB { get; set; }
    public static string DBUsername { get; set; }
    public static string DBPassword { get; set; }
    public static Boolean VerboseLogging { get; set; }
    public static void LoadSettings()
    {
        string setting;
        try
        {
            clsSettings.DBUsername = "Login";
            clsSettings.DBPassword = "Password";
            setting = ConfigurationManager.AppSettings["DatabaseServer"];
            if (setting != null) clsSettings.DBServer = setting;
            setting = ConfigurationManager.AppSettings["Database"];
            if (setting != null) clsSettings.DB = setting;
            setting = ConfigurationManager.AppSettings["VerboseLogging"];
            if (setting.ToUpper() == "TRUE") 
            {
                clsSettings.VerboseLogging = true;
            }
            else
            {
                clsSettings.VerboseLogging = false;
            }
        }
        catch (Exception ex)
        {
            clsError.LogError("clsSettings", "LoadSettings", ex.Message);
        }
    }
}
}

和网络。配置文件包含:

<?xml version="1.0"?>
<configuration>
  <appSettings>
      <add key="DatabaseServer" value="Server1'SQL2012"/>
      <add key="Database" value="Licensing"/>
      <add key="VerboseLogging" value="True"/>
    </appSettings>
  <system.web>
  <compilation debug="true" targetFramework="4.0"/>
  <httpRuntime/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before
deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

部署后创建如下结构:

包含SPService.svc的DBService(文件夹)。它还包含一个

DBService.dll/DBService.pdb目录

在上面的结构中,bin文件夹位于DBService文件夹内:-)

它还在bin文件夹中创建一个DBService.dll.config文件。我已经将其复制到父文件夹,并将其复制并重命名为Web。配置并将其放在两个文件夹中。我也试过重命名Web。配置到web.config.

我已经让程序打印设置到事件查看器,可以看到它们没有填充。

无论我做什么,它仍然不从配置文件中拾取设置。配置文件需要调用什么,应该在哪里调用?谢谢!

WCF服务无法看到Web.配置文件

您必须使用WebConfigurationManager.AppSettings而不是ConfigurationManager.AppSettings

ConfigurationManager为Windows应用程序的app.config文件,WebConfigurationManager为web应用程序及其web.config文件。

像这样使用:

setting = WebConfigurationManager.AppSettings["DatabaseServer"];