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();
        }
    }
    
  • 相关阅读:
    丘先生为世界华裔数学家大会(北京 1998)而作
    获取IFC构件的位置数据、方向数据
    TeslaManage运行日志 (201910-201911)
    IFC构件位置数据与revit模型中对应构件位置数据对比
    IFC构件位置信息—ObjectPlacement
    jsoup爬取某网站安全数据
    Java统计文件数量
    osg 渲染ifc数据-测试
    编译日志—20191101
    Tekla 导出ifc并浏览
  • 原文地址:https://www.cnblogs.com/zeroone/p/4903537.html
Copyright © 2011-2022 走看看