簡體   English   中英

WCF服務無法看到Web.config文件

[英]WCF Service cannot see Web.config file

一個新手問題,因為這是我的第一個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> 

部署后將創建以下結構:

DBService(文件夾)包含SPService.svc。 它還包含一個
包含DBService.dll和DBService.pdb的bin(文件夾)

在以上結構中,bin文件夾位於DBService文件夾內:-)

它還在bin文件夾中創建一個DBService.dll.config文件。 我已將其復制到父文件夾,也將其復制並重命名為Web.config並將其放置在兩個文件夾中。 我也嘗試過將Web.config重命名為web.config。

我已經使程序將設置打印到“事件查看器”中,並且可以看到它們沒有被填充。

沒關系,我仍然無法從配置文件中獲取設置。 配置文件需要調用什么,應該在哪里? 謝謝!

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

ConfigurationManager用於Windows應用程序的app.config文件, WebConfigurationManager用於Web應用程序及其web.config。

像這樣使用它:

setting = WebConfigurationManager.AppSettings["DatabaseServer"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM