Silverlight与WCF通信(四) :Silverlight访问控制台宿主WCF(非svc方式)

Silverlight与WCF通信(四) :Silverlight访问控制台宿主WCF(非svc方式),第1张

概述转载出处:http://www.cnblogs.com/lxblog/archive/2012/08/02/2620393.html     今日更新本系列最后一篇,Silverlight访问控制台宿主WCF,我们分别介绍tcp信道和http信道两种方式。无论用IIS宿主还是控制台等程序宿主,对于WCF服务代码没有任何区别,但由于是Silverlight来进行调用,就会面临一个跨域的问题,我们今天 转载出处:http://www.cnblogs.com/lxblog/archive/2012/08/02/2620393.html

    今日更新本系列最后一篇,Silverlight访问控制台宿主WCF,我们分别介绍tcp信道和http信道两种方式。无论用IIS宿主还是控制台等程序宿主,对于WCF服务代码没有任何区别,但由于是Silverlight来进行调用,就会面临一个跨域的问题,我们今天也主要是来演示如何解决这个问题的,其思路就是在WCF端增加一个跨域服务,当客户端Silverlight调用的时候,Silverlight就会通过跨域服务得到跨域文件,这个方法也是之前在网上查找了大量的资料后,学习到的,在此记下并与大家分享。

   首先,我们要知道跨域文件放到什么地方?前几篇IIS宿主的时候已经讲过,如果通过http绑定方式访问,跨域文件则需要放到http协议指定的端口下面(例如http://localhost:8080,则应该放到8080端口网站下),而通过net.Tcp绑定的时候,跨域文件应该放到80端口的网站下面,这是Silverlight的官方解释。好了话不多说,看代码,同样先展示一下项目结构。

项目结构

结构和以前一样,四层结构:

类库 说明 项目引用
LxContracts 数据契约及 *** 作契约

System.Runtime.Serialization

System.ServiceModel

System.ServiceModel.Web
LxSerivces WCF服务的实现

LxContracts [项目]

System.Runtime.Serialization

System.ServiceModel
LxWcfHost 控制台程序宿主WCF,需要修改Framework版本为.Net Framework 4.0

LxContracts [项目]

LxSerivces [项目]

System.ServiceModel
SilverlightClIEnt Silverlight客户端进行服务的调用  

 

 

 

 

 

 

 

 

 

 

 

 

 

代码实现:

类库LxContracts(包括数据契约student.cs   *** 作契约IStudent.cs 和 跨域契约IDomain.cs)

using System.Runtime.Serialization;namespace LxContracts{    [DataContract]    public class Student    {        /// <summary>        /// 学生编号        </summary>        [DataMember]        int StuID { get; set; }         学生姓名        string Stuname {  所在班级        string Classname {  联系电话        string TelPhoneNum { set; }    }}

using System.ServiceModel;using System.Collections.Generic;namespace LxContracts{    [ServiceContract]    interface IStudent    {        [OperationContract]        List<Student> GetStudent();    }}

 关键是这个跨域契约 

using System.Runtime.Serialization;using System.ServiceModel.Channels;using System.ServiceModel.Web;interface IDomain    {        [OperationContract]        [WebGet(UriTemplate = "ClIEntAccesspolicy.xml")]        Message ProvIDePolicyfile();    }}

类库LxServicers(集合类StudentList.cs  服务类StudentService.cs 和 契约服务DomainService.cs)

using LxContracts;namespace LxServices{    class StudentList : List<Student>    {        public StudentList()        {            this.Add(new Student() { StuID = 1,Stuname = 小明",Classname = 计算机一班123456" });            2,0)">小红计算机二班234567小兰计算机三班890123" });        }    }}

class StudentService : IStudent    {                public List<Student> GetStudent()        {            //实际情况应该为从数据库读取            本例手动生成一个StudentList            StudentList ListStuent = new StudentList();            return ListStuent;        }    }}

 关键是这个实现跨域服务

using System.IO;using System.Xml;using System.ServiceModel.Channels;class DomainService : IDomain    {        public System.ServiceModel.Channels.Message ProvIDePolicyfile()        {            MemoryStream ms = new MemoryStream();            using (fileStream fs = file.OpenRead(@"clIEntaccesspolicy.xml"))            {                int length = (int)fs.Length;                byte[] data = new byte[length];                fs.position = 0;                fs.Read(data,0,length);                ms = new MemoryStream(data);            }            XmlReader reader = XmlReader.Create(ms);            Message result = Message.CreateMessage(MessageVersion.None,"",reader);            return result;        }    }}

OK,服务代码已经编写好了,我们来进行控制台宿主,我们用配置文件先演示http方式宿主

宿主控制台LxWcfHost(我们为该控制台添加一App.config 文件 和 clIEntaccesspolicy.xml 跨域文件,并把跨域文件的属性中的“复制到输出目录” 设置成为 “始终复制”)

此跨域文件把两种跨域文件进行了合并,可以同时支持http和Tcp 

<?xml version="1.0" enCoding="utf-8"?><access-policy>  cross-domain-access>    policy>      allow-from http-request-headers="*">        domain uri="*" />      </allow-fromgrant-tosocket-resource port="4502-4534" protocol="tcp" />        resource path="/" include-subpaths="true">>

using System;using System.ServiceModel;namespace LxWcfHost{    class Program    {        static voID Main(string[] args)        {              ServiceHost host = new ServiceHost(typeof(LxServices.StudentService));            ServiceHost crossDomainserviceHost = typeof(LxServices.DomainService));            host.Opened += delegate            {                Console.Writeline(服务已经启动,按任意键终止...");            };            crossDomainserviceHost.Opened += 跨域服务已经启动,按任意键终止...");            };            crossDomainserviceHost.open();            host.open();            Console.ReadKey();            host.Close();            host.Abort();            crossDomainserviceHost.Close();            crossDomainserviceHost.Abort();        }    }}

 使用http绑定方式的App.config文件的配置如下:

xml version="1.0"configurationsystem.serviceModelbehaviorsserviceBehaviorsbehavior name="LxBehavior">          serviceMetadata httpGetEnabled="true" />          serviceDeBUG includeExceptionDetailinFaults="false" behaviorendpointBehaviors="DomainServiceBehavior"webhttp>    servicesservice ="LxServices.StudentService" behaviorConfigurationendpoint address="" binding="basichttpBinding" contract="LxContracts.IStudent" hostbaseAddresses>            add baseAddress="http://localhost:9090/StudentSrv" service="LxServices.DomainService"="DomainServiceBehavior"            binding="webhttpBinding"="LxContracts.IDomain" ="http://localhost:9090/" serviceHostingEnvironment multipleSiteBindingsEnabled/>  startupsupportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0">

注意:配置文件中的跨域服务的基地址是和WCF服务的基地址端口是一样的都是9090 

至此,我们的控制台宿主程序和WCF服务代码都已经完成,我们来启动一下服务:

SilverlightClIEnt 客户端代码:

UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"              x:Class="SilverlightClIEnt.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/Expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"GrID x:name="LayoutRoot" Background="White"sdk:DataGrID ="dgStudnet" GrID.Row="0" autoGenerateColumns="False"sdk:DataGrID.Columns>                sdk:DataGrIDTextColumn header="学生编号" WIDth="80" Binding="{Binding StuID}" />                ="学生姓名"="100"{Binding Stuname}="所在班级"="120"{Binding Classname}="电话号码"{Binding TelPhoneNum}/>            sdk:DataGrIDGrIDUserControl>

 我们为该Silverlight客户添加服务引用输入:http://localhost:9090/StudentSrv

 

using System.windows.Controls;using System.Collections.ObjectModel;using SilverlightClIEnt.WCF.StudentSrv;namespace SilverlightClIEnt{    partial class MainPage : UserControl    {        ObservableCollection<Student> ListStudent;        public MainPage()        {            InitializeComponent();            ListStudent = new ObservableCollection<Student>();            this.Loaded += new System.windows.RoutedEventHandler(MainPage_Loaded);        }        voID MainPage_Loaded(object sender,System.windows.RoutedEventArgs e)        {            StudentClIEnt proxyClIEnt = new StudentClIEnt();            proxyClIEnt.GetStudentCompleted += new EventHandler<GetStudentCompletedEventArgs>(proxyClIEnt_GetStudentCompleted);            proxyClIEnt.GetStudentAsync();        }        voID proxyClIEnt_GetStudentCompleted(if (e.Error == null)            {                ListStudent = e.Result;                this.dgStudnet.ItemsSource = ListStudent;            }        }    }}

 OK,我们来调试一下,Silverlight客户端进行一下调用WCF服务,结果如下图:

http绑定方式已经可以成功调用了,我们如何使用TCP方式呢,很简单,我们只要修改一下App.config文件即可完成,如下:

App.config Tcp方式配置代码

 注意:配置文件中的跨域服务的基地址已经变成了 http://localhost:80 端口了

我们将先前的Silverlight客户端服务引用重新配置或者删除后重新添加:

然后,再次运行Silverlight客户端程序,也会得到相应的结果。

总结

以上是内存溢出为你收集整理的Silverlight与WCF通信(四) :Silverlight访问控制台宿主WCF(非svc方式)全部内容,希望文章能够帮你解决Silverlight与WCF通信(四) :Silverlight访问控制台宿主WCF(非svc方式)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1011321.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存