Creating a Web Service for NewsShop - XML

You can create a Web service for NewsShop by using the ASP.NET Web services template that Visual Studio .NET provides. Creating a Web service involves adding Web methods to the Web service. To do so, add the following Web methods to the Service1.asmx file:

  • GetCategoryList()
  • GetArticleList()

The GetCategoryList() Web method returns a list of categories, and the GetArticleList() Web method returns a list of articles from the NewsShop database. To create the Web methods, add the following code to the Service1 class:

The GetCategoryList() method accepts no parameters and returns a list of all categories for which resources are available. Next, add the code for the GetArticleList() Web method to the Service1 class, as follows:

As you can see, the GetArticleList() Web method accepts three parameters: the category for which the articles will be returned, the username, and password for a registered user. If the username or the password that a user enters is not valid, the Web method returns an exception. You will learn about the exception that is generated in the section, "Customizing SOAP Faults."

Because the username and password are passed as text to the Web method, it is advisable that you secure the server using SSL.

After adding the code for the Web method, you need to implement the code. You can implement the GetCategoryList() method by using the following code:

The previous code contains the declaration for the SqlConnection, DataSet, and SqlDataAdapter objects. Next, an instance of the SqlConnection object, Conn, is created. Conn is used to connect to the NewsShop database.

After a connection to the database is made, an instance of the SqlDataAdapter object is created to query the Category table. The value that the Category table returns is stored in the DataSet object, which is then converted to XML and returned to the requesting application. This conversion is done so that the data can be returned to a client as a string containing XML, which the client can then parse and use.

Similarly, the implementation of the GetArticleList() Web method is as shown:

The code contains the declaration of the objects that are used in the code, such as DataSets, SqlConnection, and SqlDataAdapters. The method then connects to SQL Server and queries for the CategoryType field to identify whether the specified category is free or paid.

If the category is free, the method queries the ArticleList view and returns the required rows for the specified category. Alternatively, if the category is paid, the method verifies the username and the password that are passed with the request. To validate the username and the password, the method uses the ValidateLogin stored procedure.

If the username and the password are valid, the method retrieves the list of articles from the ArticleList view for the required category. Finally, the returned result is stored in a dataset, which is then converted into XML and returned to the calling application.

Customizing SOAP Faults
When you send a request for a Web service, an error might be generated. To handle application-level errors and return a customized error message to a client, you use the SoapException class.

For example, consider the following code that validates a username and a password. To do this, the code calls the stored procedure, which returns a user ID if the username and the password are validated. However, if the username and the password are not validated, the code throws the exception, Soap Exception, as shown:

If the username and password are not validated, then the error message This is a paid category. Please login with your user name and password is displayed, as shown in Figure.

The Error Message Displayed When the Username and Password are Not Validated.

The Error Message Displayed When the Username and Password are Not Validated.

As you know, you can send a request for a Web service in the form of a SOAP request. Therefore, the parameters to the Web methods, such as the username and the password, are sent as part of the body of the SOAP request. Sometimes, it is convenient to use the header of a SOAP message to pass some values to and from the Web service. The following section discusses using the Header element of the SOAP message to pass data to the Web service.

SOAP Header

SOAP headers provide a convenient way to store data (such as usernames and passwords), encryption information (such as keys), and other information related to the Web service. Visual Studio .NET provides attributes and classes that help in the processing of SOAP headers. These classes include SoapHeaderAttribute and SoapHeader. In addition, the SoapHeader class allows you to access the contents of the SOAP header. An example of a SOAP header is shown here:

The preceding code contains the SoapHeader attribute that contains the Direction property. This property specifies whether the client application or the Web service will process the SOAP header. In addition, the SoapHeader attribute has the Required property, which specifies whether it is mandatory for the client application or the Web service to process the SOAP header. This property takes a Boolean value. If the value of this property is True, the application needs to process the SOAP header element, and if the value is False, it is not mandatory for the application to process the Header element.

You will now create the GetArticleList() method that uses SOAP headers to pass the username and password. To do so, open the Service1. asmx. vb file and add the following class declaration:

To pass the parameters to a Web method as part of a SOAP message, you need to create a class, Header1, and declare two public string variables, username and password, in it. However, to implement this class in the Service1 class, create an instance of the Header1 class in Service1, as shown in the following code:

Public InHeader As Header1

Now, make the highlighted changes to the GetArticleList() method in the Service1 class:

The preceding code uses the MessageName property of the WebMethod attribute to provide an alias to the method. Next, the code assigns a value SoapHeaderDirection.In to the Direction property of the SoapHeader attribute. Doing this enables the Web method to process the SOAP header that contains the data. In addition, the Required property is set to False because the Web service is not required to process the SOAP header with free categories.

Finally, the Web service accesses the SOAP header element to retrieve the data, as shown in the following code snippet:

After you create the Web service, you need to create client applications that access the Web service. We will now create a client application by using Visual Studio .NET.

Accessing Web Services from Client Applications

Visual Studio .NET makes it easy to call a Web service from any kind of application. Using Visual Studio .NET, we will create a client application that you can use to test the Web service. To do so, perform the following steps:

  1. Open the project for NewsShop.
  2. Create a Visual Basic Windows Application project.
  3. In the Name text box, assign a name to the Windows application, and in the Location text box, type or browse for the location where you want to save the application. Select the Add to Solution option button to ensure the new project is added to the same solution.
  4. Tip?/td>
    It's best to save the Windows application in the same folder as the NewsShop Web service.

  5. Click on the OK button. The Windows application is added to the same solution, as shown in Figure.

The NewsShop Project Solution.

The NewsShop Project Solution

Now, you need to add a Web reference for the Web service to theWindows application. To do so, perform the following steps:

  1. Right-click on the Windows application project in the Solution Explorer window.
  2. In the displayed list, select the Add Web Reference option. The Add Web Reference dialog box is displayed, as shown in Figure.
  3. The Add Web Reference Dialog Box.

    The Add Web Reference Dialog Box.

  4. In the Address text box, enter the path for the WSDL of the Web service you created earlier as http://localhost/NewsShop/service1.asmx?wsdl and press Enter. The WSDL document will be displayed.
  5. Click on the Add Reference button.

Visual Studio .NET creates a proxy class. The following section discusses proxy classes in detail.

Proxy Class

A proxy class is similar to a Web service class. When you add a Web reference to a client application, Visual Studio .NET creates a proxy class and adds it to the project. The proxy class is created in the Web References folder.

To view the structure of the proxy class, click on the Class View option in the Solution Explorer window and expand the tree structure. The tree structure contains proxy classes for each of the classes in the original Web service. The tree structure of the proxy class is shown in Figure.

The Tree Structure of the Proxy Class.

The Tree Structure of the Proxy Class

The Web References folder contains several folders that have references to Web services. The names of these folders depend on the location where the Web service is hosted. If the Web service is hosted on the local server, the name of the folder is LocalHost. This folder contains a Disco file, a WSDL file, and a proxy class file, Service1.vb.

The code for the proxy class is as shown:

The preceding code contains declarations for the Service1 class and the Header1 class. In addition, the code contains declarations to call each of the methods in two ways:

synchronously and asynchronously. The code to call the GetArticleList() method synchronously is shown here:

The previous code creates an instance, ws1, of the NewsService class and calls the GetArticleList() method by using ws1.

Testing the Web Service from a Client Application

After you have added a proxy for the Web service class to the project, you can instantiate the proxy class and call the Web methods in the Web service. To do this, add a Button control to the blank form in the Windows application. Now, add the following code to the Click event of the Button control:

After you add a button to the Windows form, run the application, and click on the button to test the Web service.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

XML Topics