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

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

概述  [索引页] [源码下载] 稳扎稳打Silverlight(39) - 3.0通信之二进制XML通信, 本地连接 作者: webabcd 介绍 Silverlight 3.0 通信的新增功能 二进制XML通信 - 与 WCF 服务间通信,可以使用二进制 XML 传递数据(提高传输性能)  本地连接 - 允许客户端的两个 Silverlight 程序之间直接进行通信(不用通过服务端) 在线DEMO   [索引页]
[源码下载]

稳扎稳打Silverlight(39) - 3.0通信之二进制XML通信,本地连接
作者: webabcd
介绍
Silverlight 3.0 通信的新增功能
二进制XML通信 - 与 WCF 服务间通信,可以使用二进制 XML 传递数据(提高传输性能)  本地连接 - 允许客户端的两个 Silverlight 程序之间直接进行通信(不用通过服务端)
在线DEMO
http://www.cnblogs.com/webabcd/archive/2009/08/04/1538238.html  
示例
1、以二进制 XML 传递数据的演示
服务端(WCF)
BinaryXmlService.svc

using  System;

using  System.linq;

using  System.Runtime.Serialization;

using  System.ServiceModel;

using  System.ServiceModel.Activation;

using  System.Collections.Generic;

using  System.Text;


namespace  Silverlight30.Service

{

    
/// <summary>

    
/// 一个简单的 WCF 服务

    
/// </summary>

    [ServiceContract]

    [AspNetCompatibilityRequirements(RequirementsMode 
= AspNetCompatibilityRequirementsMode.Allowed)]

    
public class BinaryXmlService

    
{

        [OperationContract]

        
public string Hello(string name)

        
{

            
return "Hello: " + name;

        }

    }

}


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

using  System;

using  System.Collections.Generic;

using  System.linq;

using  System.Net;

using  System.windows;

using  System.windows.Controls;

using  System.windows.documents;

using  System.windows.input;

using  System.windows.Media;

using  System.windows.Media.Animation;

using  System.windows.Shapes;

using  System.windows.Navigation;


using  Silverlight30.BinaryXmlService;

using  System.ServiceModel.Channels;

using  System.ServiceModel;


namespace  Silverlight30.Communication

{

    
public partial class BinaryXml : Page

    
{

        
public BinaryXml()

        
{

            InitializeComponent();

        }


        
voID clIEnt_HelloCompleted(object sender, HelloCompletedEventArgs e)

        
{

            
if (e.Error == null)

                MessageBox.Show(e.Result);

            
else

                MessageBox.Show(e.Error.ToString());

        }


        
private voID btnHelloConfig_Click(object sender, RoutedEventArgs e)

        
{

            
// 通过配置文件(ServiceReferences.ClIEntConfig)的方式调用以二进制 XML 通信的 WCF 服务(需要使用代理)

            BinaryXmlServiceClIEnt clIEnt = new BinaryXmlServiceClIEnt();

            clIEnt.HelloCompleted 
+= new EventHandler<HelloCompletedEventArgs>(clIEnt_HelloCompleted);

            clIEnt.HelloAsync(txtname.Text);

        }


        
private voID btnHelloCoding_Click(object sender, RoutedEventArgs e)

        
{

            
// 通过编程的方式调用以二进制 XML 通信的 WCF 服务(需要使用代理)

            BinaryMessageEnCodingBindingElement binary = new BinaryMessageEnCodingBindingElement();

            httpTransportBindingElement transport 
= new httpTransportBindingElement();

            CustomBinding binding 
= new CustomBinding(binary, transport);

            EndpointAddress address 
= new EndpointAddress("http://localhost:8616/BinaryXmlService.svc");

            BinaryXmlServiceClIEnt clIEnt 
= new BinaryXmlServiceClIEnt(binding, address);

            clIEnt.HelloCompleted 
+= new EventHandler<HelloCompletedEventArgs>(clIEnt_HelloCompleted);

            clIEnt.HelloAsync(txtname.Text);

        }

    }

}


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

/*

 * LocalMessageReceiver - 本地连接接收器

 *     Receivername - 接收器的名称。与另一个 Silverlight 程序所设置的发送器的接收器名称相对应

 *     AllowedSenderDomains - 信任的发送器所属域名列表

 *     disableSenderTrustCheck - 是否不理会 Vista 下的 IE 7 的保护模式。默认值为 false

 *     nameScope - 接收器名称是在同域唯一还是在全域唯一(ReceivernameScope.Domain 同域唯一,默认值;ReceivernameScope.Global 全域唯一)

 *     Listen() - 开始监听发送过来的信息

 *     MessageReceived事件 - 接收完成事件

 *     

 * LocalMessageSender - 本地连接发送器

 *     Receivername - 接收器的名称。与另一个 Silverlight 程序所设置的接收器的接收器名称相对应

 *     ReceiverDomain - 将要发送至的接收器所属域名

 *     SendAsync(string message, object userState) - 异步发送数据。(参数1:需要发送的信息;参数2:上下文,可以传递给发送完成事件)

 *     SendCompleted事件 - 发送完成事件

 
*/


using  System;

using  System.Collections.Generic;

using  System.linq;

using  System.Net;

using  System.windows;

using  System.windows.Controls;

using  System.windows.documents;

using  System.windows.input;

using  System.windows.Media;

using  System.windows.Media.Animation;

using  System.windows.Shapes;

using  System.windows.Navigation;

using  System.windows.Messaging;


namespace  Silverlight30.Communication

{

    
public partial class LocalConnection : Page

    
{

        LocalMessageSender _sender;


        
public LocalConnection()

        
{

            InitializeComponent();


            
this.Loaded += new RoutedEventHandler(LocalConnection_Loaded);

        }


        
voID LocalConnection_Loaded(object sender, RoutedEventArgs e)

        
{

            _sender 
= new LocalMessageSender("abc");


            LocalMessageReceiver receiver 
= new LocalMessageReceiver("xyz");

            receiver.MessageReceived 
+= new EventHandler<MessageReceivedEventArgs>(receiver_MessageReceived);

            receiver.Listen();            

        }


        
voID receiver_MessageReceived(object sender, MessageReceivedEventArgs e)

        
{

            lblResult.Text 
+= e.Message + "\r\n";

        }


        
private voID btnsubmit_Click(object sender, RoutedEventArgs e)

        
{

            _sender.SendAsync(
"在 abc 单击了按钮");

        }

    }

}


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

using  System;

using  System.Collections.Generic;

using  System.linq;

using  System.Net;

using  System.windows;

using  System.windows.Controls;

using  System.windows.documents;

using  System.windows.input;

using  System.windows.Media;

using  System.windows.Media.Animation;

using  System.windows.Shapes;


using  System.windows.Messaging;


namespace  Silverlight30.LocalConnection

{

    
public partial class MainPage : UserControl

    
{

        LocalMessageSender _sender;


        
public MainPage()

        
{

            InitializeComponent();


            
this.Loaded += new RoutedEventHandler(MainPage_Loaded);

        }


        
voID MainPage_Loaded(object sender, RoutedEventArgs e)

        
{

            _sender 
= new LocalMessageSender("xyz");


            LocalMessageReceiver receiver 
= new LocalMessageReceiver("abc");

            receiver.MessageReceived 
+= new EventHandler<MessageReceivedEventArgs>(receiver_MessageReceived);

            receiver.Listen();

        }


        
voID receiver_MessageReceived(object sender, MessageReceivedEventArgs e)

        
{

            lblResult.Text 
+= e.Message + Environment.Newline;

        }


        
private voID btnsubmit_Click(object sender, RoutedEventArgs e)

        
{

            _sender.SendAsync(
"在 xyz 单击了按钮");

        }

    }

}


以上两个 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"   />

    
< 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"   />

    
< 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 >
OK
[源码下载] @H_440_3419@ 总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(39) - 3.0通信之二进制XML通信, 本地连接全部内容,希望文章能够帮你解决稳扎稳打Silverlight(39) - 3.0通信之二进制XML通信, 本地连接所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1071676.html

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

发表评论

登录后才能评论

评论列表(0条)

保存