1.在web.config中定义
<appSettings>
<add key="WcfServiceAddress" value="http://localhost:820"/>
</appSettings>
web.config文件内容参考:
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="WcfServiceAddress" value="http://localhost:820"/>
</appSettings>
<connectionStrings>
<add name="RptPath" connectionString="\\192.128.58.248\pabo$\Job\Report"/>
<add name="DBServerIP" connectionString="192.128.58.248"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
***********************************************************************************************************
2.在调用Silverlight的aspx和html中添加
<param name="InitParams" value='WcfServiceAddress=<%= System.Configuration.ConfigurationManager.AppSettings["WcfServiceAddress"] %>' />
代码参考
<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>
***********************************************************************************************************
3.在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();
}
***********************************************************************************************************
4.在Servermanger.cs统一调用处使用如下代码
注意:主要是Application.Current.Resources["WcfServiceAddress"]
internal static wcfMain.IwcfMainClient GetPox()
{
try
{
if (servicePicture.State == System.ServiceModel.CommunicationState.Created)
{
servicePicture.Endpoint.Address =
new System.ServiceModel.EndpointAddress(Application.Current.Resources["WcfServiceAddress"] + "/wcfMain.svc");
((IContextChannel)servicePicture.InnerChannel).OperationTimeout = new TimeSpan(1, 0, 240);
return servicePicture;
}
else
{
return servicePicture;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return null;
}
}