SL - 动态设置Web服务的EndPoint(Address)

public class ServiceFactory
    {
        private static ServiceFactory instance;

        public static ServiceFactory Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ServiceFactory();
                }
                return instance;
            }
        }

        private string serviceUrl;

        public string ServiceUrl
        {
            get { return serviceUrl; }
            set { serviceUrl = value; }
        }

        public void SetServiceUrl(string rootUrl, string svrUrl)
        {
            string t_url = "";
            if (!string.IsNullOrEmpty(svrUrl) && !string.IsNullOrEmpty(rootUrl))
            {

                while (rootUrl.EndsWith("/") || rootUrl.EndsWith("\\"))
                {
                    rootUrl = rootUrl.Remove(rootUrl.Length - 1, 1);
                }

                if (svrUrl.StartsWith("~"))
                {
                    t_url = svrUrl.Replace("~", rootUrl);
                }
                else
                {
                    t_url = svrUrl;
                }
            }
            this.serviceUrl = t_url;
        }
       
        public void SetSoapClientServiceUrl(object client)
        {
            if (!string.IsNullOrEmpty(serviceUrl))
            {
                var endpoint = client.GetType().GetProperty("Endpoint").GetValue(client, null);
                endpoint.GetType().GetProperty("Address").SetValue(endpoint, new System.ServiceModel.EndpointAddress(serviceUrl), null);
            }
        }
    }


<script runat="server">
    protected string rootUrl;

    protected void Page_Load(object sender, EventArgs e)
    {
        string str = "http://" + Request.Url.Host + ":" + Request.Url.Port + Request.ApplicationPath;
        while (str.EndsWith("/"))
        {
            str = str.Remove(str.Length - 1);
        }
        this.rootUrl = str;
    }
</script>


<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
            width="100%" height="100%">
            <param name="source" value="<%=this.rootUrl %>/ClientBin/AdmUserMgr.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="rootUrl=<%=this.rootUrl %>,svrUrl=~/WebServices/AdmUserMgr.asmx" />
            <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="Get Microsoft Silverlight"
                    style="border-style: none" />
            </a>
        </object>
阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

icewizardry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值