稳扎稳打Silverlight(39) - 3.0通信之二进制XML通信, 本地连接

[索引页]
[源码下载]


稳扎稳打Silverlight(39) - 3.0通信之二进制XML通信, 本地连接


作者: webabcd


介绍
Silverlight 3.0 通信的新增功能
  • 二进制XML通信 - 与 WCF 服务间通信,可以使用二进制 XML 传递数据(提高传输性能) 
  • 本地连接 - 允许客户端的两个 Silverlight 程序之间直接进行通信(不用通过服务端)


在线DEMO
http://webabcd.blog.51cto.com/1787395/342289  


示例
1、以二进制 XML 传递数据的演示
服务端(WCF)
BinaryXmlService.svc
InBlock.gif using System; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Runtime.Serialization; 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.ServiceModel.Activation; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Service 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 一个简单的 WCF 服务 
InBlock.gif         /// </summary> 
InBlock.gif        [ServiceContract] 
InBlock.gif        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
InBlock.gif         public  class BinaryXmlService 
InBlock.gif        { 
InBlock.gif                [OperationContract] 
InBlock.gif                 public  string Hello( string name) 
InBlock.gif                { 
InBlock.gif                         return  "Hello: " + name; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Web.config
< system.serviceModel > 
         < bindings > 
                 < customBinding > 
                         < binding  name ="customBinding0" > 
                                 < binaryMessageEncoding  /> 
                                 < httpTransport  /> 
                         </ binding > 
                 </ customBinding > 
         </ bindings > 
         < serviceHostingEnvironment  aspNetCompatibilityEnabled ="true"  /> 
         < behaviors > 
                 < serviceBehaviors > 
                         < behavior  name ="Silverlight30.Service.BinaryXmlServiceBehavior" > 
                                 < serviceMetadata  httpGetEnabled ="true"  /> 
                                 < serviceDebug  includeExceptionDetailInFaults ="false"  /> 
                         </ behavior > 
                 </ serviceBehaviors > 
         </ behaviors > 
         < services > 
                 < service  behaviorConfiguration ="Silverlight30.Service.BinaryXmlServiceBehavior" 
                         name ="Silverlight30.Service.BinaryXmlService" > 
                         < endpoint  address ="" binding="customBinding"  bindingConfiguration ="customBinding0" 
                                 contract ="Silverlight30.Service.BinaryXmlService"  /> 
                         < endpoint  address ="mex"  binding ="mexHttpBinding"  contract ="IMetadataExchange"  /> 
                 </ service > 
         </ services > 
</ system.serviceModel >
 
客户端
BinaryXml.xaml
<navigation:Page x:Class="Silverlight30.Communication.BinaryXml"    
                     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" 
                     mc:Ignorable="d" 
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
                     d:DesignWidth="640" d:DesignHeight="480" 
                     Title="BinaryXml Page"> 
        <Grid x:Name="LayoutRoot"> 
                <StackPanel Orientation="Horizontal" Height="30"> 
                 
                        <!--支持二进制 XML 通信--> 
                 
                        <TextBox x:Name="txtName" Text="webabcd" /> 
                        <Button x:Name="btnHelloConfig" Content="引用服务后(使用代理),通过配置的方式与服务端做二进制XML通信" Click="btnHelloConfig_Click" /> 
                        <Button x:Name="btnHelloCoding" Content="引用服务后(使用代理),通过编程的方式与服务端做二进制XML通信" Click="btnHelloCoding_Click" /> 
                 
                </StackPanel> 
        </Grid> 
</navigation:Page>
 
BinaryXml.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif using Silverlight30.BinaryXmlService; 
InBlock.gif using System.ServiceModel.Channels; 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Communication 
InBlock.gif
InBlock.gif         public partial  class BinaryXml : Page 
InBlock.gif        { 
InBlock.gif                 public BinaryXml() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void client_HelloCompleted( object sender, HelloCompletedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         if (e.Error ==  null
InBlock.gif                                MessageBox.Show(e.Result); 
InBlock.gif                         else 
InBlock.gif                                MessageBox.Show(e.Error.ToString()); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnHelloConfig_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // 通过配置文件(ServiceReferences.ClientConfig)的方式调用以二进制 XML 通信的 WCF 服务(需要使用代理) 
InBlock.gif                        BinaryXmlServiceClient client =  new BinaryXmlServiceClient(); 
InBlock.gif                        client.HelloCompleted +=  new EventHandler<HelloCompletedEventArgs>(client_HelloCompleted); 
InBlock.gif                        client.HelloAsync(txtName.Text); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnHelloCoding_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // 通过编程的方式调用以二进制 XML 通信的 WCF 服务(需要使用代理) 
InBlock.gif                        BinaryMessageEncodingBindingElement binary =  new BinaryMessageEncodingBindingElement(); 
InBlock.gif                        HttpTransportBindingElement transport =  new HttpTransportBindingElement(); 
InBlock.gif                        CustomBinding binding =  new CustomBinding(binary, transport); 
InBlock.gif                        EndpointAddress address =  new EndpointAddress( "http://localhost:8616/BinaryXmlService.svc"); 
InBlock.gif                        BinaryXmlServiceClient client = new BinaryXmlServiceClient(binding, address); 
InBlock.gif                        client.HelloCompleted += new EventHandler<HelloCompletedEventArgs>(client_HelloCompleted); 
InBlock.gif                        client.HelloAsync(txtName.Text); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
ServiceReferences.ClientConfig
< configuration > 
         < system.serviceModel > 
                 < bindings > 
                         < customBinding > 
                                 < binding  name ="CustomBinding_BinaryXmlService" > 
                                         < binaryMessageEncoding  /> 
                                         < httpTransport  maxReceivedMessageSize ="2147483647"  maxBufferSize ="2147483647"  /> 
                                 </ binding > 
                         </ customBinding > 
                 </ bindings > 
                 < client > 
                         < endpoint  address ="http://localhost:8616/BinaryXmlService.svc" 
                                 binding ="customBinding"  bindingConfiguration ="CustomBinding_BinaryXmlService" 
                                 contract ="BinaryXmlService.BinaryXmlService"  name ="CustomBinding_BinaryXmlService"  /> 
                 </ client > 
         </ system.serviceModel > 
</ configuration >
 
 
2、本地连接的演示
Silverlight 程序 1
LocalConnection.xaml
<navigation:Page x:Class="Silverlight30.Communication.LocalConnection"    
                     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" 
                     mc:Ignorable="d" 
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
                     d:DesignWidth="640" d:DesignHeight="480" 
                     Title="LocalConnection Page"> 
        <Grid x:Name="LayoutRoot"> 
                <StackPanel> 
                 
                        <!--结合 Silverlight30.LocalConnection/MainPage.xaml 中的项目演示 Silverlight 对本地连接的支持--> 
                 
                        <TextBlock Text="我是 abc" /> 
                        <Button x:Name="btnSubmit" Content="提交" Click="btnSubmit_Click" /> 
                        <TextBlock x:Name="lblResult" /> 
                         
                </StackPanel> 
        </Grid> 
</navigation:Page>
 
LocalConnection.xaml.cs
InBlock.gif /* 
InBlock.gif * LocalMessageReceiver - 本地连接接收器 
InBlock.gif *         ReceiverName - 接收器的名称。与另一个 Silverlight 程序所设置的发送器的接收器名称相对应 
InBlock.gif *         AllowedSenderDomains - 信任的发送器所属域名列表 
InBlock.gif *         DisableSenderTrustCheck - 是否不理会 Vista 下的 IE 7 的保护模式。默认值为 false 
InBlock.gif *         NameScope - 接收器名称是在同域唯一还是在全域唯一(ReceiverNameScope.Domain 同域唯一,默认值;ReceiverNameScope.Global 全域唯一) 
InBlock.gif *         Listen() - 开始监听发送过来的信息 
InBlock.gif *         MessageReceived事件 - 接收完成事件 
InBlock.gif *            
InBlock.gif * LocalMessageSender - 本地连接发送器 
InBlock.gif *         ReceiverName - 接收器的名称。与另一个 Silverlight 程序所设置的接收器的接收器名称相对应 
InBlock.gif *         ReceiverDomain - 将要发送至的接收器所属域名 
InBlock.gif *         SendAsync(string message, object userState) - 异步发送数据。(参数1:需要发送的信息;参数2:上下文,可以传递给发送完成事件) 
InBlock.gif *         SendCompleted事件 - 发送完成事件 
InBlock.gif */
 
InBlock.gif 
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif using System.Windows.Navigation; 
InBlock.gif using System.Windows.Messaging; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Communication 
InBlock.gif
InBlock.gif         public partial  class LocalConnection : Page 
InBlock.gif        { 
InBlock.gif                LocalMessageSender _sender; 
InBlock.gif 
InBlock.gif                 public LocalConnection() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(LocalConnection_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void LocalConnection_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        _sender =  new LocalMessageSender( "abc"); 
InBlock.gif 
InBlock.gif                        LocalMessageReceiver receiver =  new LocalMessageReceiver( "xyz"); 
InBlock.gif                        receiver.MessageReceived +=  new EventHandler<MessageReceivedEventArgs>(receiver_MessageReceived); 
InBlock.gif                        receiver.Listen();                         
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void receiver_MessageReceived( object sender, MessageReceivedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        lblResult.Text += e.Message +  "\r\n"
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnSubmit_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        _sender.SendAsync( "在 abc 单击了按钮"); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Silverlight 程序 2
Silverlight30.LocalConnection/MainPage.xaml
<UserControl x:Class="Silverlight30.LocalConnection.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"    
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"> 
        <Grid x:Name="LayoutRoot"> 
                <StackPanel> 
                 
                        <!--结合 Silverlight30/Communication/LocalConnection.xaml 中的项目演示 Silverlight 对本地连接的支持--> 
                 
                        <TextBlock Text="我是 xyz" /> 
                        <Button x:Name="btnSubmit" Content="提交" Click="btnSubmit_Click" /> 
                        <TextBlock x:Name="lblResult" /> 
                         
                </StackPanel> 
        </Grid> 
</UserControl>
 
Silverlight30.LocalConnection/MainPage.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Windows.Messaging; 
InBlock.gif 
InBlock.gif namespace Silverlight30.LocalConnection 
InBlock.gif
InBlock.gif         public partial  class MainPage : UserControl 
InBlock.gif        { 
InBlock.gif                LocalMessageSender _sender; 
InBlock.gif 
InBlock.gif                 public MainPage() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(MainPage_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void MainPage_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        _sender =  new LocalMessageSender( "xyz"); 
InBlock.gif 
InBlock.gif                        LocalMessageReceiver receiver =  new LocalMessageReceiver( "abc"); 
InBlock.gif                        receiver.MessageReceived +=  new EventHandler<MessageReceivedEventArgs>(receiver_MessageReceived); 
InBlock.gif                        receiver.Listen(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void receiver_MessageReceived( object sender, MessageReceivedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        lblResult.Text += e.Message + Environment.NewLine; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnSubmit_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        _sender.SendAsync( "在 xyz 单击了按钮"); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
以上两个 Silverlight 程序间可以进行本地通信
Silverlight30.LocalConnectionTestPage.html
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" 
        height="100%" style="float: left; width: 50%"> 
         < param  name ="source"  value ="ClientBin/Silverlight30.xap"  /> 
         < param  name ="onError"  value ="onSilverlightError"  /> 
         < param  name ="background"  value ="white"  /> 
         < param  name ="minRuntimeVersion"  value ="3.0.40624.0"  /> 
         < param  name ="autoUpgrade"  value ="true"  /> 
         < a  href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"  style ="text-decoration: none" > 
                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" 
                        style="border-style: none" /> 
         </a> 
</object> 
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" 
        height="100%" style="float: left; width: 50%"> 
         < param  name ="source"  value ="ClientBin/Silverlight30.LocalConnection.xap"  /> 
         < param  name ="onError"  value ="onSilverlightError"  /> 
         < param  name ="background"  value ="white"  /> 
         < param  name ="minRuntimeVersion"  value ="3.0.40624.0"  /> 
         < param  name ="autoUpgrade"  value ="true"  /> 
         < a  href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"  style ="text-decoration: none" > 
                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" 
                        style="border-style: none" /> 
         </a> 
</object>
 
 



     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/342768 ,如需转载请自行联系原作者

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值