zoukankan      html  css  js  c++  java
  • WCF异步

    WCF异步与否由客户端来决定

    服务端接口:

    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetData(int value);

            [OperationContract]
            string SayHello(string name);
        }

     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }


            public string SayHello(string name)
            {
                System.Threading.Thread.Sleep(5000);
                return "hello" + name;
            }
        }

    前台生成同步+异步方法

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                WCF.Syn.Serivces.Service1Client _client = new WCF.Syn.Serivces.Service1Client();
                MessageBox.Show(_client.SayHello("peter.peng"));
            }

            private void button2_Click(object sender, EventArgs e)
            {
                WCF.Syn.Serivces.Service1Client _client = new WCF.Syn.Serivces.Service1Client();
                _client.SayHelloCompleted+= new EventHandler<WCF.Syn.Serivces.SayHelloCompletedEventArgs>(_client_GetDataCompleted);//第一种方式
                _client.SayHelloAsync("peter");//第一种方式

                //_client.BeginSayHello("peter.peng", doCallBack, _client);//第二种方式
            }

            void _client_GetDataCompleted(object sender, WCF.Syn.Serivces.SayHelloCompletedEventArgs e)//第一种方式
            {
                MessageBox.Show(e.Result);
            }
            private void doCallBack(IAsyncResult result)
            {
                //string s = ((WCF.Syn.Serivces.Service1Client)result.AsyncState).EndSayHello(result);//第二种方式
                //MessageBox.Show(s);//第二种方式
            }
        }

  • 相关阅读:
    (转)130道ASP.NET面试题
    (转)c#对象内存模型
    (转)探讨:ASP.NET技术的学习顺序问题
    (转)ASP.NET缓存概念及其应用浅析
    Response.Redirect在新窗口打开网页
    转载 C# 序列化与反序列化意义详解
    简单进制转化
    简单成绩管理系统(没有存盘)
    kali不能ifconfig等简单命令
    蓝桥杯练习之开花(二分法查找)
  • 原文地址:https://www.cnblogs.com/peter-peng/p/3452818.html
Copyright © 2011-2022 走看看