现在的位置: 首页 > 综合 > 正文

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

2011年06月27日 ⁄ 综合 ⁄ 共 8245字 ⁄ 字号 评论关闭
[索引页]
[源码下载]

稳扎稳打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

抱歉!评论已关闭.