Walkthrough: Calling an XML Web services from a Windows form

Source: Internet
Author: User
Tags integer tostring web services client visual studio
Services|web|window|xml XML Web Services is a new feature of Visual Studio that provides the ability to exchange messages in loosely coupled environments using standard protocols such as HTTP, XML, XSD, SOAP, and WSDL. These messages can be structured and typed or loosely defined. Because Web services are based on standard protocols, Web service applications can communicate with a variety of implementations, platforms, and devices. For more information, see XML WEB Services in managed code.
You can use WEB services to enhance Windows forms functionality. Connecting Windows forms and Web services is as simple as invoking Web service methods, which are processed on the server and then return the result of the method call.
There are two types of Web service methods: synchronous and asynchronous. When the synchronization Web service method is invoked, the caller waits for the Web service to respond before continuing the operation. When you invoke an asynchronous Web service method, you can continue to use the calling thread while waiting for the Web service to respond. This allows you to effectively use an existing collection of threads in the client application. For more information about using synchronous and asynchronous Web service methods, see Accessing XML Web Services using managed code.
synchronizing Web service methods
Calling the synchronization Web service method involves calling the method, waiting on the calculation on the server and returning a value, and then continuing with other code in Windows forms.
Creating an XML Web services
    1. Create a WEB service application. For more information, see Creating XML Web Services in managed code.
    2. In Solution Explorer, right-click the. asmx file and select View Code.
    3. Create a Web service method that performs the addition. The following Web service method adds two integers and then returns the sum of both:

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 #
[WebMethod]
One. public int webadd (int x, int y)
12. {
return x + y;
}
    1. Create another Web service method that performs multiplication. The following Web service method multiplies two integers and returns the product of both:

' Visual Basic
<webmethod () > Public Function webmultiply (ByVal x As Integer, ByVal y As Integer) As Integer
return x * y
End Function
19.
//C #
[WebMethod]
public int webmultiply (int x, int y)
23. {
return x * y;
}
    1. From the Build menu, select Build Solution. You can also browse to the. asmx file that you created in this project to learn more about WEB services. You can now invoke the Web service from Windows forms.

synchronizing calls to XML Web services
    1. Create a new Windows application. For more information, see Creating a Windows Application project.
    2. Adds a reference to the Web service created above. For more information, see Adding and removing Web references.
    3. From the Toolbox, add three TextBox controls and two Button controls. Text boxes are used for numbers, and buttons are used to compute and invoke Web service methods.
    4. Set the properties of the control in the following ways:

Control
Property
text
TextBox1
Text
0
TextBox2
Text
0
TextBox3
Text
0
Button1
Text
Add
Button2
Text
Multiply
    1. Right-click the form and select View Code.
    2. Creates an instance of a Web service as a class member. You need to know the name of the server where the Web service is created.

7. ' Visual Basic
8. ' Replace localhostBelow with the name of the server where
9. ' You created the Web service.
Dim Mathserviceclass as New localhost. Service1 ()
11.
//C #
localhost. Service1 Mathserviceclass = new localhost. Service1 ();
    1. Creates an event handler for the Button1 Click event. For more information, see Creating an event handler on the Windows Forms Designer.

' Visual Basic
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
' Create instances of the operands and result.
Dim x, Y, z as Integer
Parse the contents of the text boxes into integers.
x = Integer.parse (TextBox1.Text)
y = Integer.parse (TextBox2.Text)
' Call the Webadd Web service to the instance of the Web service.
z = Mathserviceclass.webadd (x, y)
TextBox3.Text = z.tostring
-End Sub
25.
num//C #
private void Button1_Click (object sender, System.EventArgs e)
28. {
//Create instances of the operands and result.
int x, y, Z;
The//Parse the contents of the text boxes into integers.
x = Int. Parse (TextBox1.Text);
y = Int. Parse (TextBox2.Text);
The Webadd Web service method from the instance the Web service.
z = Mathserviceclass.webadd (x, y);
TextBox3.Text = Z.tostring ();
}
    1. Create an event handler for the Button2 Click event in the same way and add the following code.

' Visual Basic
Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click
' Create instances of the operands and result.
Dim x, Y, z as Integer
Parse the contents of the text boxes into integers.
x = Integer.parse (TextBox1.Text)
y = Integer.parse (TextBox2.Text)
The Webmultiply Web service method from the instance of the Web service.
z = mathserviceclass.webmultiply (x, y)
TextBox3.Text = z.tostring
The. End Sub
49.
//C #
The private void button2_click (object sender, System.EventArgs e)
52. {
//Create instances of the operands and result.
int x, y, Z;
//Parse The contents of the text boxes into integers.
x = Int. Parse (TextBox1.Text);
y = Int. Parse (TextBox2.Text);
Webadd//Call the "Web service method" from the instance of the Web service.
z = mathserviceclass.webmultiply (x, y);
TextBox3.Text = Z.tostring ();
}
    1. Press the F5 key to run the application. Enter a value in the first two text boxes. When you press the Add button, the third text box displays the and two values. When you press the Multiply button, the third text box displays the product of two values.

AttentionBecause Web services are instantiated on the server, the server takes a while to process the first Web service invocation. Remember this when you press these buttons in your application. The following section deals with this time lag.
Asynchronous Web Services
When an asynchronous Web service method is invoked, the application continues to run while waiting for the Web service to respond. This enables you to use resources efficiently in client applications. This method of implementing WEB services in a Windows application is very resource-saving.
For more information, see Accessing XML WEB Services in managed code asynchronously.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.