WEB开发网
开发学院WEB开发ASP.NET 稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTc... 阅读

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

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";
        }
    }
}

上一页  1 2 3 4 5 6 7  下一页

Tags:稳扎稳打 Silverlight 通信

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接