Building And Consuming A Simple XML Web Service - Part 2

In this article you will learn to Create a Web service and client, Working with the client Proxy, Deploying a Web Service and Configuring the Client, Debugging and Exception Handling in Web Services.

Before reading this article, I highly recommend reading the previous part of the series:

Agenda:

Working with the Client Proxy

Question: What is client proxy?

Answer: A Web proxy client is a client application or computer that sends requests to the TCP port on which ISA Server listens for outgoing Web requests from the network in which the client computer resides. By default, ISA Server listens for outgoing Web requests from the clients in the internal network on port 8080. Web proxy clients are typically Web browser applications that comply with HTTP 1.1 and are configured to send Web requests to an ISA Server computer.

                                                            --msdn

WEb Proxy

Task 1: Modify the Web method attributes

  1. In Solution Explorer, in the CurrencyService project, expand the App_Code folder, and then do one of the following:

    • If you are working with Visual Basic, double-click the Service.vb file.
    • If you are working with Visual C#, double-click the Service.cs file.

      Locate the IntegerSum method.

  2. Modify the WebMethod attribute for the IntegerSum method, specifying values for the MessageName and Description properties. Modify the WebMethod attribute as in the following:

    1. If you are working with Visual Basic:
      1. <WebMethod(MessageName:="AddInt", _  
      2. Description:="Add two numbers")> _  
    2. If you are working with Visual C#:
      1. [WebMethod(MessageName="AddInt",   
      2. Description="Add two numbers")]  
  3. On the Build menu, select Build Solution.

  4. In Solution Explorer, in the CurrencyService project, right-click the Service.asmx page, and then click View in Browser.

    Your new values for MessageName and Description will appear on the test page.

  5. Click the Address bar, type http://localhost:port/CurrencyService/Service.asmx?WSDL, and then press ENTER. Replace port with the port number identified in Exercise 1, task 3.

    The IntegerSum element is no longer present but has been changed to AddInt. The WSDL has changed because the message format has changed. The service now handles the call to the IntegerSum method with a SOAP element called AddInt.

  6. Close Internet Explorer.

  7. Press F5

  8. In the Test Form for Currency Service dialog box, type a number into the First Number text box, type a number into the Second Number text box, and then click Add.

    When you attempt to call the IntegerSum method, the application may fail with a SoapException. This is because the client proxy may not have been updated, and if so it will not be aware of the change to the SOAP message format caused by changing the WebMethod attribute. In some circumstances, the client proxy may be refreshed automatically by Visual Studio, although this behavior is somewhat unpredictable. The following steps in this task assume that the proxy was not automatically refreshed, and the exception occurred.

  9. Close the SoapException was unhandled dialog box, and then stop debugging the application.

  10. To fix this problem, in Solution Explorer, in the CurrencyClient project, expand the Web References folder.

  11. Right-click the Currency entry, and then click Update Web Reference.

    This will regenerate the source code files for the client proxy.

  12. Press F5.

  13. In the Test Form for Currency Service dialog box, type a number into the First Number box, type a number into the Second Number box, and then click Add.

  14. Verify that it now works, and then close the application.

Task 2: Create a proxy class manually

  1. On the Project menu, click Show All Files.

    You can see the files that Visual Studio creates automatically, including those for the Web reference.

  2. In Solution Explorer, in the Web References folder, expand the Currency Web reference, and then double-click the Service.wsdl file.

  3. On the File menu, click Save Web References\Currency\Service.wsdl As.

  4. In the Save File As dialog box, browse to E:\Labfiles.

  5. Click the File name text box, type currency.wsdl, and then click Save.

  6. Click Start, point to All Programs, point to Microsoft Visual Studio 2005, Visual Studio Tools, and then click Visual Studio 2005 Command Prompt.

  7. At the command prompt, type E: and then press ENTER.

  8. At the command prompt, type cd Labfiles and then press ENTER.

  9. At the command prompt:

    • If you are working with Visual Basic, type wsdl /language:vb /out:currency.vbcurrency.wsdl and then press ENTER.
    • If you are working with Visual C#, type wsdl /language:cs/out:currency.cscurrency.wsdl and then press ENTER.

  10. Click Start, point to All Programs, point to Accessories, and then click Windows Explorer.

  11. Browse to E:\Labfiles, right-click the currency.vb or currency.cs file (depending on the language chosen), point to Open With, and then click Notepad to view the file you have just generated.

  12. Close Notepad and Microsoft Windows® Explorer.

Deploying a Web Service and Configuring the Client

Task 1: Create a Web Setup project and deploy the Web service to IIS.

  1. Return to Visual Studio.

  2. On the File menu, point to Add, and then click New Project.

  3. In the Add New Project dialog box, in the Project types pane, expand Other Project Types, and then click Setup and Deployment.

  4. In the Templates pane, click Web Setup Project.

  5. Click the Name text box, and then type CurrencyService_Install.

  6. Click the Location drop-down box, type E:\Labfiles and then click OK.

  7. In Solution Explorer, right-click the CurrencyService_Install project, point to Add, and then click Project Output.

  8. In the Add Project Output Group dialog box, select Project list, and click CurrencyService.

  9. In the project list, click Content Files, and then click OK.

    The Setup project is now configured to install all the content files for the Web service.

  10. On the Build menu, click Build CurrencyService_Install.

    This will generate the files Setup.exe and CurrencyService_Install.msi in the folder E:\Labfiles\CurrencyService_Install\Debug.

  11. Click Start, point to All Programs, point to Accessories, and then click Windows Explorer.

  12. Browse to E:\Labfiles\CurrencyService_Install\Debug, and then double-click the CurrencyService_Install.msi file.

    The CurrencyService_InstallWizard appears.

  13. On the Welcome to the CurrencyService_Install Setup Wizard page, click Next.

  14. On the Installation Address page, click the Virtual directory text box, type CurrencyService and then click Next.

  15. On the Confirm Installation page, click Next.

  16. On the Installation Complete page, click Close.

  17. Click Start and then click Internet Explorer.

  18. Click the Address bar, type http://LON-DEV-01/CurrencyService/Service.asmx, and then press ENTER to verify that the service has been successfully installed. Close Internet Explorer.

    (Labfiles is already attached with previous part of our article)

Task 2: Update the client application to use the deployed Web service on IIS

  1. Return to Visual Studio.

  2. In Solution Explorer, in the CurrencyClient project, click Web References folder, right-click the Currency Web reference, and then click Delete. In the dialog box, click OK.

  3. In Solution Explorer, in the CurrencyClient project, right-click the Web References folder, and then click Add Web Reference.

  4. In the Add Web Reference dialog box, on the Start Browsing for Web Services page, click the Web services on the local machine link.

  5. On the Web Services and on the Local Machine page, in the Services column, click the first Service link.

  6. In the Web reference name text box, type localhost with Currency and then click Add Reference.

  7. Press F5 to run the client application. Type two numbers in the First Number and Second Number text boxes, and then click Add.

    The application will now work using the deployed version of the service on the Internet Information Services (IIS). If you make subsequent changes to the Web service, remember that you must rebuild the installation project. Copy the installation executable to the target server and run them to install the updated Web service.

  8. Close the application.