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
' 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;
}
' 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;
}
同步调用 XML Web services
安全说明 调用 Web 方法需要 System.Net.WebPermisson 类授予的权限级别。如果在部分信任的上下文中运行,则该进程可能会引发异常。有关更多信息,请参见代码访问安全性基础知识。
| 控件 | 属性 | 文本 |
|---|---|---|
| TextBox1 | Text | 0 |
| TextBox2 | Text | 0 |
| TextBox3 | Text | 0 |
| Button1 | Text | 相加 |
| Button2 | Text | 相乘 |
' 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();
' 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);' 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);注意 因为 Web 服务要在服务器上实例化,所以服务器需要花费一段时间来处理第一个 Web 服务调用。在应用程序中按这些按钮时,要切记这一点。下面一节处理这种时间滞后。
当调用异步 Web 服务方法时,应用程序在等待 Web 服务响应的同时继续运行。这使得您能够在客户端应用程序中有效地使用资源。这种在 Windows 应用程序中实现 Web 服务的方法非常节省资源。
有关详细信息,请参见异步访问托管代码中的 XML Web services。