Silverlight中WCF的动态部署

本文详细介绍了在Web.config中配置WCF服务地址,Silverlight客户端通过参数调用并集成使用该服务的方法,以及在Silverlight客户端应用中如何获取并使用配置信息。同时展示了如何在服务端统一调用时获取并应用配置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
            }
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值