[英]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.