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
- 创建 Web 服务应用程序。有关更多信息,请参见创建托管代码中的 XML Web services。
- 在解决方案资源管理器中,用右键单击 .asmx 文件并选择“查看代码”。
- 创建执行相加的 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;
}
- 创建另一个执行相乘的 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;
}
- 从“生成”菜单中,选择“生成解决方案”。也可以浏览到在此项目中创建的 .asmx 文件,以便了解 Web 服务的更多信息。现在就可以从 Windows 窗体调用 Web 服务了。
同步调用 XML Web services
- 创建新的 Windows 应用程序。有关更多信息,请参见创建 Windows 应用程序项目。
安全说明 调用 Web 方法需要 System.Net.WebPermisson 类授予的权限级别。如果在部分信任的上下文中运行,则该进程可能会引发异常。有关更多信息,请参见代码访问安全性基础知识。
- 添加对上面创建的 Web 服务的引用。有关详细信息,请参见添加和移除 Web 引用。
- 从“工具箱”中,添加三个 TextBox 控件和两个 Button 控件。文本框用于数字,按钮则用于计算和调用 Web 服务方法。
- 按以下方式设置控件的属性:
| 控件 | 属性 | 文本 |
| TextBox1 |
Text |
0 |
| TextBox2 |
Text |
0 |
| TextBox3 |
Text |
0 |
| Button1 |
Text |
相加 |
| Button2 |
Text |
相乘 |
- 右击该窗体并选择“查看代码”。
- 将 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();
- 为 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);
- 以相同方式为 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);
- 按 F5 键运行您的应用程序。在前两个文本框中输入值。当按“相加”按钮时,第三个文本框将显示两个值的和。当按“相乘”按钮时,第三个文本框将显示两个值的积。
注意 因为 Web 服务要在服务器上实例化,所以服务器需要花费一段时间来处理第一个 Web 服务调用。在应用程序中按这些按钮时,要切记这一点。下面一节处理这种时间滞后。
异步 Web 服务
当调用异步 Web 服务方法时,应用程序在等待 Web 服务响应的同时继续运行。这使得您能够在客户端应用程序中有效地使用资源。这种在 Windows 应用程序中实现 Web 服务的方法非常节省资源。
有关详细信息,请参见异步访问托管代码中的 XML Web services。 |