中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > Web开发 > .NET > ASP.NET
演练:从 Windows 窗体调用 XML Web services
作者:佚名 时间:2005-06-08 14:43 出处:互连网 责编:chinaitpower
              摘要:暂无

XML Web services 是 Visual Studio 的一个新功能,它提供在松耦合环境中使用标准协议(如 HTTP、XML、XSD、SOAP 和 WSDL)交换消息的功能。可以结构化和类型化这些消息或对这些消息进行松散定义。因为 Web 服务基于标准协议,所以您的 Web 服务应用程序可以与各种不同的实现、平台和设备进行通信。有关更多信息,请参见托管代码中的 XML Web services

可以使用 Web 服务增强 Windows 窗体功能。连接 Windows 窗体和 Web 服务与调用 Web 服务方法一样简单,这些方法在服务器上进行处理,然后返回方法调用的结果。

有两种类型的 Web 服务方法:同步和异步。当调用同步 Web 服务方法时,调用方等待 Web 服务响应后再继续执行操作。当调用异步 Web 服务方法时,可以在等待 Web 服务响应的同时继续使用调用线程。这使得您能够在客户端应用程序中有效地使用现有的线程集合。有关使用同步和异步 Web 服务方法的更多信息,请参见使用托管代码访问 XML Web services

同步 Web 服务方法

调用同步 Web 服务方法包括调用该方法;等待在服务器上进行的计算并返回一个值;然后再继续执行 Windows 窗体中的其他代码。

创建 XML Web services

  1. 创建 Web 服务应用程序。有关更多信息,请参见创建托管代码中的 XML Web services
  2. 在解决方案资源管理器中,用右键单击 .asmx 文件并选择“查看代码”。
  3. 创建执行相加的 Web 服务方法。以下 Web 服务方法将两个整数相加,然后返回两者的和:
    ' Visual Basic
        <WebMethod()> Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer
        Return x + y
        End Function
        // C#
        [WebMethod]
        public int WebAdd(int x, int y)
        {
        return x + y;
        }
  4. 创建另一个执行相乘的 Web 服务方法。以下 Web 服务方法将两个整数相乘,并返回两者的积:
    ' Visual Basic
        <WebMethod()> Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer
        Return x * y
        End Function
        // C#
        [WebMethod]
        public int WebMultiply(int x, int y)
        {
        return x * y;
        }
  5. 从“生成”菜单中,选择“生成解决方案”。也可以浏览到在此项目中创建的 .asmx 文件,以便了解 Web 服务的更多信息。现在就可以从 Windows 窗体调用 Web 服务了。

同步调用 XML Web services

  1. 创建新的 Windows 应用程序。有关更多信息,请参见创建 Windows 应用程序项目
    安全说明   调用 Web 方法需要 System.Net.WebPermisson 类授予的权限级别。如果在部分信任的上下文中运行,则该进程可能会引发异常。有关更多信息,请参见代码访问安全性基础知识
  2. 添加对上面创建的 Web 服务的引用。有关详细信息,请参见添加和移除 Web 引用
  3. 从“工具箱”中,添加三个 TextBox 控件和两个 Button 控件。文本框用于数字,按钮则用于计算和调用 Web 服务方法。
  4. 按以下方式设置控件的属性:
    控件属性文本
    TextBox1 Text 0
    TextBox2 Text 0
    TextBox3 Text 0
    Button1 Text 相加
    Button2 Text 相乘
  5. 右击该窗体并选择“查看代码”。
  6. 将 Web 服务的实例创建为类成员。需要知道创建上述 Web 服务所在的服务器名称。
    ' Visual Basic
        ' Replace localhost below with the name of the server where
        ' you created the Web service.
        Dim MathServiceClass As New localhost.Service1()
        // C#
        localhost.Service1 MathServiceClass = new localhost.Service1();
  7. 为 Button1 的 Click 事件创建事件处理程序。有关详细信息,请参见在 Windows 窗体设计器上创建事件处理程序
    ' Visual Basic
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Create instances of the operands and result.
        Dim x, y, z As Integer
        ' Parse the contents of the text boxes into integers.
        x = Integer.Parse(TextBox1.Text)
        y = Integer.Parse(TextBox2.Text)
        ' Call the WebAdd Web service method from the instance of the Web service.
        z = MathServiceClass.WebAdd(x, y)
        TextBox3.Text = z.ToString
        End Sub
        // C#
        private void button1_Click(object sender, System.EventArgs e)
        {
        // Create instances of the operands and result.
        int x, y, z;
        // Parse the contents of the text boxes into integers.
        x = int.Parse(textBox1.Text);
        y = int.Parse(textBox2.Text);
        // Call the WebAdd Web service method from the instance of the Web service.
        z = MathServiceClass.WebAdd(x, y);
        textBox3.Text = z.ToString();
        }
    Visual C# 说明   确保启用事件处理程序所需的代码存在。在这种情况下,它类似于以下内容:
    this.button1.Click += new System.EventHandler(this.button1_Click);
  8. 以相同方式为 Button2 的 Click 事件创建事件处理程序,并添加以下代码。
    ' Visual Basic
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Create instances of the operands and result.
        Dim x, y, z As Integer
        ' Parse the contents of the text boxes into integers.
        x = Integer.Parse(TextBox1.Text)
        y = Integer.Parse(TextBox2.Text)
        ' Call the WebMultiply Web service method from the instance of the Web service.
        z = MathServiceClass.WebMultiply(x, y)
        TextBox3.Text = z.ToString
        End Sub
        // C#
        private void button2_Click(object sender, System.EventArgs e)
        {
        // Create instances of the operands and result.
        int x, y, z;
        // Parse the contents of the text boxes into integers.
        x = int.Parse(textBox1.Text);
        y = int.Parse(textBox2.Text);
        // Call the WebAdd Web service method from the instance of the Web service.
        z = MathServiceClass.WebMultiply(x, y);
        textBox3.Text = z.ToString();
        }
    Visual C# 说明   确保启用事件处理程序所需的代码存在。在这种情况下,它类似于以下内容:
    this.button2.Click += new System.EventHandler(this.button2_Click);
  9. 按 F5 键运行您的应用程序。在前两个文本框中输入值。当按“相加”按钮时,第三个文本框将显示两个值的和。当按“相乘”按钮时,第三个文本框将显示两个值的积。
    注意   因为 Web 服务要在服务器上实例化,所以服务器需要花费一段时间来处理第一个 Web 服务调用。在应用程序中按这些按钮时,要切记这一点。下面一节处理这种时间滞后。

异步 Web 服务

当调用异步 Web 服务方法时,应用程序在等待 Web 服务响应的同时继续运行。这使得您能够在客户端应用程序中有效地使用资源。这种在 Windows 应用程序中实现 Web 服务的方法非常节省资源。

有关详细信息,请参见异步访问托管代码中的 XML Web services

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有