遇到程序代码调试时可以正常读取web.config数据库连接串
System.Configuration.ConfigurationManager.ConnectionStrings["PigeonConnectionString"].ToString()
但部署到IIS上,却数据库连接失败,经过写日志跟踪发现读取的连接串为$(ReplacableToken_PigeonConnectionString-Web.config Connection String_0)
解决方法1
需要修改项目文件,注意是项目文件(即:D:\VS2010Pro\Pigeon.Web\Pigeon.Web.csproj
在PropertyGroup里添加如下属性:
<AutoParameterizationWebConfigConnectionStrings>False</AutoParameterizationWebConfigConnectionStrings>
这样就会正常生成自定义的连接串
以下是截取的片段
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AutoParameterizationWebConfigConnectionStrings>False</AutoParameterizationWebConfigConnectionStrings>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
另外一种Silverlight读取网站项目中的Web.config数据库连接串的方法(VS2010)
添加一个Silverlight项目,会生成两个工程,(一个Silvelight项目,一个网站项目)
1. 在网站项目的PA_QCReportTestPage.html和PA_QCReportTestPage.aspx中加入红色字体的内容
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/PA_QCReport.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50826.0" />
<param name="autoUpgrade" value="true" />
<param name="InitParams" value='WcfServiceAddress=<%= System.Configuration.ConfigurationManager.AppSettings["WcfServiceAddress"] %>' />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="获取 Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>
2.在Silverlight项目的App.xaml.cs中加入红色字体的内容
private void Application_Startup(object sender, StartupEventArgs e)
{
var slServicePath = e.InitParams["WcfServiceAddress"];
//将读取到的WCF地址保存到资源中。
Application.Current.Resources.Add("WcfServiceAddress", slServicePath);
this.RootVisual = new MainPage();
}
//调用方法
servicePicture.Endpoint.Address =
new System.ServiceModel.EndpointAddress(Application.Current.Resources["WcfServiceAddress"] + "/wcfMain.svc");