跟着Artech学习WCF扩展(1) Binding进行通信

这个demo简单 就一个服务器段的一个客户端的 主要是注释

Server的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace QqServer
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Uri对象,代表监听的URI: 
            Uri address = new Uri("http://127.0.0.1:9999/messagingviabinding");
            //创建BasicHttpBinding对象,我们正是通过它来使用所有的通信功能:
            BasicHttpBinding binding = new BasicHttpBinding();
            //通过binding对象创建IChannelListener对象,并调用Open方法打开它:
            IChannelListener<IReplyChannel> channelListener = binding.BuildChannelListener<IReplyChannel>(address);
            channelListener.Open();
           //通过IChannelListener对象创建IReplyChannel 并调用Open方法打开它: 
            IReplyChannel channel = channelListener.AcceptChannel();
            channel.Open();
            Console.WriteLine("Begin to listen  ");
            while (true)
            {
                //在While循环中监听来自client端的request,一旦request抵达, 
                //调用IReplyChannel 的ReceiveRequest方法,并得到一个RequestContext 对象,
                //通过RequestContext 对象可以得到request message并打印出来:
                RequestContext context = channel.ReceiveRequest(new TimeSpan(1, 0, 0));
                Console.WriteLine("Receive a request message:\n{0}", context.RequestMessage);
                //创建一个Reply message,借助得到的RequestContext 对象发送回client端:
                Message replyMessage = Message.CreateMessage(MessageVersion.Soap11, "http://artech.messagingviabinding", "This is a mannualy created reply message for the purpose of testing");
                context.Reply(replyMessage);

            }
        }
    }
}
Client端的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;

namespace QqClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建EndpointAddress 对象,这和Server的Uri一致,代表请求的地址
            EndpointAddress address = new EndpointAddress("http://127.0.0.1:9999/messagingviabinding");
            //创建BasicHttpBinding对象,通过实现向Server端的发送Request,并接收Reply:
            BasicHttpBinding binding = new BasicHttpBinding();
            //通过Binding对象创建IChannelFactory对象并调用Open方法打开它:
            IChannelFactory<IRequestChannel> chananelFactory = binding.BuildChannelFactory<IRequestChannel>();
            chananelFactory.Open();
            //通过IChannelFactory对象创建IRequestChannel 对象并调用Open方法打开它:
            IRequestChannel channel = chananelFactory.CreateChannel(address);
            channel.Open();
            //创建Request message通过Channel对象发送到Server端,
            //Request方法调用会返回一个Message对象代表从Server端发送回来的Reply message:
            Message requestMessage = Message.CreateMessage(MessageVersion.Soap11, "http://artech/messagingviabinding", "The is a request message manually created for the purpose of testing.");
            Message replyMessage = channel.Request(requestMessage);
            Console.WriteLine("Receive a reply message:\n{0}", replyMessage);

            channel.Close();
            chananelFactory.Close();
            Console.Read();


        }
    }
}
posted @ 2011-10-03 19:28  互联网Fans  阅读(301)  评论(0编辑  收藏  举报