• 我们一起学习WCF 第八篇回调函数


      什么是回调函数?

    一个简单的例子:小明想要在京东购买一件商品。他会登陆网站选好自己的商品。然后他把这件商品放在购物车,然后开始付钱(这个表示触发,不付钱不发货(排除货到付款))。然后京东的人员收到了小明这个买商品的信号,就开始发货,选好货品之后委托快递人员送到小明手里。这就是回调。

    现在我用例子详细看看回调函数到底怎么编写的,先看UML图

    开始编码:

     第一步:创建一个契约

     [ServiceContract(SessionMode = SessionMode.Required)]
       public interface ISessionService
        {
            [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
            void CallBackStart();
            [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = true)]
            void CallBackEnd();
        }

    注释1:IsInitiating = true表示开启会话 IsTerminating =true表示收到消息后(如果存在)就关闭会话

    第二步:实现契约

     private Timer myTimer = null;
            Random rd = new Random();
            private IcallBack cb;
            public void CallBackStart()
            {
                cb = OperationContext.Current.GetCallbackChannel<IcallBack>();
                Console.WriteLine("会话ID{0}", OperationContext.Current.SessionId);
                myTimer = new Timer(2000);           
                myTimer.Elapsed += new ElapsedEventHandler(Start);
                myTimer.Enabled = true;
            }
            public void Start(object sender, ElapsedEventArgs e)
            {
                cb.CallBack(rd.Next(1, 1000000));
            }
           public void Dispose()
            {
                myTimer.Dispose();
                Console.WriteLine("服务实例已释放     {0}", DateTime.Now.ToString());  
            }
         
            public void CallBackEnd()
            { 
            Console.WriteLine("{0}:会话即将停止。",OperationContext.Current.SessionId);
            }
        }

    注释2:Timer是一个定时器显示 用random产生随机数。

    第三步:创建一个回调接口

     [ServiceContract]
        public interface IcallBack
        {
            [OperationContract(IsOneWay = true)]
            void CallBack(int Value);
        }

    第四步:客户端实现回调接口

    首先客户端建一个类实现回调接口但是我们发现报下列一个错误

    最后检查发现服务端 并没有加上回调接口然后我们在契约在加上CallbackContract=typeof(IcallBack)然后在进行引用发现没有问题了

      public event EventHandler CallBackEvent;
           public void CallBack(int Value)
           {
               if (CallBackEvent != null)
               {
                   TimeEventArg Tea = new TimeEventArg();
                   Tea.Value = Value.ToString();
                   CallBackEvent(this,Tea);
               }
           }

    注释3:CallBackEvent是我们定义的一个事件,进行把服务器传来的数值返回给客户端

    第五步:客户端进行调用

     ConClient.SessionServiceClient Client = null;
                CallBackHandler cbk = new CallBackHandler();
                cbk.CallBackEvent += cb_ValueCallbacked;
                Console.WriteLine("请选择会话模式:0表示开始,1表示关闭");
                while(true)
                {
                    string SessionMode = Console.ReadLine();
                    if (SessionMode == "0")
                    {
                        Client = new ConClient.SessionServiceClient(new System.ServiceModel.InstanceContext(cbk));
                        Client.CallBackStart();
                    }
                    else if(SessionMode=="1")
                    {
                        if (Client != null)
                        {
                            Client.CallBackEnd();
                        }
                    }
                }
               
            }
            public static void cb_ValueCallbacked(object sender,EventArgs e)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                TimeEventArg Tea = new TimeEventArg();
                Tea = (TimeEventArg)e;
                Console.WriteLine(Tea.Value);
            }

    最后我们看运行结果

    服务端:

    客户端:

    回调就讲到这里了。

    源码

  • 相关阅读:
    Codeforces Gym 100571A A. Cursed Query 离线
    codeforces Gym 100500 J. Bye Bye Russia
    codeforces Gym 100500H H. ICPC Quest 水题
    codeforces Gym 100500H A. Potion of Immortality 简单DP
    Codeforces Gym 100500F Problem F. Door Lock 二分
    codeforces Gym 100500C D.Hall of Fame 排序
    spring data jpa 创建方法名进行简单查询
    Spring集成JPA提示Not an managed type
    hibernate配置文件中的catalog属性
    SonarLint插件的安装与使用
  • 原文地址:https://www.cnblogs.com/LipeiNet/p/4673715.html
Copyright © 2020-2023  润新知