zoukankan      html  css  js  c++  java
  • 异步调用开发应用总结(WCF)

    上一篇随笔的异步调用是方法的异步调用实现,WCF的异步调用有自己的实现逻辑(一些方法和调用过程.net已经制定好)

    WCF异步调用示例:

    客户端调用:

                AsyncManageAgent clientAgent = this.Command as AsyncManageAgent;
                clientAgent.GetNameAsync(Code, userState);
                clientAgent.GetNameCompleted += delegate(object sender, Agent.GetNameCompletedEventArgs args)
                {

                    this.GetNameCompleted(this, new GetNameCompletedEventArgs(args.results,args.Error,args.Cancelled,args.UserState));
                };

    WCF代理类(异步调用)

            private System.IAsyncResult OnBeginGetName(object[] inValues, System.AsyncCallback callback, object asyncState)
            {
                string Code = inValues[0].ToString();
                return this.BeginGetName(Code, callback, asyncState);
            }

            //onEnd
            private object[] OnEndGetName(System.IAsyncResult result)
            {
                string retVal = this.EndGetName(result);
                return new object[] {
                        retVal};
            }

            //OnCompleted
            private void OnGetNameCompleted(object state)
            {
                if ((this.GetNameCompleted != null))
                {
                    InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                    this.GetNameCompleted(this, new GetNameCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
                }
            }
            //Async方法
            public void GetNameAsync(string Code)
            {
                this.GetNameAsync(Code, null);
            }

            public void GetNameAsync(string Code, object userState)
            {
                if ((this.onBeginGetNameDelegate == null))
                {
                    this.onBeginGetNameDelegate = new BeginOperationDelegate(this.OnBeginGetName);
                }
                if ((this.onEndGetNameDelegate == null))
                {
                    this.onEndGetNameDelegate = new EndOperationDelegate(this.OnEndGetName);
                }
                if ((this.onGetNameCompletedDelegate == null))
                {
                    this.onGetNameCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetNameCompleted);
                }
                base.InvokeAsync(this.onBeginGetNameDelegate, new object[] {
                            Code}, this.onEndGetNameDelegate, this.onGetNameCompletedDelegate, userState);
            }

    关于服务端具体实现,服务部署配置等需要另写博客,待续。。

  • 相关阅读:
    Python3 sorted() 函数
    [Python网络编程]一个简单的TCP时间服务器
    [爬虫]统计豆瓣读书中每个标签下的前两百本书
    [leetcode]39. Combination Sum
    [leetcode]18. 4Sum
    [leetcode DP]72. Edit Distance
    [leetcode DP]120. Triangle
    [leetcode DP]91. Decode Ways
    [leetcode DP]70. Climbing Stairs
    [leetcode DP]64. Minimum Path Sum
  • 原文地址:https://www.cnblogs.com/log-long/p/3278140.html
Copyright © 2011-2022 走看看