WCF服务无法看到Web.config文件

时间:2014-06-17 09:00:10

标签: c# wcf

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

当它部署到服务器上的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);
        }
    }
}
}

Web.config文件包含:

<?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(文件夹)。它还包含一个 bin(文件夹)包含DBService.dll和DBService.pdb

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

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

我已让程序将设置打印到事件查看器,并且可以看到它们未填充。

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

1 个答案:

答案 0 :(得分:0)

您必须使用WebConfigurationManager.AppSettings代替ConfigurationManager.AppSettings

ConfigurationManager适用于Windows应用的app.config文件,WebConfigurationManager适用于Web应用及其web.config。

像这样使用:

setting = WebConfigurationManager.AppSettings["DatabaseServer"];