稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTcpBinding的支持
2010-10-12 12:30:06 来源:WEB开发网NetTcpBinding.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 System.ServiceModel;
using System.Net.Sockets;
using System.ServiceModel.Channels;
namespace Silverlight40.Communication
{
public partial class NetTcpBinding : Page, DuplexServiceReference.IDuplexCallback
{
private DuplexServiceReference.DuplexClient _client;
public NetTcpBinding()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void btnSend_Click(object sender, RoutedEventArgs e)
{
// 客户端与服务端之间如果没有信道的话,则产生这个信道
if (_client == null)
{
var ctx = new InstanceContext(this);
// 通过配置文件的方式建立信道
_client = new DuplexServiceReference.DuplexClient(ctx);
// 通过编写代码的方式建立信道
/*
EndpointAddress ea = new EndpointAddress("net.tcp://localhost:4502/SocketServer/Duplex");
BindingElement be = new TcpTransportBindingElement();
CustomBinding cb = new CustomBinding(be);
_client = new DuplexServiceReference.DuplexClient(ctx, cb, ea);
*/
}
// 调用服务端的方法
_client.HelloDuplexAsync("客户端发给服务端的信息:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
// _client.HelloDuplexCompleted - 在此 Handler 中获取服务端方法的返回值,因为本例是 IsOneWay 方式,所以没有返回值
}
// 客户端方法,其用于被服务端调用
public void HelloDuplexCallback(string msg)
{
lblMsg.Text += msg + "\n";
}
}
}
Tags:稳扎稳打 Silverlight 通信
编辑录入:爽爽 [复制链接] [打 印]- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTc...
- ››稳扎稳打Silverlight(54) - 4.0通信之对UDP协议的...
- ››稳扎稳打Silverlight(55) - 4.0通信之对UDP协议的...
- ››稳扎稳打Silverlight(56) - 4.0通信之与 WCF Data...
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››Silverlight for Symbian
- ››Silverlight3系列(四)数据绑定 Data Binding 1
更多精彩
赞助商链接