http://www.cnblogs.com/webabcd/archive/2010/09/16/1827752.html
稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTcpBinding的支持,在Socket通信中通过HTTP检索策略文件,HTTP请求中的ClientHttp和BrowserHttp
Posted on 2010-09-16 08:55 webabcd 阅读(6844) 评论( 13) 编辑 收藏
作者:webabcd
介绍
Silverlight 4.0 通信方面的增强:
- NetTcpBinding - 通过 NetTcpBinding 与 WCF 服务进行通信
- 支持在 Socket 通信中通过 HTTP 的方式检索策略文件
- HTTP 请求中的 ClientHttp 方式和 browserHttp 方式的应用
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、演示如何通过 NetTcpBinding 与 WCF 进行双向通信
服务端:
IDuplex.cs
/*
* 双向通信的 Contract
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace SocketServer
{
[ServiceContract(CallbackContract = typeof (IDuplexCallback))]
public interface IDuplex
{
[OperationContract(IsOneWay = true )]
void HelloDuplex( string msg);
}
interface IDuplexCallback
{
[OperationContract(IsOneWay = void HelloDuplexCallback( string msg);
}
}
* 双向通信的 Contract
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace SocketServer
{
[ServiceContract(CallbackContract = typeof (IDuplexCallback))]
public interface IDuplex
{
[OperationContract(IsOneWay = true )]
void HelloDuplex( string msg);
}
interface IDuplexCallback
{
[OperationContract(IsOneWay = void HelloDuplexCallback( string msg);
}
}
Duplex.cs
* 实现 IDuplex 契约
namespace SocketServer
{
class Duplex : IDuplex
{
private IDuplexCallback _callback;
// 服务端方法,其用于被客户端调用
string msg)
{
Program.Form1.ShowMessage(msg);
if (_callback == null )
{
实例化回调接口
_callback = OperationContext.Current.GetCallbackChannel < IDuplexCallback > ();
每一秒调用一次回调接口(即调用客户端的方法)
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 3000d;
timer.Elapsed += delegate { _callback.HelloDuplexCallback( " 服务端发给客户端的信息: " + DateTime.Now.ToString( yyyy-MM-dd HH:mm:ss " )); };
timer.Start();
}
}
}
}
App.config
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
system.serviceModel >
services >
<!--
元数据地址:http://localhost:12345/SocketServer/Duplex/mex
TCP 地址:net.tcp://localhost:4502/SocketServer/Duplex
TCP 端口限制在 4502 - 4534 之间
-->
service name ="SocketServer.Duplex" >
endpoint address ="SocketServer/Duplex" binding ="customBinding" contract ="SocketServer.IDuplex" />
="mex" ="mexHttpBinding" ="IMetadataExchange" host >
baseAddresses >
add baseAddress ="http://localhost:12345/SocketServer/Duplex" />
="net.tcp://localhost:4502/" />
</ service >
Silverlight 4.0 对 NetTcpBinding 的支持是通过自定义绑定的方式来实现的。服务端和客户端都需要使用自定义绑定
-->
bindings customBinding binding binaryMessageEncoding ></ tcpTransport maxReceivedMessageSize ="2147483647" maxBufferSize behaviors serviceBehaviors behavior serviceMetadata httpGetEnabled ="true" serviceDebug includeExceptionDetailInFaults ="true" >
>
< configuration >
system.serviceModel >
services >
<!--
元数据地址:http://localhost:12345/SocketServer/Duplex/mex
TCP 地址:net.tcp://localhost:4502/SocketServer/Duplex
TCP 端口限制在 4502 - 4534 之间
-->
service name ="SocketServer.Duplex" >
endpoint address ="SocketServer/Duplex" binding ="customBinding" contract ="SocketServer.IDuplex" />
="mex" ="mexHttpBinding" ="IMetadataExchange" host >
baseAddresses >
add baseAddress ="http://localhost:12345/SocketServer/Duplex" />
="net.tcp://localhost:4502/" />
</ service >
Silverlight 4.0 对 NetTcpBinding 的支持是通过自定义绑定的方式来实现的。服务端和客户端都需要使用自定义绑定
-->
bindings customBinding binding binaryMessageEncoding ></ tcpTransport maxReceivedMessageSize ="2147483647" maxBufferSize behaviors serviceBehaviors behavior serviceMetadata httpGetEnabled ="true" serviceDebug includeExceptionDetailInFaults ="true" >
>
Form1.cs
启动 WCF 服务,用于演示 Silverlight 4.0 与 WCF 的交互(基于 NetTcpBinding 绑定)
private void LaunchNetTcpBinding()
{
ServiceHost host = new ServiceHost( typeof (SocketServer.Duplex));
host.open();
ShowMessage( 演示 NetTcpBinding 的 WCF 服务已启动 " );
}
private void LaunchNetTcpBinding()
{
ServiceHost host = new ServiceHost( typeof (SocketServer.Duplex));
host.open();
ShowMessage( 演示 NetTcpBinding 的 WCF 服务已启动 " );
}
客户端(需要引用服务的元数据):
NetTcpBinding.xaml
navigation:Page
x:Class
="Silverlight40.Communication.NetTcpBinding"
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"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="NetTcpBinding Page" Grid x:Name ="LayoutRoot" StackPanel HorizontalAlignment ="Left" >
Button Name ="btnSend" Content ="发送信息到服务端" Click ="btnSend_Click" />
TextBlock ="lblMsg" />
StackPanel Grid navigation:Page >
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"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="NetTcpBinding Page" Grid x:Name ="LayoutRoot" StackPanel HorizontalAlignment ="Left" >
Button Name ="btnSend" Content ="发送信息到服务端" Click ="btnSend_Click" />
TextBlock ="lblMsg" />
StackPanel Grid navigation:Page >