zoukankan      html  css  js  c++  java
  • 一个简单的webservice的demo(下)winform异步调用webservice

      绕了一大圈,又开始接触winform的项目来了,虽然很小吧。写一个winform的异步调用webservice的demo,还是简单的。

      一个简单的Webservice的demo,简单模拟服务

      一个简单的Webservice的demo(中)_前端页面调用

      当winform同步调用服务时,由于调用服务不能像C/S那样快,winform的UI进程一直在等待服务的返回结果,就无法响应用户事件。为了解决这种问题,我们用异步调用。

      首先,先准备一个模拟用的webservice,如下:

      

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading;
     5 using System.Web;
     6 using System.Web.Services;
     7 
     8 namespace WFasy
     9 {
    10     /// <summary>
    11     /// AsyWeb 的摘要说明
    12     /// </summary>
    13     [WebService(Namespace = "http://tempuri.org/")]
    14     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    15     [System.ComponentModel.ToolboxItem(false)]
    16     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    17     // [System.Web.Script.Services.ScriptService]
    18     public class AsyWeb : System.Web.Services.WebService
    19     {
    20 
    21         [WebMethod(Description="模拟服务等待,返回输入字符的前三位")]
    22         public string AsynchronousCall(string code)
    23         {
    24             // 线程sleep5秒,模拟服务请求等待。
    25             Thread.Sleep(5000);
    26             if (code.Length >= 3)
    27                 return code.Substring(0, 3);
    28             return "***";
    29         }
    30     }
    31 }

      现在写winform客户端,引用服务:

      

      点击高级,选择生成异步操作(应注意)

      

      客户端代码:

      

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 
    11 namespace WFAsyC
    12 {
    13     public partial class form : Form
    14     {
    15         public form()
    16         {
    17             InitializeComponent();
    18         }
    19 
    20         private void btnSure_Click(object sender, EventArgs e)
    21         {
    22             string inString = txtIn.Text;
    23             string outString = txtOut.Text;
    24 
    25             WS.AsyWebSoapClient ws = new WS.AsyWebSoapClient();
    26 
    27             // 在调用服务前,应注册调用完成事件,即“回调方法”
    28             // 一定要在异步调用前注册
    29             ws.AsynchronousCallCompleted += ws_AsynchronousCallCompleted;
    30 
    31             // 我们之前用这个方法调用服务
    32             // ws.AsynchronousCall(txtIn.Text);
    33             // 异步调用时用Asyn
    34             ws.AsynchronousCallAsync(txtIn.Text);
    35         }
    36 
    37         /// <summary>
    38         /// 调用成功后触发此事件
    39         /// </summary>
    40         /// <param name="sender">object</param>
    41         /// <param name="e">EventArgs</param>
    42         void ws_AsynchronousCallCompleted(object sender, WS.AsynchronousCallCompletedEventArgs e)
    43         {
    44             if (e.Error == null)
    45             {
    46                 // 若调用服务没有异常
    47                 txtOut.Text = e.Result;
    48             }
    49             else
    50             {
    51                 txtOut.Text = "服务端出现异常!";
    52             }
    53         }
    54     }
    55 }

      最近很忙了,以后坚持写博客,写下一些最简单的demo,记录自己成长过程。这里暂且为下篇了,未来慢慢添加一系列webservice的后续篇章。

  • 相关阅读:
    问题 I: 夫子云游
    问题 H: 小k的简单问题
    问题 G: 圆桌上的晚餐
    问题 F: 超超的自闭意思
    promise与aysnc 与EventProxy
    node的实践(项目三)
    node的实践(项目二)
    node不懂的方法的使用
    github
    node的实践(项目一)
  • 原文地址:https://www.cnblogs.com/muyixiaoguang/p/3948109.html
Copyright © 2011-2022 走看看