Building And Consuming A Simple XML Web Service - Part 1

Agenda:

  • Creating 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.

Question: What are Web Services?

Answer: Just try with some google search about definition and you will get an answer and if you want know how to do it so here's the solution.

Creating a Web Service and Client

Task 1: Create a Web service Web site

  1. Click Start, point to All Programs, point to Microsoft Visual Studio 2008 or Later, and then click Microsoft Visual Studio.

    If prompted to choose your default environment settings, select General Development Settings, and then click Start Visual Studio.

  2. On the File menu, point to New, and then click Web Site.

  3. In the New Web Site dialog box:

    1. In the Templates pane, click ASP.NET Web Service.
    2. In the Location drop-down box, type E:\Labfiles\CurrencyService.
    3. In the Language list, click the language of your choice (either Microsoft Visual Basic® or Microsoft Visual C#), and then click OK.

  4. In Solution Explorer, click the CurrencyService web site.

  5. If you are working with Visual C#, on the Website menu, click Add New Item. In the Add New Item dialog box:

    1. In the Templates pane, click Web Configuration File.
    2. In the Name text box, ensure that the name of the file to add is set to Web.config and then click Add.

    Note that if you are working with Visual Basic the Web site will already contain a Web Configuration file, so this step is not necessary.

  6. In Solution Explorer, 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.

  7. In the WebService attribute, change the Namespace default value http://tempuri.org/ to http://www.adventure-works.com.

  8. Select the HelloWorld method and the WebMethod attribute, and then press DELETE.

  9. In Solution Explorer, right-click the App_Code folder, and then click Add Existing Item.

  10. In the Add Existing Item dialog box:

    • If you are working with Visual Basic, browse to E:\Labfiles\Starter\VB, and then double-click the DataHelper.vb file.
    • If you are working with Visual C#, browse to E:\Labfiles\Starter\CS, and then double-click the DataHelper.cs file.

  11. In Solution Explorer, double-click the Web.config file.

  12. Replace the <connectionStrings/> element with the following code:
    1. <connectionStrings>  
    2.    <add name="adventureworks" connectionString="Data Source=lon-dev-01;Initial Catalog=AdventureWorks;Integrated Security=True"/>  
    3. </connectionStrings>  

  13. In the Web.config file, select the <compilation> element and replace the value false with the value true.

    This enables debugging for the Web service. If you omit this step, the Microsoft Visual Studio development environment will offer to make the change for you the first time you run the service using the debugger.

  14. In Solution Explorer:

    • 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.

  15. Enter the following code inside the class definition after the constructor:

    • If you are working with Visual Basic:
      1. <WebMethod()> _  
      2. Public Function IntegerSum( _  
      3.    ByVal a As Integer, ByVal b As Integer) As Integer  
      4.   
      5.    Return a + b  
      6. End Function

    • If you are working with Visual C#:
      1. [WebMethod]  
      2. publicintIntegerSum(int a, int b)  
      3. {  
      4.    return a + b;  
      5. }

  16. Enter the following code inside the class definition:

    • If you are working with Visual Basic:
      1. <WebMethod()> _  
      2. Public Function CurrencyForCountry( _  
      3.    ByValcountryRegionCode As String) As String  
      4.   
      5.    Return DataHelper.GetCurrencyForCountry(countryRegionCode)  
      6. End Function  

    • If you are working with Visual C#:

      1. [WebMethod]  
      2. public string CurrencyForCountry(string countryRegionCode)  
      3. {  
      4.    returnDataHelper.GetCurrencyForCountry(  
      5.    countryRegionCode);  
      6. }  

  17. In the Solution Explorer, select the Service.asmx file.

  18. Press F5.

    Microsoft Internet Explorer starts and displays a page containing information about the service. Use this page to test the Web service methods.

    • Click the CurrencyForCountry link.
    • In the CurrencyForCountry Test page, type US for the countryRegionCode and then click Invoke.
    • Verify that a second Internet Explorer window opens, displaying the following XML text:
      1. <?xml version="1.0" encoding="utf-8" ?>  
      2. <string xmlns="http://www.adventure-works.com">US Dollar</string>  

  19. Close both Internet Explorer windows.

Task 2: Add a Windows Forms client project

  1. In Solution Explorer, right-click solution ‘CurrencyService’, point to Add, and then click Existing Project.

  2. In the Add Existing Project dialog box:

    • If you are working with Visual Basic, browse to the E:\Labfiles\Starter\VB\CurrencyClient folder, and then double-click the CurrencyClient project file.
    • If you are working with Visual C#, browse to the E:\Labfiles\Starter\CS\CurrencyClient folder, and then double-click the CurrencyClient project file.

  3. In Solution Explorer, right-click the CurrencyClient project, and then click Set as StartUp Project.

  4. In Solution Explorer, right-click the CurrencyClient project, and then click Add Web Reference.

  5. In the Add Web Reference dialog box, on the Start Browsing for Web Services page, click the Web services in this solution link.

  6. On the Web Services in this Solution page, click the Service link.

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

  8. In Solution Explorer, in the CurrencyClient project, double-click the DemoForm form, and then familiarize yourself with its contents.

  9. On the View menu, click Code.

  10. Locate the AddTwoNumbers_Click method, and replace the comment in this method with the following code:

    • If you are working with Visual Basic:
      1. Using svc As New Currency.Service()  
      2.    svc.UseDefaultCredentials = True  
      3.    c = svc.IntegerSum(a, b)  
      4. End Using  

    • If you are working with Visual C#:

      1. using (Currency.Service svc = new Currency.Service())  
      2. {  
      3.    svc.UseDefaultCredentials = true;  
      4.    c = svc.IntegerSum(a, b);  
      5. }  

      Setting the UseDefaultCredentials property to true will ensure that the requests made by this proxy will be authenticated using the credentials of the currently logged-on user.

  11. Press F5 to run the project. Type two numbers into the First Number and Second Number text boxes, and then verify that you can use the Add button to add two integers and display the results.

    There may be a small delay as the Web service is loaded and compiled for the first time, but subsequent calls will be quite fast.

  12. Close the application.

  13. Locate the GetCurrency_Click event handler and replace the comment in this method with the following code:

    • If you are working with Visual Basic:
      1. Using svc As New Currency.Service()  
      2.   
      3. svc.UseDefaultCredentials = True  
      4. currencyName = svc.CurrencyForCountry(countryCodeText)  
      5.   
      6. If currencyNameIs Nothing Then  
      7. MessageBox.Show(_ String.Format(_ "Currency name could not be determined for country"  
      8.     _ & " code '{0}'", _ countryCode) _)  
      9. Else  
      10.   
      11. MessageBox.Show(_ String.Format("The currency for country code '{0}'"  
      12.     _ & " is '{1}'", _ countryCode, _ currencyName) _)  
      13. End If  
      14.   
      15. End Using  
    • If you are working with Visual C#:
      1. using(Currency.Service svc = new Currency.Service()) {  
      2.     svc.UseDefaultCredentials = true;  
      3.     currencyName = svc.CurrencyForCountry(countryCode);  
      4.   
      5.     if (currencyName == null) {  
      6.         MessageBox.Show(  
      7.             String.Format(  
      8.                 "Currency name could not be determined for country" + " code '{0}'",  
      9.                 countryCode)  
      10.         );  
      11.     } else {  
      12.         MessageBox.Show(  
      13.             String.Format("The currency for country code '{0}'" + " is '{1}'",  
      14.                 countryCode,  
      15.                 currencyName)  
      16.         );  
      17.     }  
      18. }  
  14. Press F5 to run the project. Type US for the Country Code, click Get Currency, and verify that a message box appears containing the text US Dollar.

Task 3: Examine Web service discovery features

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

    Internet Explorer displays the Web service test page. By default, when the Microsoft ASP.NET development server runs a Web service, it allocates a port number dynamically.

  2. Examine the start page Uniform Resource Locator (URL), and then make a note of the port number.

  3. Click the Address bar, type http://localhost:port/CurrencyService/Service.asmx?DISCO, and then press ENTER. Replace port with the port number determined in the previous step.

    Verify that this action generates and displays the discovery document for the Web service.

  4. Click the Address bar, type http://localhost:port/CurrencyService/Service.asmx?WSDL, and then press ENTER. Replace port with the port number as before.

  5. Verify that this action shows the Web Services Description Language (WSDL) entry for the service. The WSDL page is an XML document that describes the various methods and parameters of the service.

  6. Close Internet Explorer.


Similar Articles