设为首页 收藏本站
查看: 980|回复: 0

[经验分享] Silverlight与WCF通信(一) :Silverlight通过httpBinding访问IIS宿主WCF

[复制链接]

尚未签到

发表于 2015-8-12 11:41:31 | 显示全部楼层 |阅读模式
  首语
  本人在学习Silverlight WCF的时候,各种问题层出不穷,在园子里面查阅大量的资料,尤其是ArtechFrank xu Lei WCF博文给我很大的帮助,在此衷心感谢两位。本人不才,特写下几个SilverlightWCF通信的例子与大家分享和交流,希望初学者少走些弯路,而专心于系统的业务层的开发,高手请绕行,但欢迎拍砖!:)
  
  本系列是面向SilverlightWCF交互初学者的,主要包括:
  Silverlight 通过httpBinding方式访问IIS 宿主WCF
  Silverlight通过netTcpBinding方式访问IIS 宿主WCF
  Silverlight通过netTcpBinding方式访问IIS 宿主WCF(全双工)
  Silverlight通过httpBinding方式访问控制台宿主WCF
  Silverlight通过netTcpBinding方式访问控制台宿主WCF
  
  Silverlight通过httpBiding方式访问IIS宿主WCF是我们在Silverlight与WCF通信中最为常见的,也是用的最多的,我们用个很简单的例子进行演示。
  项目结构:
DSC0000.jpg
  项目目结构简单说明:

程序集名称需添加的引用简要说明
LxContractsSystem.Runtime.Serialization System.ServiceModel用于存放操作契约与数据契约
LxServicesLxContracts[项目]服务,操作契约的实现
WcfHost.webLxContracts[项目] 和LxServices[项目]利用Svc文件发布服务的站点
SilverlightDemo Silverlight程序,调用WCF服务
  





  注意:建立Silverlight程序的时候,不需要承载网站,建立一个单一的Silverlight程序即可,这样做的原因是,把Silverlight和WCF服务不放到同一个站点下面,是为了演示跨域的问题。
  代码实现:
  类库LxContracts:(包括数据契约Student.cs和操作契约IStudent.cs)


DSC0001.gif DSC0002.gif 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
{
/// <summary>
/// 学生编号
/// </summary>
        [DataMember]
public int StuId { get; set; }
/// <summary>
/// 学生姓名
/// </summary>
        [DataMember]
public string StuName { get; set; }
/// <summary>
/// 所在班级
/// </summary>
        [DataMember]
public string ClassName { get; set; }
/// <summary>
/// 联系电话
/// </summary>
        [DataMember]
public string TelPhoneNum { get; set; }
}
}

IStudent.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<Student> 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<Student>
{
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<Student> GetStudent()
{
//实际情况应该为从数据库读取
//本例手动生成一个StudentList
StudentList ListStuent = new StudentList();
return ListStuent;
}
}
}
  站点WcfHost.web
  站点WcfHost.web,这是一个Asp.net 空web应用程序。
  1、右击” WcfHost.web”—“添加”—“新建项”—“wcf服务”,命名为”StudentSrv.svc” 。如图:
DSC0003.jpg
在项目中删除”StudentSrv.svc.cs”文件和”IStudentSrv.cs”文件。右击”StudentSrv.svc”文件,选择”查看标记”,将代码修改为:




<%@ ServiceHost Language="C#" Service="LxServices.StudentService" %>
  2、修改webconfig 文件,代码如下:


WebConfig


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="LxBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="LxServices.StudentService" behaviorConfiguration="LxBehavior">
<endpoint address="" binding="basicHttpBinding" contract="LxContracts.IStudent" />
</service>
</services>
<!--关闭 ASP.NET 兼容性模式-->
<serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>
</system.serviceModel>
</configuration>
  注意:endpoint中的address 为空:因为svc文件的地址就是元数据发布的地址。
  3、右击”StudentSrv.svc”文件,在”浏览器中查看”,显示如下图,说明服务已经部署好了,我用的端口是 9090:
DSC0004.jpg
  在Silverlight中进行调用:
  Silverlight调用wcf很简单,直接在”SilverlightDemo”中添加”服务引用即可”,Silverlight项目中会自动生成” ServiceReferences.ClientConfig”配置文件,当然也可以利用代码的方式调用,但是我比较懒 :)。
  1、为Silverlight程序添加WCF:
   “右击”—“SiverlightDemo”—“添加服务引用”—“输入服务地址”(我的是http://localhost:9090/StudentSrv.svc)--点击“前往”,就会找到服务,命名为“WCF.StudentSrv”后,点击“确定”
DSC0005.jpg
  2、在Silverlight中调用WCF:
  MainPage.xaml中添加”DataGrid”控件,xaml代码如下:


MainPage.xaml 代码


<sdk:DataGrid x:Name="dgStudnet" Grid.Row="0" AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="学生编号" Width="80" Binding="{Binding StuId}" />
<sdk:DataGridTextColumn Header="学生姓名" Width="100" Binding="{Binding StuName}" />
<sdk:DataGridTextColumn Header="所在班级" Width="120" Binding="{Binding ClassName}" />
<sdk:DataGridTextColumn Header="电话号码" Width="100" Binding="{Binding TelPhoneNum}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>

MainPage.cs 代码


public partial class MainPage : UserControl
{
ObservableCollection<Student> listStudent;
public MainPage()
{
InitializeComponent();
listStudent = new ObservableCollection<Student>();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
StudentClient proxyClient = new StudentClient();
proxyClient.GetStudentAsync();
proxyClient.GetStudentCompleted += new EventHandler<GetStudentCompletedEventArgs>(proxyClient_GetStudentCompleted);
}
void proxyClient_GetStudentCompleted(object sender, GetStudentCompletedEventArgs e)
{
if (e.Error == null)
{
listStudent = e.Result;
this.dgStudnet.ItemsSource = listStudent;
}
}
}
   运行结果:
  将” SilverlightDemo”设置为启动项目,运行,会产生下面的异常:
DSC0006.jpg
  这就是因为当时建立项目的时候没有把Silverlight程序和WCF服务放到同一个站点的缘故,因此需要在发布WCF的网站根目录放置一个跨域文件:clientaccesspolicy.xml


clientaccesspolicy.xml


<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
  再次运行,结果如下图所示:
DSC0007.jpg
  至此,Silverlight通过httbBingding方式访问IIS宿主的WCF的演示我们就进行到这里。
  下一篇我们将演示一下 Silverlight通过netTcpBinding方式访问IIS宿主的WCF的环境配置和应该注意的问题。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-97889-1-1.html 上篇帖子: Silverlight与WCF通信(二) :Silverlight通过netTcpBinding访问IIS宿主WCF 下篇帖子: 微软的坑:Url重写竟然会引起IIS内核模式缓存不工作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表