zoukankan      html  css  js  c++  java
  • 跟着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();
    
    
            }
        }
    }
  • 相关阅读:
    [App Store Connect帮助]八、维护您的 App(5)生成产品报告
    [App Store Connect帮助]八、维护您的 App(4.4)重置 App 总评分(iOS、Apple TVOS、macOS)
    [App Store Connect帮助]八、维护您的 App(4.3)回复顾客评论(iOS、macOS 或 watchOS)
    [App Store Connect帮助]八、维护您的 App(4.2)查看评分与评论
    Agent XPs disable
    (2.4)DDL增强功能-数据汇总grouping、rollup、cube
    (2.3)DDL增强功能-流程化控制与动态sql
    (2.2)DDL增强功能-自定义函数/表值函数与存储过程
    (2.1)sql server数据类型、同义词、分区表
    (1.5)DML增强功能-try catch及事务控制
  • 原文地址:https://www.cnblogs.com/qqloving/p/2198636.html
Copyright © 2011-2022 走看看