Monday, December 6, 2010

Calling XML Web Services from Windows Forms

A new aspect of Visual Studio is XML Web services, which provides the ability to 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 Web services are based on standard protocols, your Web service applications can communicate with a broad variety of implementations, platforms, and devices. For more information, see XML Web Services in Managed Code.
Web services can be used 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 allows 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 XML 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 XML 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. This following Web service method will take two integers and add them, returning the sum:
    ' Visual Basic
     Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer
       Return x + y
    End Function
    
    // C#
    [WebMethod]
    public int WebAdd(int x, int y)
    {
       return x + y;
    }
  4. Create another Web service method that does multiplication. The following Web service method will take two integers and multiply them, returning the product:
    ' Visual Basic
     Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer
       Return x * y
    End Function
    
    // C#
    [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 application. For more information, see Creating a Windows Application Project.
    Security Note   Calls to Web Methods require a privilege level granted by the System.Net.WebPermisson 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. Add a reference to the Web service created above. For details, see Adding and Removing Web References.
  3. 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.
  4. Set the properties of the controls as follows:
    ControlPropertyText
    TextBox1Text0
    TextBox2Text0
    TextBox3Text0
    Button1TextAdd
    Button2TextMultiply
  5. Right-click the form and choose View Code.
  6. 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 above.
    ' Visual Basic
    ' Replace localhost below with the name of the server where
    ' you created the Web service.
    Dim MathServiceClass As New localhost.Service1()
    
    // C#
    localhost.Service1 MathServiceClass = new localhost.Service1();
  7. Create an event handler for Button1's Click event. For details, see Creating Event Handlers 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 method from the instance of the Web service.
       z = MathServiceClass.WebAdd(x, y)
       TextBox3.Text = z.ToString
    End Sub
    
    // C#
    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# Note   Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    this.button1.Click += new System.EventHandler(this.button1_Click);
  8. Create an event handler for Button2's Click event in the same fashion, 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)
    ' Call the WebMultiply Web service method from the instance of the Web service.
       z = MathServiceClass.WebMultiply(x, y)
       TextBox3.Text = z.ToString
    End Sub
    
    // C#
    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# Note   Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    this.button2.Click += new System.EventHandler(this.button2_Click);
  9. Press F5 to run your application. 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 a while for the server to process, because the Web service is instantiated on the server. Keep this in mind when pressing the buttons in your application. This lag is dealt with in the section below.

Asynchronous Web Services

When you call asynchronous Web service methods, the application continues to run while waiting for the Web service to respond. This allows you to use the resources efficiently in the client application. This is a far more resource-savvy way to implement Web services within your Windows application.
For details, see Accessing an XML Web Service Asynchronously in Managed Code.

No comments:

Post a Comment