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();
        }
    }
    
  • 相关阅读:
    C++运算符优先级
    UNREFERENCED_PARAMETER
    Eclipse无法查看源码 提示source not found
    JAVAWEB dbutils执行sql命令并遍历结果集时不能查到内容的原因
    JAVAWEB和数据库 Mysql连接不上的原因及解决方案
    电子设备 Kindle如何删除书籍或漫画
    JAVA StringUtils需要导入的包
    JAVAWEB servlet和jsp的权限访问控制
    JAVAWEB servlet验证登录时进行完全的非空判断防止空值登录
    JAVAWEB 遍历mysql结果集时 字段为0、false、null的问题
  • 原文地址:https://www.cnblogs.com/zeroone/p/4903537.html
Copyright © 2011-2022 走看看