教你一步一步动手创建并使用自己的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 服务方法。
按以下方式设置控件的属性:
用右键单击该窗体并选择“查看代码”。
将 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
在解决方案资源管理器中,用右键单击 .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
相关文章推荐
- 使用 Eclipse 和 Java SE 6 创建独立 Web Services 应用程序,第 2 部分: Web 服务客户端应用程序
- 一步一步教你在 Android 里创建自己的账号系统(二)--同步数据以及设计账号页面
- log4j使用详解--创建自己的日志系统
- 使用Atlas创建自己的Client Control
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之创建Viewport(2)
- 利用 Composer 一步一步构建自己的 PHP 框架(四)——使用 ORM
- XenServer没有自己的文件系统,也就没有自己的ISO Libery,给我们创建虚拟机带来了不少困扰,使用正文这个方法可以不用先安装FileServer
- 自己动手创建最精简的、最具特色linux
- iphone中使用Xcode创建自己的静态链接库
- 使用VS2012主题插件创建自己的主题
- 使用Libcef+Duilib创建自己的Chrome内核浏览器
- 给自己的文件或软件创建快捷方式,为何不能在别人的电脑上使用的解决办法
- Git使用Github创建自己的代码库
- 自己动手编写一个VS插件(三)——创建工具栏之一
- Visual Studio使用技巧,创建自己的代码片段
- 自己学Docker:14.Docker使用实战:MySQL服务的创建
- Visual Studio使用技巧,创建自己的代码片段
- 使用rpmbuild来创建自己的RPM
- 使用swift3创建自己的pod
- 使用linq to xml 快速创建自己的Rss 之二 Syndication篇