17.7 Consuming Web Services with a Windows Form Application

 <  Day Day Up  >  

You want to call methods defined in a Web Service from a Windows Form application.


Technique

To use a Web service, you have to add a reference to it, similar to adding a reference to a .NET assembly. After you create your Windows Form application, click on Project, Add Web Reference from the main menu. This example uses a Web Service on the local machine, but you can also access the Microsoft UDDI registry to search for the Web Service you want to use.

Within the Add Web Reference dialog, click on the link to browse Web Services on the local machine. Visual Studio .NET searches within IIS for available Web Services. After clicking on the Web Service you want to use, click on the Add Reference button to add it to your project.

Using the Web Service within your Windows Form code is a matter of simply instantiating an object and calling the methods. You use a class called the proxy class to interact with the Web Service, and it is created within a defined namespace. Use Class View to determine the namespace and class name of the proxy class. Listing 17.6 shows how to use the Lottery Web Service with a Windows Form control. The form itself contains five labels that display five unique random numbers from the Web Service when the user clicks a button on the form.

Listing 17.6 Consuming Web Services with Windows Forms
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace _7_LotteryClient {     public class Form1 : System.Windows.Forms.Form     {         private System.Windows.Forms.GroupBox groupBox1;         private System.Windows.Forms.Label lbl1;         private System.Windows.Forms.Label lbl3;         private System.Windows.Forms.Label lbl4;         private System.Windows.Forms.Label lbl5;         private System.Windows.Forms.Label lbl2;         private System.Windows.Forms.Button btnGo;         private System.ComponentModel.Container components = null;         public Form1()         {             InitializeComponent();         }         protected override void Dispose( bool disposing )         {             if( disposing )             {                 if (components != null)                 {                     components.Dispose();                 }             }             base.Dispose( disposing );         }         private void InitializeComponent()         {             this.groupBox1 = new System.Windows.Forms.GroupBox();             this.lbl1 = new System.Windows.Forms.Label();             this.lbl3 = new System.Windows.Forms.Label();             this.lbl4 = new System.Windows.Forms.Label();             this.lbl5 = new System.Windows.Forms.Label();             this.lbl2 = new System.Windows.Forms.Label();             this.btnGo = new System.Windows.Forms.Button();             this.groupBox1.SuspendLayout();             this.SuspendLayout();         // forms designer generated code removed         }         [STAThread]         static void Main()         {             Application.Run(new Form1());         }         private void btnGo_Click(object sender, System.EventArgs e)         {             localhost.LotteryWebService gen=new localhost.LotteryWebService();             int[] result = gen.GenerateNumbers( 5, 0, 100, false );             lbl1.Text = result[0].ToString();             lbl2.Text = result[1].ToString();             lbl3.Text = result[2].ToString();             lbl4.Text = result[3].ToString();             lbl5.Text = result[4].ToString();         }     } } 

Comments

The recurring pattern throughout this chapter is that Web Services within Visual Studio .NET are handled by Visual Studio with a minimal amount of intervention on your part. Adding a Web reference to your project launches a few steps to integrate the necessary code into your project. When the reference is made, the WSDL document of the Web Service is read in and subsequently added to your project. Additionally, a static discovery file is added to the project. A discovery file, which has a disco extension, contains all the information needed to find the location of the Web Service, entries for the location of the WSDL document and .asmx file, and binding information that is used for SOAP.

The largest function that adding a reference performs is the generation of a proxy class. This proxy class is generated by translating the messages within the WSDL file into corresponding class methods that your client application uses as a proxy to the Web Service. This proxy class is derived from the SoapHttpClientProtocol class defined within the .NET framework, which performs the main communications between your client and the associated Web Service. Furthermore, the proxy class also adds methods that allow you to call Web Service methods asynchronously, which is a topic covered in Recipe 17.9, "Calling Web Methods Asynchronously."

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net