Walkthrough: Calling XML Web Services from Windows Forms

XML Web services is a feature of Visual Studio that enables you to develop applications that exchange messages in a loosely coupled environment using standard protocols such as HTTP, XML, XSD, SOAP, and WSDL. The messages can be structured and typed or loosely defined. Because the Web services feature is based on standard protocols, your Web service applications can communicate with a wide variety of implementations, platforms, and devices. For more information, see Web Services in Managed Code.

You can use Web services to enhance the functionality of Windows Forms. Connecting Windows Forms to Web services is as simple as making calls to Web service methods, which are processed on a server that then returns the results of the method call.

There are two types of Web service methods: synchronous and asynchronous. When synchronous Web service methods are called, the caller waits for the Web service to respond before continuing operations. When asynchronous Web service methods are called, you can continue to use the calling thread while waiting for the Web service to respond. This enables you to use the existing set of threads efficiently in the client application. For more information about working with synchronous and asynchronous Web service methods, see Accessing Web Services in Managed Code.

Synchronous Web Service Methods

A call to a synchronous Web service method involves calling the method and waiting for the computation to occur on the server and return a value before continuing with the rest of the code in the Windows Form.

To create an XML Web service

  1. Create a Web service application. For more information, see Creating Web Services in Managed Code.

  2. In Solution Explorer, right-click the .asmx file and choose View Code.

  3. Create a Web service method that does addition. The following code example shows a Web service method that takes two integers and adds them, returning the sum.

    <WebMethod()> Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer
       Return x + y
    End Function
    
    [WebMethod]
    public int WebAdd(int x, int y)
    {
       return x + y;
    }
    
    /** @attribute WebMethod() */
    public int WebAdd(int x, int y)
    {
       return x + y;
    }
    
  4. Create another Web service method that does multiplication. The following code example shows a Web service method that takes two integers and multiplies them, returning the product.

    <WebMethod()> Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer
       Return x * y
    End Function
    
    [WebMethod]
    public int WebMultiply(int x, int y) 
    {
       return x * y;
    }
    
    /** @attribute WebMethod() */
    public int WebMultiply(int x, int y) 
    {
       return x * y;
    }
    
  5. From the Build menu, choose Build Solution. You can also browse to the .asmx file you created in this project to learn more about Web services. Your Web service is now available for calling from a Windows Form.

To call an XML Web service synchronously

  1. Create a new Windows-based application. For more information, see How to: Create a Windows Application Project.

    Security noteSecurity Note:

    Calls to Web methods require a privilege level granted by the WebPermission class. If you are running in a partial-trust context, the process might throw an exception. For more information, see Code Access Security Basics.

  2. In Solution Explorer, right-click the References node of the project, and then click Add Web Reference.

    The Add Web Reference dialog box opens.

  3. Type the following Web Service URI in the Address box, and then press ENTER:

    https://localhost/WebService1/Service1.asmx
    

    This is the URI of the Web Service you created in the previous procedure. The WebAdd and WebMultiply Web methods appear in the left pane of the Add Web Reference dialog box.

  4. Click Add Reference.

  5. From the Toolbox, add three TextBox controls and two Button controls. The text boxes will be for the numbers, and the buttons will be used for the calculations and to call the Web service methods.

  6. Set the properties of the controls in the following way:

    Control

    Property

    Text

    TextBox1

    Text

    0

    TextBox2

    Text

    0

    TextBox3

    Text

    0

    Button1

    Text

    Add

    Button2

    Text

    Multiply

  7. Right-click the form and choose View Code.

  8. Create an instance of the Web service as a member of the class. You need to know the name of the server where you created the Web service previously.

    ' Replace localhost below with the name of the server where
    ' you created the Web service.
    Dim MathServiceClass As New localhost.Service1()
    
    localhost.Service1 MathServiceClass = new localhost.Service1();
    
    localhost.Service1 MathServiceClass = new localhost.Service1();
    
  9. Create an event handler for Button1's Click event. For details, see How to: Create Event Handlers Using the Designer.

    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 method from the instance of the Web service.
       z = MathServiceClass.WebAdd(x, y)
       TextBox3.Text = z.ToString
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
    // 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);
    // Call the WebAdd Web service method from the instance of the Web service.
       z = MathServiceClass.WebAdd(x, y);
       textBox3.Text = z.ToString();
    }
    

    (Visual C#) Place the following code in the form's constructor to register the event handler.

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    private void button1_Click (Object sender, System.EventArgs e)
    {
       // Create instances of the operands and result.
       int x, y, z;
       // Parse the contents of the text boxes into integers.
       x = Integer.parseInt(textBox1.get_Text());
       y = Integer.parseInt(textBox2.get_Text());
       // Call the WebAdd Web service method from the instance of the Web service.
       z = MathServiceClass.WebAdd(x, y);
       textBox3.set_Text(""+z);
    }
    
  10. Create an event handler for Button2's Click event in the same way, and add the following code.

    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)
    ' Call the WebMultiply Web service method from the instance of the Web service.
       z = MathServiceClass.WebMultiply(x, y)
       TextBox3.Text = z.ToString
    End Sub
    
    private void button2_Click(object sender, System.EventArgs e)
    {
    // 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);
    // Call the WebAdd Web service method from the instance of the Web service.
       z = MathServiceClass.WebMultiply(x, y);
       textBox3.Text = z.ToString();
    }
    

    (Visual C#) Place the following code in the form's constructor to register the event handler.

    this.button2.Click += new System.EventHandler(this.button2_Click);
    
    private void button2_Click (Object sender, System.EventArgs e)
    {
       // Create instances of the operands and result.
       int x, y, z;
       // Parse the contents of the text boxes into integers.
       x = Integer.parseInt(textBox1.get_Text());
       y = Integer.parseInt(textBox2.get_Text());
       // Call the WebAdd Web service method from the instance of the Web service.
       z = MathServiceClass.WebMultiply(x, y);
       textBox3.set_Text(""+z);
    }
    
  11. Press F5 to run your application.

  12. Enter values into the first two text boxes. When you press the Add button, the third text box should show their sum. When you press the Multiply button, the third text box should show their product.

    Note

    The first call to a Web service takes some time for the server to process, because the Web service is instantiated on the server. Keep this in mind when you press the buttons in your application. This lag time is dealt with in the following section.

Next Steps

When you call asynchronous Web service methods, the application continues to run while waiting for the Web service to respond. This enables you to use the resources efficiently in the client application.

For more information, see How to: Access a Web Service Asynchronously in Managed Code.

See Also

Tasks

How to: Bind to a Web Service Using the Windows Forms BindingSource

Concepts

Choosing Between Windows Forms and Web Forms

Web References in Visual Studio

How to: Generate a Web Service Proxy

Other Resources

Accessing Web Services in Managed Code

Web Services in Managed Code