天天看點

穩紮穩打Silverlight(39) - 3.0通信之二進制XML通信, 本地連接配接

<a href="http://webabcd.blog.51cto.com/1787395/342743" target="_blank">[索引頁]</a>

<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>

穩紮穩打Silverlight(39) - 3.0通信之二進制XML通信, 本地連接配接

介紹

Silverlight 3.0 通信的新增功能

二進制XML通信 - 與 WCF 服務間通信,可以使用二進制 XML 傳遞資料(提高傳輸性能) 

本地連接配接 - 允許用戶端的兩個 Silverlight 程式之間直接進行通信(不用通過服務端)

線上DEMO

示例

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 

        /// &lt;summary&gt; 

        /// 一個簡單的 WCF 服務 

        /// &lt;/summary&gt; 

        [ServiceContract] 

        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

        public class BinaryXmlService 

        { 

                [OperationContract] 

                public string Hello(string name) 

                { 

                        return "Hello: " + name; 

                } 

        } 

}

Web.config

&lt;system.serviceModel&gt; 

        &lt;bindings&gt; 

                &lt;customBinding&gt; 

                        &lt;binding name="customBinding0"&gt; 

                                &lt;binaryMessageEncoding /&gt; 

                                &lt;httpTransport /&gt; 

                        &lt;/binding&gt; 

                &lt;/customBinding&gt; 

        &lt;/bindings&gt; 

        &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" /&gt; 

        &lt;behaviors&gt; 

                &lt;serviceBehaviors&gt; 

                        &lt;behavior name="Silverlight30.Service.BinaryXmlServiceBehavior"&gt; 

                                &lt;serviceMetadata httpGetEnabled="true" /&gt; 

                                &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt; 

                        &lt;/behavior&gt; 

                &lt;/serviceBehaviors&gt; 

        &lt;/behaviors&gt; 

        &lt;services&gt; 

                &lt;service behaviorConfiguration="Silverlight30.Service.BinaryXmlServiceBehavior" 

                        name="Silverlight30.Service.BinaryXmlService"&gt; 

                        &lt;endpoint address="" binding="customBinding" bindingConfiguration="customBinding0" 

                                contract="Silverlight30.Service.BinaryXmlService" /&gt; 

                        &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt; 

                &lt;/service&gt; 

        &lt;/services&gt; 

&lt;/system.serviceModel&gt;

用戶端

BinaryXml.xaml

&lt;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"&gt; 

        &lt;Grid x:Name="LayoutRoot"&gt; 

                &lt;StackPanel Orientation="Horizontal" Height="30"&gt; 

                        &lt;!--支援二進制 XML 通信--&gt; 

                        &lt;TextBox x:Name="txtName" Text="webabcd" /&gt; 

                        &lt;Button x:Name="btnHelloConfig" Content="引用服務後(使用代理),通過配置的方式與服務端做二進制XML通信" Click="btnHelloConfig_Click" /&gt; 

                        &lt;Button x:Name="btnHelloCoding" Content="引用服務後(使用代理),通過程式設計的方式與服務端做二進制XML通信" Click="btnHelloCoding_Click" /&gt; 

                &lt;/StackPanel&gt; 

        &lt;/Grid&gt; 

&lt;/navigation:Page&gt;

BinaryXml.xaml.cs

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; 

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&lt;HelloCompletedEventArgs&gt;(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); 

ServiceReferences.ClientConfig

&lt;configuration&gt; 

        &lt;system.serviceModel&gt; 

                &lt;bindings&gt; 

                        &lt;customBinding&gt; 

                                &lt;binding name="CustomBinding_BinaryXmlService"&gt; 

                                        &lt;binaryMessageEncoding /&gt; 

                                        &lt;httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /&gt; 

                                &lt;/binding&gt; 

                        &lt;/customBinding&gt; 

                &lt;/bindings&gt; 

                &lt;client&gt; 

                        &lt;endpoint address="http://localhost:8616/BinaryXmlService.svc" 

                                binding="customBinding" bindingConfiguration="CustomBinding_BinaryXmlService" 

                                contract="BinaryXmlService.BinaryXmlService" name="CustomBinding_BinaryXmlService" /&gt; 

                &lt;/client&gt; 

        &lt;/system.serviceModel&gt; 

&lt;/configuration&gt;

2、本地連接配接的示範

Silverlight 程式 1

LocalConnection.xaml

&lt;navigation:Page x:Class="Silverlight30.Communication.LocalConnection"    

                     Title="LocalConnection Page"&gt; 

                &lt;StackPanel&gt; 

                        &lt;!--結合 Silverlight30.LocalConnection/MainPage.xaml 中的項目示範 Silverlight 對本地連接配接的支援--&gt; 

                        &lt;TextBlock Text="我是 abc" /&gt; 

                        &lt;Button x:Name="btnSubmit" Content="送出" Click="btnSubmit_Click" /&gt; 

                        &lt;TextBlock x:Name="lblResult" /&gt; 

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.Windows.Messaging; 

        public partial class LocalConnection : Page 

                LocalMessageSender _sender; 

                public LocalConnection() 

                        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&lt;MessageReceivedEventArgs&gt;(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

&lt;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"&gt; 

                        &lt;!--結合 Silverlight30/Communication/LocalConnection.xaml 中的項目示範 Silverlight 對本地連接配接的支援--&gt; 

                        &lt;TextBlock Text="我是 xyz" /&gt; 

&lt;/UserControl&gt;

Silverlight30.LocalConnection/MainPage.xaml.cs

namespace Silverlight30.LocalConnection 

        public partial class MainPage : UserControl 

                public MainPage() 

                        this.Loaded += new RoutedEventHandler(MainPage_Loaded); 

                void MainPage_Loaded(object sender, RoutedEventArgs e) 

                        _sender = new LocalMessageSender("xyz"); 

                        LocalMessageReceiver receiver = new LocalMessageReceiver("abc"); 

                        receiver.Listen(); 

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

                        _sender.SendAsync("在 xyz 單擊了按鈕"); 

以上兩個 Silverlight 程式間可以進行本地通信

Silverlight30.LocalConnectionTestPage.html

&lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" 

        height="100%" style="float: left; width: 50%"&gt; 

        &lt;param name="source" value="ClientBin/Silverlight30.xap" /&gt; 

        &lt;param name="onError" value="onSilverlightError" /&gt; 

        &lt;param name="background" value="white" /&gt; 

        &lt;param name="minRuntimeVersion" value="3.0.40624.0" /&gt; 

        &lt;param name="autoUpgrade" value="true" /&gt; 

        &lt;a href="http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=3.0.40624.0" style="text-decoration: none"&gt; 

                &lt;img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" 

                        style="border-style: none" /&gt; 

        &lt;/a&gt; 

&lt;/object&gt; 

        &lt;param name="source" value="ClientBin/Silverlight30.LocalConnection.xap" /&gt; 

&lt;/object&gt;

OK

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/342768,如需轉載請自行聯系原作者