zoukankan      html  css  js  c++  java
  • async callback z

    public class StackOverflow_5979252
    {
        [ServiceContract(Name = "IMessageCallback")]
        public interface IAsyncMessageCallback
        {
            [OperationContract(AsyncPattern = true)]
            IAsyncResult BeginOnMessageAdded(string msg, DateTime timestamp, AsyncCallback callback, object asyncState);
            void EndOnMessageAdded(IAsyncResult result);
        }
        [ServiceContract(CallbackContract = typeof(IAsyncMessageCallback))]
        public interface IMessage
        {
            [OperationContract]
            void AddMessage(string message);
        }
        [ServiceBehavior(IncludeExceptionDetailInFaults = true, ConcurrencyMode = ConcurrencyMode.Multiple)]
        public class Service : IMessage
        {
            public void AddMessage(string message)
            {
                IAsyncMessageCallback callback = OperationContext.Current.GetCallbackChannel<IAsyncMessageCallback>();
                callback.BeginOnMessageAdded(message, DateTime.Now, delegate(IAsyncResult ar)
                {
                    callback.EndOnMessageAdded(ar);
                }, null);
            }
        }
        class MyClientCallback : IAsyncMessageCallback
        {
            public IAsyncResult BeginOnMessageAdded(string msg, DateTime timestamp, AsyncCallback callback, object asyncState)
            {
                Action<string, DateTime> act = (txt, time) => { Console.WriteLine("[{0}] {1}", time, txt); };
                return act.BeginInvoke(msg, timestamp, callback, asyncState);
            }
    
            public void EndOnMessageAdded(IAsyncResult result)
            {
                Action<string,DateTime> act = (Action<string,DateTime>)((System.Runtime.Remoting.Messaging.AsyncResult)result).AsyncDelegate;
                act.EndInvoke(result);
            }
        }
        static Binding GetBinding()
        {
            return new NetTcpBinding(SecurityMode.None);
        }
        public static void Test()
        {
            string baseAddress = "net.tcp://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            host.AddServiceEndpoint(typeof(IMessage), GetBinding(), "");
            host.Open();
            Console.WriteLine("Host opened");
    
            InstanceContext instanceContext = new InstanceContext(new MyClientCallback());
            DuplexChannelFactory<IMessage> factory = new DuplexChannelFactory<IMessage>(instanceContext, GetBinding(), new EndpointAddress(baseAddress));
            IMessage proxy = factory.CreateChannel();
            proxy.AddMessage("Hello world");
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            ((IClientChannel)proxy).Close();
            factory.Close();
            host.Close();
        }
    }
    
  • 相关阅读:
    昨晚睡不着觉,测这周运气
    老大让我看baidu他们的查公交是怎么做的,我就看了
    破逼Json,该死的Json库,操了
    我真的好累,实在不知道该怎么办了
    今晚刚回家,给人算了一卦运气
    bzoj4590[Shoi2015]自动刷题机
    bzoj4552[Tjoi2016&Heoi2016]排序
    bzoj3155Preprefix sum
    bzoj2463[中山市选2009]谁能赢呢?
    bzoj3668[Noi2014]起床困难综合症
  • 原文地址:https://www.cnblogs.com/zeroone/p/4903537.html
Copyright © 2011-2022 走看看