简体   繁体   中英

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.

When it is deployed to IIS on a server it cannot see this file.

The class accessing this file is:

using 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:

 <?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. It also contains a
bin (folder) containing DBService.dll and DBService.pdb

In the above structure, the bin folder is inside the DBService folder :-)

It also creates a DBService.dll.config file in the bin folder. I have copied this to the parent folder, and also copied and renamed it to Web.config and placed this in both folders. I've also tried renaming Web.config to 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 .

ConfigurationManager is for app.config files of Windows apps, WebConfigurationManager is for web applications and their web.config.

Use it like this:

setting = WebConfigurationManager.AppSettings["DatabaseServer"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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