简体   繁体   English

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

[英]WCF Service cannot see Web.config file

A bit of a newbie question as this is my first WCF service: Running in the development environment the service can see the Web.config file. 一个新手问题,因为这是我的第一个WCF服务:在开发环境中运行,该服务可以看到Web.config文件。

When it is deployed to IIS on a server it cannot see this file. 将其部署到服务器上的IIS时,它将看不到此文件。

The class accessing this file is: 访问此文件的类为:

using System.Configuration; 使用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);
        }
    }
}
}

And the Web.config file contains: 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> 

Deployed it creates the structure: 部署后将创建以下结构:

DBService (folder) containg SPService.svc. DBService(文件夹)包含SPService.svc。 It also contains a 它还包含一个
bin (folder) containing DBService.dll and DBService.pdb 包含DBService.dll和DBService.pdb的bin(文件夹)

In the above structure, the bin folder is inside the DBService folder :-) 在以上结构中,bin文件夹位于DBService文件夹内:-)

It also creates a DBService.dll.config file in the bin folder. 它还在bin文件夹中创建一个DBService.dll.config文件。 I have copied this to the parent folder, and also copied and renamed it to Web.config and placed this in both folders. 我已将其复制到父文件夹,也将其复制并重命名为Web.config并将其放置在两个文件夹中。 I've also tried renaming Web.config to web.config. 我也尝试过将Web.config重命名为web.config。

I have made the program print the settings to the Event Viewer and can see they aren't populated. 我已经使程序将设置打印到“事件查看器”中,并且可以看到它们没有被填充。

No Matter what I do it still doesn't pick up the settings from the config file. 没关系,我仍然无法从配置文件中获取设置。 What does the config file need to be called and where should it be? 配置文件需要调用什么,应该在哪里? Thanks! 谢谢!

You have to use WebConfigurationManager.AppSettings instead of ConfigurationManager.AppSettings . 您必须使用WebConfigurationManager.AppSettings而不是ConfigurationManager.AppSettings

ConfigurationManager is for app.config files of Windows apps, WebConfigurationManager is for web applications and their web.config. ConfigurationManager用于Windows应用程序的app.config文件, WebConfigurationManager用于Web应用程序及其web.config。

Use it like this: 像这样使用它:

setting = WebConfigurationManager.AppSettings["DatabaseServer"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM