- 創建 Web 服務應用程序。有關更多信息,請參閱創建託管代碼中的 XML Web services。
- 在解決方案資源管理器中,用右鍵單擊 .asmx 文件並選擇“查看代碼”。
- 創建執行相加的 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;
}
- 創建另一個執行相乘的 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;
}
- 從“生成”菜單中,選擇“生成解決方案”。也可以瀏覽到在此項目中創建的 .asmx 文件,以便了解 Web 服務的更多信息。現在就可以從 Windows 窗體調用 Web 服務了。
第二步:調用 XML Web services
- 創建新的 Windows 應用程序。有關更多信息,請參閱創建 Windows 應用程序項目。
- 添加對上面創建的 Web 服務的引用。詳細信息,請參閱添加和移除 Web 引用。
- 從工具箱中,添加三個 TextBox 控件和兩個 Button 控件。文本框用於數字,按鈕則用於計算和調用 Web 服務方法。
- 按以下方式設置控件的屬性:
控件 | 屬性 | 文本 |
TextBox1 | Text | 0 |
TextBox2 | Text | 0 |
TextBox3 | Text | 0 |
Button1 | Text | 相加 |
Button2 | Text | 相乘 |
- 用右鍵單擊該窗體並選擇“查看代碼”。
- 將 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();
- 爲 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();
}
- 以相同方式爲 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