【IT168 技术文档】
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 服务方法以两个整数作为输入并将其相加,然后返回和。
<WebMethod()> Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
[WebMethod]
public int WebAdd(int x, int y)
{
return x + y;
}
/** @attribute WebMethod() */
public int WebAdd(int x, int y)
{
return x + y;
}
-
创建另一个执行相乘的 Web 服务方法。下面的代码示例显示的 Web 服务方法以两个整数作为输入并将其相乘,然后返回积。
<WebMethod()> Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer
Return x * y
End Function
[WebMethod]
public int WebMultiply(int x, int y)
{
return x * y;
}
/** @attribute WebMethod() */
public int WebMultiply(int x, int y)
{
return x * y;
}
-
从“生成”菜单中选择“生成解决方案”。也可以浏览到在此项目中创建的 .asmx 文件,以便了解 Web 服务的更多信息。现在就可以从 Windows 窗体调用 Web 服务了。
同步调用 XML Web services
-
创建新的基于 Windows 的应用程序。有关更多信息,请参见如何:创建 Windows 应用程序项目。
-
在“解决方案资源管理器”中,右键单击项目的“引用”节点,然后单击“添加 Web 引用”。
“添加 Web 引用”对话框打开。
-
在“地址”框中键入下面的 Web 服务 URI,然后按 Enter 键:
http://localhost/WebService1/Service1.asmx
这是您在上一过程中创建的 Web 服务的 URI。在“添加 Web 引用”对话框的左窗格中出现 WebAdd 和 WebMultiply Web 方法。
-
单击“添加引用”。
-
从“工具箱”中,添加三个 TextBox 控件和两个 Button 控件。文本框用于数字,按钮则用于计算和调用 Web 服务方法。
-
采用下面的方式设置控件的属性:
控件 |
属性 |
文本 |
TextBox1 |
文本 |
0 |
TextBox2 |
文本 |
0 |
TextBox3 |
文本 |
0 |
Button1 |
文本 |
相加 |
Button2 |
文本 |
相乘 |
-
右键单击窗体并选择“查看代码”。
-
将 Web 服务的实例创建为类成员。需要知道创建上述 Web 服务所在的服务器的名称。
' Replace localhost below with the name of the server where
' you created the Web service.
Dim MathServiceClass As New localhost.Service1()
localhost.Service1 MathServiceClass = new localhost.Service1();
localhost.Service1 MathServiceClass = new localhost.Service1();
-
为 Button1 的 Click 事件创建一个事件处理程序。有关详细信息,请参见如何:使用设计器创建事件处理程序。
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
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);
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 事件创建事件处理程序,并添加以下代码。
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
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);
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 服务调用。在应用程序中按这些按钮时,要切记这一点。下面一节处理这种时间滞后。 |
|