您的位置:首页 > 其它

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

2005-10-02 18:00 453 查看
第一步:创建 XML Web services 创建 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 服务方法。
按以下方式设置控件的属性:
控件属性文本
TextBox1Text0
TextBox2Text0
TextBox3Text0
Button1Text相加
Button2Text相乘
用右键单击该窗体并选择“查看代码”。
将 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: