Consuming Web Services


If you read the last section, then you know the basics of creating a Web Service with the wizard. Of course, there is a lot more to creating robust Web Services. For example, there are issues with how to transfer various data types through the Web using the SOAP protocol. One important issue is the issue of using a Web Service. VS.NET offers a wizard that lets you connect easily to a Web Service.

To use a Web Service:

For this example let's assume you want to create a desktop application that connects to the Web Service without the use of a browser.

  1. Run VS.NET.

  2. Select File > New Project from the menu bar.

  3. Click the Windows Application icon from the New Project dialog.

  4. Enter a name for the Windows Application, CurrencyClient for example, in the Name field and press OK ( Figure 13.21 ).

    Figure 13.21. This is a change for us, but we're writing a Windows Form application to illustrate that you can still have a traditional Windows application (non-Web front end) communicate through the Internet with a service.

    graphics/13fig21.gif

  5. Right-click on the References item in the Solution Explorer and choose Add Web Reference ( Figure 13.22 ).

    Figure 13.22. We normally use the Add Reference option when the DLL is present in the same machine. Web References are used when we want to talk to a Web Service. The process creates what is called a proxy. It is a class that looks just like the class in the Web Service but when you call one of its methods it forwards the call to the Web Service.

    graphics/13fig22.gif

  6. You will see the Add Web Reference dialog ( Figure 13.23 ). In the address field enter the path to the Web Service. For example: http://www.josemojica.com/CurrencyService/CurrencyConverter.asmx

    Figure 13.23. The Web Service provides a mechanism that reports to the wizard what methods it contains so that the wizard can create a client-side proxy that looks like the service.

    graphics/13fig23.gif

  7. Click the Add Reference button. VS.NET will add several files to your application. One of the files is hidden, but contains source for a class with the same name as the class you authored for the service in the previous section.

  8. Write code that creates an instance of the Web Service class and uses it. For example: CurrencyConverter conv = new CurrencyConverter(); ( Figure 13.24 ).

    Figure 13.24 CurrencyConverter is the client-side proxy class that the wizard created. Its full name is CurrencyClient.localhost.CurrencyConverter. CurrencyClient is the name of our client application. Localhost is the name of the Web server. If the server had been www.ibm.com, then the name of the class would have been CurrencyClient.ibm.com.www.CurrencyConverter.
     //using CurrencyClient.localhost; private void cmdConvert_Click(             object sender,             System.EventArgs e) {  CurrencyConverter conv =   new CurrencyConverter();  } 

graphics/tick.gif Tips

  • When you add a Web Reference with the wizard, the wizard creates a file called Reference.cs . It creates a subdirectory under your application directory called Web References, and a subdirectory under that called the same thing as the server name; that's where it puts the References.cs file. You can take a look at it from the Solution Explorer if you click the Show All Files icon ( Figure 13.25 ).

    Figure 13.25. After you add a Web Reference, a few files are created but to view them you have to click on the View All icon at the top of the Solution Explorer window.

    graphics/13fig25.gif

  • The Web Service client class contains a property called Url . This property points to the address of the Web Service at the time you added the Web Reference. It could be that this property will change over time. Figures 13.26 and 13.27 show you how to read this property from the app.config file and set the Url property at runtime. With this code you can modify the Web Service's address at any time by editing the web.config file.

    Figure 13.26 You may recall from other chapters that the web.config file lets you add custom configuration information. You can either define your own section or use the appSettings section to add your own information. In this case we're saving the Url to the Web Service in appSettings.
     <configuration>       <appSettings>             <add key="Url"             value="http://localhost/CurrencyService/             CurrencyConverter.asmx;"/>       </appSettings> </configuration> 
    Figure 13.27 Once we add the information for the Url to the config file reading it is a simple matter of using the ConfigurationSettings.AppSettings property passing the name of the key.
     private void cmdConvert_Click( object sender, System.EventArgs e) {    CurrencyConverter conv = new    CurrencyConverter();  conv.Url = System.Configuration.   ConfigurationSettings.   AppSettings["Url"];  } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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