您好,欢迎访问一九零五行业门户网

Silverlight通过httpBinding访问IIS宿主WCF

silverlight和wcf通信是大家开发中用得相对较多的东西,我以silverlight 通过 httpbinding 访问 iis 宿主 wcf 来简单介绍一下。 silverlight 通过 httpbiding方式 访问 iis 宿主 wcf是我们在silverlight与wcf通信中最为常见的,也是用的最多的,我们用个很简
silverlight和wcf通信是大家开发中用得相对较多的东西,我以silverlight通过httpbinding访问iis宿主wcf 来简单介绍一下。
silverlight通过httpbiding方式访问iis宿主wcf是我们在silverlight与wcf通信中最为常见的,也是用的最多的,我们用个很简单的例子进行演示。
项目结构:
项目目结构简单说明:
程序集名称 需添加的引用 简要说明
lxcontracts system.runtime.serialization system.servicemodel 用于存放操作契约与数据契约
lxservices lxcontracts[项目] 服务,操作契约的实现
wcfhost.web lxcontracts[项目] 和lxservices[项目] 利用svc文件发布服务的站点
silverlightdemo   silverlight程序,调用wcf服务
注意:建立silverlight程序的时候,不需要承载网站,建立一个单一的silverlight程序即可,这样做的原因是,把silverlight和wcf服务不放到同一个站点下面,是为了演示跨域的问题。
代码实现:
类库lxcontracts:(包括数据契约student.cs和操作契约istudent.cs)
student.cs 代码








代码如下 复制代码
using system;using system.collections.generic;using system.linq;using system.text;using system.servicemodel;using system.runtime.serialization;namespace lxcontracts{ [datacontract] public class student { /// /// 学生编号 /// [datamember] public int stuid { get; set; } /// /// 学生姓名 /// [datamember] public string stuname { get; set; } /// /// 所在班级 /// [datamember] public string classname { get; set; } /// /// 联系电话 /// [datamember] public string telphonenum { get; set; } }}
student.cs 代码








代码如下 复制代码
using system;using system.collections.generic;using system.linq;using system.text;using system.runtime.serialization;using system.servicemodel;namespace lxcontracts{ [servicecontract] public interface istudent { [operationcontract] list getstudent(); }}
类库lxservices:( 改类库包括一个模仿获取数据库集合类studentlist.cs和服务类studentservice.cs)studentlist.cs








代码如下 复制代码
using system;using system.collections.generic;using system.linq;using system.text;using lxcontracts;namespace lxservices{ public class studentlist:list { public studentlist() { this.add(new student() { stuid = 1, stuname = 小明, classname = 计算机一班, telphonenum = 123456 }); this.add(new student() { stuid = 2, stuname = 小红, classname = 计算机二班, telphonenum = 234567 }); this.add(new student() { stuid = 2, stuname = 小兰, classname = 计算机三班, telphonenum = 890123 }); } }}
studentservice 代码








代码如下 复制代码
using system;using system.collections.generic;using system.linq;using system.text;using lxcontracts;namespace lxservices{ public class studentservice:istudent { public list getstudent() { //实际情况应该为从数据库读取 //本例手动生成一个studentlist studentlist liststuent = new studentlist(); return liststuent; } }}
站点wcfhost.web站点wcfhost.web,这是一个asp.net 空web应用程序。
1、右击” wcfhost.web”—“添加”—“新建项”—“wcf服务”,命名为”studentsrv.svc” 。如图:
在项目中删除”studentsrv.svc.cs”文件和”istudentsrv.cs”文件。右击”studentsrv.svc”文件,选择”查看标记”,将代码修改为:








2、修改webconfig 文件,代码如下:
webconfig








代码如下 复制代码

注意:endpoint中的address 为空:因为svc文件的地址就是元数据发布的地址。3、右击”studentsrv.svc”文件,在”浏览器中查看”,显示如下图,说明服务已经部署好了,我用的端口是 9090:
在silverlight中进行调用:
silverlight调用wcf很简单,直接在”silverlightdemo”中添加”服务引用即可”,silverlight项目中会自动生成” servicereferences.clientconfig”配置文件,当然也可以利用代码的方式调用,但是我比较懒 :)。
1、为silverlight程序添加wcf:
   “右击”—“siverlightdemo”—“添加服务引用”—“输入服务地址”(我的是http://localhost:9090/wcf/studentsrv.svc)--点击“前往”,就会找到服务,命名为“wcf.studentsrv”后,点击“确定”
2、在silverlight中调用wcf:
mainpage.xaml中添加”datagrid”控件,xaml代码如下:
mainpage.xaml 代码








代码如下 复制代码

mainpage.cs 代码








代码如下 复制代码
public partial class mainpage : usercontrol { observablecollection liststudent; public mainpage() { initializecomponent(); liststudent = new observablecollection(); this.loaded += new routedeventhandler(mainpage_loaded); } void mainpage_loaded(object sender, routedeventargs e) { studentclient proxyclient = new studentclient(); proxyclient.getstudentasync(); proxyclient.getstudentcompleted += new eventhandler(proxyclient_getstudentcompleted); } void proxyclient_getstudentcompleted(object sender, getstudentcompletedeventargs e) { if (e.error == null) { liststudent = e.result; this.dgstudnet.itemssource = liststudent; } } }
运行结果:将” silverlightdemo”设置为启动项目,运行,会产生下面的异常:
这就是因为当时建立项目的时候没有把silverlight程序和wcf服务放到同一个站点的缘故,因此需要在发布wcf的网站根目录放置一个跨域文件:clientaccesspolicy.xml
clientaccesspolicy.xml

再次运行,结果如下图所示:
至此,silverlight通过httbbingding方式访问iis宿主的wcf的演示我们就进行到这里
其它类似信息

推荐信息