Tutorials to .com

Tutorials to .com » Dotnet » Webservice » Exercise: from Windows Form to call XML Web services

Exercise: from Windows Form to call XML Web services

Print View , by: iSee ,Total views: 20 ,Word Count: 1533 ,Date: Thu, 21 May 2009 Time: 12:02 AM

xml Web services is a new Visual Studio feature, which provides in the loosely coupled environment using standard protocols (eg HTTP, XML, XSD, SOAP and WSDL) message exchange functions. Can be structured and the types of these messages or loose definition of these messages. Because the Web services standards-based protocol, so Web services applications with a variety of different implementations, platforms and communications equipment. For more information, please refer to the managed code xml Web services.
Web services can use the enhanced Windows Forms features. Connect with Windows Forms and Web services and Web service method calls as simple as these methods to deal with on the server, and then return the results of method calls.
There are two types of Web service method: synchronous and asynchronous. When the synchronous Web service method call, the caller to respond after waiting for Web services continue to carry out the operation. When the asynchronous Web service method call, you can wait for Web services in response to calls at the same time continue to use the thread. This allows you to client applications in the effective use of existing collection of threads. The use of synchronous and asynchronous Web service method For more information, please refer to the use of managed code access to xml Web services.
Synchronous Web service method <br> synchronous Web service method call, including the method call; waiting for the server to carry out the calculation and return a value; and then continue to implement the Windows form in the other code.
The creation of XML Web services
  1. Create Web services applications. For more information, please refer to the creation of managed code in the XML Web services.
  2. In the solution Explorer, right-click. Asmx file and choose "View Code."
  3. Add up to create the implementation of the Web service method. The following Web service method to add two integers, and then return to the two:

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. Multiplied to create another implementation of the Web service method. The following Web service method to multiply two integers and return the product both:

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. From the "generation" menu, select "generate solutions." Can also be browsed in this project created. Asmx file in order to understand more information about Web services. Can now call from the Windows Forms Web services.

Synchronous call XML Web services
  1. Create a new Windows application. For more information, please refer to the project to create Windows applications.
  2. Add on top of Web services to create a reference. For more information, please refer to add and remove Web reference.
  3. From the toolbox, add three TextBox controls and two Button controls. Text box for digital, button is used in computing and Web service method call.
  4. Controls set up by the following attributes:

Control

Properties

Text

TextBox1

Text

0

TextBox2

Text

0

TextBox3

Text

0

Button1

Text

Add

Button2

Text

Multiplied
  1. Right-click the form and select "View Code."
  2. Examples of Web services will be created for the class members. Need to know to create Web services, where the above-mentioned server name.

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. Click event for Button1 to create the event handler. For more information, please refer to the "Windows Form Designer" to create event handler.

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. The same way for the creation of Button2's Click event handler events and add the following code.

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 ();
)
  1. Press F5 key to run the application. The first two text box value. When press the "Add" button, the third text box will appear and the two values. When press the "x" button, the third text box will display the two values of the plot.

Attention because the Web services instantiated on the server, so the server needs to take some time to deal with the first Web service call. In the application of these button, it is necessary to bear in mind this point. Following a deal with the time lag.
When <br> asynchronous Web service calls asynchronous Web service method, the application is waiting to respond to Web services continue to run at the same time. This allows you to client applications in the effective use of resources. This Windows application in the realization of Web services is to save resources.
For more information, please refer to asynchronous access to managed code in the XML Web services.


.Net WebService Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.