演练:从 Windows 窗体调用 XML Web services
来源:互联网 发布:二次元聊天软件 编辑:程序博客网 时间:2024/04/28 15:15
演练:从 Windows 窗体调用 XML Web services
Visual Studio 2005 其他版本
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 服务方法包括调用该方法;等待在服务器上进行的计算并返回一个值;然后再继续执行 Windows 窗体中的其他代码。
创建 XML Web services
创建 Web 服务应用程序。有关更多信息,请参见使用托管代码创建 XML Web services。
在“解决方案资源管理器”中,右键单击 .asmx 文件并选择“查看代码”。
创建执行相加的 Web 服务方法。下面的代码示例显示的 Web 服务方法以两个整数作为输入并将其相加,然后返回和。
VB<WebMethod()> Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer Return x + yEnd Function
C#[WebMethod]public int WebAdd(int x, int y){ return x + y;}
J#/** @attribute WebMethod() */public int WebAdd(int x, int y){ return x + y;}
创建另一个执行相乘的 Web 服务方法。下面的代码示例显示的 Web 服务方法以两个整数作为输入并将其相乘,然后返回积。
VB<WebMethod()> Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer Return x * yEnd Function
C#[WebMethod]public int WebMultiply(int x, int y) { return x * y;}
J#/** @attribute WebMethod() */public int WebMultiply(int x, int y) { return x * y;}
从“生成”菜单中选择“生成解决方案”。也可以浏览到在此项目中创建的 .asmx 文件,以便了解 Web 服务的更多信息。现在就可以从 Windows 窗体调用 Web 服务了。
同步调用 XML Web services
创建新的基于 Windows 的应用程序。有关更多信息,请参见如何:创建 Windows 应用程序项目。
安全注意 调用 Web 方法需要 WebPermission 类授予的特权级别。如果在部分信任的上下文中运行,则该进程可能会引发异常。有关更多信息,请参见代码访问安全性基础知识。
在“解决方案资源管理器”中,右键单击项目的“引用”节点,然后单击“添加 Web 引用”。
“添加 Web 引用”对话框打开。
在“地址”框中键入下面的 Web 服务 URI,然后按 Enter 键:
otherhttp://localhost/WebService1/Service1.asmx
这是您在上一过程中创建的 Web 服务的 URI。在“添加 Web 引用”对话框的左窗格中出现 WebAdd 和 WebMultiply Web 方法。
单击“添加引用”。
从“工具箱”中,添加三个 TextBox 控件和两个 Button 控件。文本框用于数字,按钮则用于计算和调用 Web 服务方法。
采用下面的方式设置控件的属性:
控件 属性 文本 TextBox1
文本
0
TextBox2
文本
0
TextBox3
文本
0
Button1
文本
相加
Button2
文本
相乘
右键单击窗体并选择“查看代码”。
将 Web 服务的实例创建为类成员。需要知道创建上述 Web 服务所在的服务器的名称。
VB' 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();
J#localhost.Service1 MathServiceClass = new localhost.Service1();
为 Button1 的 Click 事件创建一个事件处理程序。有关详细信息,请参见如何:使用设计器创建事件处理程序。
VBPrivate 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.ToStringEnd 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#) 在窗体的构造函数中放置以下代码以注册事件处理程序。
J#this.button1.Click += new System.EventHandler(this.button1_Click);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 = Integer.parseInt(textBox1.get_Text()); y = Integer.parseInt(textBox2.get_Text()); // Call the WebAdd Web service method from the instance of the Web service. z = MathServiceClass.WebAdd(x, y); textBox3.set_Text(""+z);}
以相同的方式为 Button2 的 Click 事件创建事件处理程序,并添加以下代码。
VBPrivate 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.ToStringEnd 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#) 在窗体的构造函数中放置以下代码以注册事件处理程序。
J#this.button2.Click += new System.EventHandler(this.button2_Click);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 = Integer.parseInt(textBox1.get_Text()); y = Integer.parseInt(textBox2.get_Text()); // Call the WebAdd Web service method from the instance of the Web service. z = MathServiceClass.WebMultiply(x, y); textBox3.set_Text(""+z);}
按 F5 键运行应用程序。
在前两个文本框中输入值。当按“相加”按钮时,第三个文本框将显示两个值的和。当按“相乘”按钮时,第三个文本框将显示两个值的积。
注意 因为 Web 服务要在服务器上实例化,所以服务器需要花费一段时间来处理第一个 Web 服务调用。在应用程序中按这些按钮时,要切记这一点。下面一节处理这种时间滞后。
当调用异步 Web 服务方法时,应用程序在等待 Web 服务响应的同时继续运行。这使得您能够在客户端应用程序中有效地使用资源。
有关更多信息,请参见如何:使用托管代码异步访问 XML Web services。
- 演练:从 Windows 窗体调用 XML Web services
- 演练:使用 Visual C# 从 Windows 窗体控件继承
- php通过nusoap调用XML Web services
- 从PL/SQL调用web services实例
- 演练:使用转换在 Web 窗体页中显示 XML 文档
- Java调用.NET XML Web Services返回的DataSet
- 调用.NET XML Web Services返回数据集合一
- Java调用.NET XML Web Services返回的DataSet
- XML Web Services性能
- vc++ 调用web services
- web services 调用
- 底层调用 Web Services
- web services客户端调用
- Using Windows Web Services
- XML Web Services 底层结构
- XML Web services 基础结构
- 创建 XML Web services 方法
- Microsoft .NET XML Web Services
- 在Delphi中开发使用多显示器的应用程序
- 鸟哥的Linux私房菜10.12 档案权限与目录配置(root身份)
- 利用php中的buffer control来控制输出
- MyEclipse性能优化
- android view 的 setVisibility(int visibility)的参数
- 演练:从 Windows 窗体调用 XML Web services
- Fedora 15 安装手记
- 引发类型为System.OutOfMemoryException的异常
- 百度知道推客户端APP 同时支持iOS及Android
- connamn的mian文件分析(待续)
- 时间过得真快,一晃三年过去了
- 浅谈 Qt 静态编译
- 关于JQuery Ajax时出现 parsererror的问题
- oracle网络配置(listener.ora/sqlnet.ora/tnsnames.ora)