我是靠谱客的博主 俭朴花瓣,这篇文章主要介绍async callback z,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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(); } }

 

转载于:https://www.cnblogs.com/zeroone/p/4903537.html

最后

以上就是俭朴花瓣最近收集整理的关于async callback z的全部内容,更多相关async内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部