首页 技术 正文
技术 2022年11月15日
0 收藏 991 点赞 4,072 浏览 1853 个字

做过Silverlight项目的朋友都知道一般来说我们在Silverlight项目中都需要引用WebService或是WCF,引用的方式是在Visual Studio窗口中通过“添加服务引用”来添加引用,这时Visual Studio会生成一个 ServiceReferences.ClientConfig 配置文件,这个文件中存储了WebService的地址,绑定等信息,我们在Silverlight中实例化WebService的代理类时需要用到这个文件,没有这个文件WebService代理类的实例化就会失败。

使用ServiceReferences.ClientConfig的方式来实例化WebService代理类比较简单,但是也有一个缺点,就是每次将Silverlight项目上线时都需要手工打开生成的XAF包,将ServiceReferences.ClientConfig文件中的WebService地址从http://Localhost/YourProject/YourWebService.asmx的形式改为http://YourDomain/YourProject/YourWebService.asmx,如果某次你忘记了修改,可能会导致你的Siverlight项目在线上无法正常运行。那么有没有一种简单的方式让我们不需要每次上线都修改ServiceReferences.ClientConfig文件,让我们摆脱对ServiceReferences.ClientConfig的信赖呢?下面就说一下我的实现方式。

实现原理很简单,就是在一个类里手工的构造WebService的EndPoint, Binding等信息,将ServiceReferences.ClientConfig的作用用代码来代替,示例实现代码如下,代码很简单,各位一看就明白。

///<summary>
/// 得到WebService对象
/// 以后实例化WebService代理时请用var serviceClient = Utility.GetDesignerServiceInstance();的形式,
/// 不要再用默认的 var serviceClient = new WSDesignerSoapClient();
/// 后一种形式会报错,因为我们删除了ServiceReferences.ClientConfig文件
///</summary>
///<returns></returns>
public static WSDesignerSoapClient GetDesignerServiceInstance()
{
var basicBinding = new BasicHttpBinding() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue, Name = "WSDesignerSoap" };
basicBinding.Security.Mode = BasicHttpSecurityMode.None;
var endPoint = new EndpointAddress(getHostUrl() + "/WebService.asmx");
var ctor =
typeof (WSDesignerSoapClient).GetConstructor(new Type[] {typeof (Binding), typeof (EndpointAddress)});
return (WSDesignerSoapClient) ctor.Invoke(new object[] {basicBinding, endPoint});
} ///<summary>
/// 得到当前所在网站的根目录,如Http://localhost/flow
/// 注意站点名字必须是Flow,否则会报错。
///</summary>
///<returns></returns>
private static string getHostUrl()
{
var location = (HtmlPage.Window.GetProperty("location")) as ScriptObject;
var hrefObject = location.GetProperty("href");
string url = hrefObject.ToString().Substring(, hrefObject.ToString().IndexOf("Flow/") + );
return url;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902