教你一步一步动手创建并使用自己的web services

来源:互联网 发布:爱福窝软件福币 编辑:程序博客网 时间:2024/05/16 07:23
第一步:创建 XML Web services
  1. 创建 Web 服务应用程序。有关更多信息,请参阅创建托管代码中的 XML Web services。
  2. 在解决方案资源管理器中,用右键单击 .asmx 文件并选择“查看代码”。
  3. 创建执行相加的 Web 服务方法。以下 Web 服务方法将两个整数相加,然后返回两者的和:

4.           ' Visual Basic
5.           <WebMethod()> Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer
6.              Return x + y
7.           End Function
8.            
9.           // C#
10.       [WebMethod]
11.       public int WebAdd(int x, int y)
12.       {
13.          return x + y;
}
  1. 创建另一个执行相乘的 Web 服务方法。以下 Web 服务方法将两个整数相乘,并返回两者的积:

15.       ' Visual Basic
16.       <WebMethod()> Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer
17.          Return x * y
18.       End Function
19.        
20.       // C#
21.       [WebMethod]
22.       public int WebMultiply(int x, int y)
23.       {
24.          return x * y;
}
  1. 从“生成”菜单中,选择“生成解决方案”。也可以浏览到在此项目中创建的 .asmx 文件,以便了解 Web 服务的更多信息。现在就可以从 Windows 窗体调用 Web 服务了。

第二步:调用 XML Web services
  1. 创建新的 Windows 应用程序。有关更多信息,请参阅创建 Windows 应用程序项目。
  2. 添加对上面创建的 Web 服务的引用。详细信息,请参阅添加和移除 Web 引用。
  3. 从工具箱中,添加三个 TextBox 控件和两个 Button 控件。文本框用于数字,按钮则用于计算和调用 Web 服务方法。
  4. 按以下方式设置控件的属性:

控件
属性
文本
TextBox1
Text
0
TextBox2
Text
0
TextBox3
Text
0
Button1
Text
相加
Button2
Text
相乘
  1. 用右键单击该窗体并选择“查看代码”。
  2. 将 Web 服务的实例创建为类成员。需要知道创建上述 Web 服务所在的服务器名称。

7.           ' Visual Basic
8.           ' Replace localhost below with the name of the server where
9.           ' you created the Web service.
10.       Dim MathServiceClass As New localhost.Service1()
11.        
12.       // C#
localhost.Service1 MathServiceClass = new localhost.Service1();
  1. 为 Button1 的 Click 事件创建事件处理程序。详细信息,请参阅在“Windows 窗体设计器”上创建事件处理程序。

14.       ' Visual Basic
15.       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
16.       ' Create instances of the operands and result.
17.          Dim x, y, z As Integer
18.       ' Parse the contents of the text boxes into integers.
19.          x = Integer.Parse(TextBox1.Text)
20.          y = Integer.Parse(TextBox2.Text)
21.       ' Call the WebAdd Web service method from the instance of the Web service.
22.          z = MathServiceClass.WebAdd(x, y)
23.          TextBox3.Text = z.ToString
24.       End Sub
25.        
26.       // C#
27.       private void button1_Click(object sender, System.EventArgs e)
28.       {
29.       // Create instances of the operands and result.
30.          int x, y, z;
31.       // Parse the contents of the text boxes into integers.
32.          x = int.Parse(textBox1.Text);
33.          y = int.Parse(textBox2.Text);
34.       // Call the WebAdd Web service method from the instance of the Web service.
35.          z = MathServiceClass.WebAdd(x, y);
36.          textBox3.Text = z.ToString();
}
  1. 以相同方式为 Button2 的 Click 事件创建事件处理程序,并添加以下代码。

38.       ' Visual Basic
39.       Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
40.       ' Create instances of the operands and result.
41.          Dim x, y, z As Integer
42.       ' Parse the contents of the text boxes into integers.
43.          x = Integer.Parse(TextBox1.Text)
44.          y = Integer.Parse(TextBox2.Text)
45.       ' Call the WebMultiply Web service method from the instance of the Web service.
46.          z = MathServiceClass.WebMultiply(x, y)
47.          TextBox3.Text = z.ToString
48.       End Sub
49.        
50.       // C#
51.       private void button2_Click(object sender, System.EventArgs e)
52.       {
53.       // Create instances of the operands and result.
54.          int x, y, z;
55.       // Parse the contents of the text boxes into integers.
56.          x = int.Parse(textBox1.Text);
57.          y = int.Parse(textBox2.Text);
58.       // Call the WebAdd Web service method from the instance of the Web service.
59.          z = MathServiceClass.WebMultiply(x, y);
60.          textBox3.Text = z.ToString();
}

F5 键运行应用程序。在前两个文本框中输入值。当按“添加”按钮时,第三个文本框将显示两个值的和。当按“乘”按钮时,第三个文本框将显示两个值的积。

本文摘自:http://www.host01.com/Get/Net/00020004/0561213303392802.htm

原创粉丝点击