Advantages of Creating a Web Service by Using the ATL Server Library XML

As you know, a Web service is a way for a Web site to expose programmatic functionality. Web services accept input in the form of messages and respond through messages. You can send and receive these messages by using HTTP-POST, HTTP-GET, or SOAP. Creating a Web service by using the ATL Server is not as simple and easy as doing it in any of the .NET languages, such as Visual Basic .NET or Visual C#. However, there are some reasons why a developer might use the ATL Server library to create Web services. The following list shows some of the reasons so that you can appreciate the effort required to create Web services by using the ATL Server library:

  • Create high-performance Web services. The most important reason for using the ATL Server library to create Web services is that ATL Server Web services offer high performance.
  • Use classes in the ATL Server library. To create ATL Server Web services, you can use the classes that the ATL-based libraries provide. These classes make the work easier for developers.

Creating a Web Service for GW by Using the ATL Server Library

Creating a Web service for GW involves creating the database and the stored procedures in SQL. After you create the database and stored procedures, you will create the Web service.

Creating the Database for GW

In your local installation of SQL Server, create a database and the following tables:

  • Products table. The Products table contains the fields shown in the Figure. The CategoryID field has a foreign key relationship with the CategoryID field of the Category table. The ManufID field has a foreign key relationship with the ManufID field of the Manufacturer table, and the SupplierID field has a foreign key relationship with the SupplierID field of the Supplier table.
  • The Products Table.

    Creating the Database for GW

  • Category table. The Category table contains the fields shown in Figure. This table stores data on various categories of products that are available at the store. In addition, the table contains the descriptions of the respective categories.
  • The Category Table

    The Category Table

  • Manufacturer table. The Manufacturer table stores the information about the manufacturers of the products, as shown in Figure.
  • The Manufacturer Table

    The Manufacturer Table

  • Supplier table. The Supplier table contains the details of the suppliers of various products, as shown in Figure.
  • The Supplier Table

    The Supplier Table

  • Orders table. The orders that are placed on the Web site and through the Web services are stored in this table. The table has foreign key relationships with the Products table and the Customer table. The Orders table is shown in Figure.
  • The Orders Table

    The Orders Table

  • Customer table. The Customer table contains the details of the customers who are registered with the site. In addition, the table contains details about the members of the associate program. The table has an integer field, associate, which is set to 1 for storing the details of an associate program member. The details of the Customer table are shown in Figure.
  • The Customer Table

    The Customer Table

The relationship between these tables is shown in Figure.

The Relationship between the Tables of the GW Database

The Relationship between the Tables of the GW Database

After you create the database with the previously discussed tables, you will create the stored procedures as discussed in the following section.

Creating the Stored Procedures for GW

As discussed earlier, the business solution for GW will contain three Web services, and the stored procedures will provide the data that the Web services require. You will now create these stored procedures.

TheGetFeaturedProduct Stored Procedure

The GetFeaturedProduct stored procedure returns the details of the featured product of the month. The code of the stored procedure is as shown:

The GetProductDetail Stored Procedure

The GetProductDetail stored procedure accepts a product ID from the Web service and returns the corresponding product details from the Products table. The code of the stored procedure is as follows:

The PlaceOrder Stored Procedure

The PlaceOrder stored procedure accepts the data for an order that a customer places and then inserts a record for the order into the Orders table. The code for the PlaceOrder stored procedure is as follows:

Creating the ATL Server Web Service

After you create the database and the stored procedures, you will create the Web service. To create a Web service by using the ATL Server library, perform the following steps:

  1. Start Visual Studio .NET and create a new Visual C++ project.
  2. Select ATL Server Web Service as the project type and specify the project name as GizmoWS.
  3. Click on the OK button. Figure shows the New Project dialog box.

The New Project Dialog Box

The New Project Dialog Box

When you click on the OK button, Visual Studio .NET launches the ATL Server Project Wizard. The ATL Server Project Wizard appears as shown in Figure.

The Overview Page

The Overview Page

After you click on the Finish button in the ATL Server Project Wizard, Visual Studio .NET creates a HelloWorld Web service and opens it in the design view. In addition, Visual Studio .NET creates several files for the project and adds them to the Solution Explorer window. Figure shows the Solution Explorer window for a Web service.

The Solution Explorer Window

The Solution Explorer Window

As you can see, Visual Studio .NET creates two projects as listed:

  • GizmoWS.GizmoWS is the Web service that the ATL Server Project Wizard creates.
  • GizmoWSIsapi. GizmoWSIsapi is the ISAPI extension DLL for the ATL Server project.

You are not required to make any modifications to the ISAPI Extension DLL project. Therefore, I will discuss only the GizmoWS project in detail. Table discusses the files that the ATL Server Project Wizard creates for the GizmoWS project.

Default Files for the GizmoWS Project

Default Files for the GizmoWS Project

Note?/td>
As discussed, the GizmoWS.htm file contains the hard-coded description of the Hello World Web service. The information about this Web service does not change when you add more operations to the Web service or delete the HelloWorld Web service. Therefore, when you deploy the Web service, you will find a description of the Hello World Web service, even if it is not functional.

At this point, we will discuss the contents of the GizmoWS.h file, which contains the functionality of the Web service. The code of the Web service is as follows:

In the preceding code, the [soap_method] attribute indicates to the compiler that the method is for an operation that a Web service exposes. In addition, the parameters that are sent to the Web service or are returned by the Web service are encoded in the form of a SOAP message.

The preceding code is the default code for the Web service. To create your own Web service, you need to add Web methods to the Web service. The Web methods to be added are discussed in the following list:

  • GetFeaturedProduct(). The GetFeaturedProduct() method returns the details of the Featured product of the month.
  • GetProductDetail(). The GetProductDetail() method accepts a product ID from an associate member and returns the corresponding product details.
  • PlaceOrder(). The PlaceOrder() method stores an order that a customer places for a product.

Before you add the code for the Web methods, you need to make interface declarations for these methods. The interface declarations for the methods are shown in the following code snippet:

After you add the interface declarations for the methods, add the code for the Web methods in the GizmoWS.h file:

The preceding methods need to interact with the database. For example, when a user places an order, the PlaceOrder() Web method stores the details of the order in the Orders table. Consider another situation in which an associate member requests the product details; the GetProductDetail() Web method needs to retrieve the details of the requested product from the Products table and expose it to the Web service.

To allow your application to interact with a database, you can use the ATL OLE DB Consumer Wizard. Using this wizard greatly simplifies the ability to access databases from Visual C++ applications. To run the ATL OLE DB Consumer Wizard, perform the following steps:

  1. Right-click on the Web service project name in the Solution Explorer window.
  2. Select Add, Add Class from the resulting menus. The Add Class dialog box appears as shown in Figure.
  3. The Add Class Dialog Box

    The Add Class Dialog Box

  4. In the dialog box, select the ATL OLE DB Consumer option.
  5. Click on the Open button. This launches the ATL OLE DB Consumer Wizard. The first page of the wizard is shown in Figure.
  6. The ATL OLE DB Consumer Wizard

    The ATL OLE DB Consumer Wizard

  7. Click on the Data Source button. The Data Link Properties dialog box is displayed, as shown in Figure.
  8. The Data Link Properties Dialog Box

    The Data Link Properties Dialog Box

  9. In the displayed list, select the Microsoft OLE DB Provider for SQL Server option.
  10. Click on the Next button. In the Connection dialog box, enter the details for connecting to the SQL Server. The Connection dialog box is shown in Figure.
  11. The Connection Page of the Data Link Properties Dialog Box

    The Connection Page of the Data Link Properties Dialog Box

  12. Select the name of your SQL server and the authentication type as appropriate to your local SQL server installation. Also, select the name of the database you created. To validate the connection string, click on the Test Connection button. A message confirming the successful connection of the database is displayed.
  13. Click on the OK button. The Select Database Object dialog box appears, as displayed in Figure.
  14. The Select Database Object Dialog Box

    The Select Database Object Dialog Box

  15. Expand the tree for the stored procedures and select the GetFeatureProduct stored procedure.
  16. Click on the OK button. This will bring you back to the ATL OLE DB Consumer Wizard dialog box. This dialog box now has the names for the files added to it. If required, you can change the names of the files and the classes. The ATL OLE DB Consumer Wizard dialog box is shown in Figure.
  17. The ATL OLE DB Consumer Wizard Dialog Box

    The ATL OLE DB Consumer Wizard Dialog Box

  18. Click on the Finish button. The wizard adds a header file, GetFeaturedProduct.h, to the project.
  19. To view the contents of the GetFeaturedProduct.h file, open the file. The file contains the following code:

As you can see, the CGetFeaturedProduct class encapsulates the GetFeaturedProduct stored procedure. To do this, the CGetFeaturedProduct class contains variable declarations for each of the values that the stored procedure returns. For example, for the Name field in the Products table, the class has the following code:

Similarly, the ATL OLE DB Consumer Wizard creates a class for encapsulating the GetProductDetail stored procedures. This class is contained in the GetProductDetail.h file as shown:

The CGetFeaturedProduct and CGetProductDetail classes are contained in the GetFeaturedProduct.h and GetProductDetail.h files, respectively. To use these files in your Web service, you need to include them in the Gizmo.h file, as shown in the following code snippet:

The next step in the creation of the Web service is to modify the GetFeaturedProduct() and GetProductDetail() methods. These methods are similar, but the GetProductDetail() method accepts the ID of the product whose details are to be retrieved. To use this method in your Web service, you need to create an instance of the CGetFeaturedProduct class, which encapsulates the stored procedure. The code for creating an instance, p, of the GetFeaturedProduct class is as shown:

Next, you need to call the OpenAll() method to invoke the stored procedure. After you call the OpenAll() method, call the MoveFirst() method to store the data in the variables of the GetFeaturedProduct class. For example, the m_Name variable contains the data from the Name field. This data needs to be stored in the variable of the GetFeaturedProduct class. To do this, call the following methods by using the instance, p, of the GetFeaturedProduct class:

When the data becomes accessible, copy the values from the variables of the encapsulating class to the variables of the GetFeaturedProduct() method as shown:

Tip?/td>
Remember that the data in the CgetFeaturedProduct class is an array of TCHARs, whereas the return variables are of the BSTR type.

Now the variables of the GetFeaturedProduct() method contain the values from the encapsulating class. The complete code for the GetFeaturedProduct() method is as shown:

After you add the preceding methods to the Web service, you will create another Web method that allows a customer of the Web service to place an order with GW. To add this method, you need to follow the same procedure as adding the GetFeaturedProduct() and GetProductDetail() methods. Add the interface declaration statement for the PlaceOrder() method as shown:

The PlaceOrder() method accepts the product name, the quantity of items to be purchased, the username, and the password of the customer. Now use the ATL OLE DB Consumer Wizard to create one more class that encapsulates the PlaceOrder stored procedure. The code for the class is shown here:

Now add a PlaceOrder() method to the Web service. Following is the code for the Web method:

The PlaceOrder() method accepts the required parameters and initializes the members of the class, CPlaceOrder, with the corresponding values. The strings that are in the BSTR format need to be converted into TCHAR[]. To do this, add a helper method, Convert(), as shown:

After adding the three Web methods, you're ready to deploy the Web service. To deploy a Web service, perform the following steps:

  1. Select the Build, Build Solution option.
  2. In the Solution Explorer window, select the GizmoWS project.
  3. Select the Build, Deploy option.

Testing the Application

A client application tests a Web service. To create a client application, perform the following steps:

  1. Select File, Add Project, New Project.;
  2. Create a new Windows application project in Visual Basic .NET. Visual Studio .NET adds one more project to the same solution.
  3. In the Windows Application form, add a Button control from the Windows Forms toolbox.
  4. Right-click on the name of the Visual Basic .NET project in the Solution Explorer window and select the Add Web Reference option. The Add Web Reference dialog box is displayed, as shown in Figure. In the Address text box, enter the URL of the Web service WSDL as http://localhost/Gizmo/Gizmo.dll?Handler=GenGizmoWSDL.
  5. The Add Web Reference Dialog Box

    The Add Web Reference Dialog Box

  6. Click on the Add Reference button.

Visual Studio .NET adds a proxy class to the Web service. In addition, some default files are added to the project. You can find these files under the Local host node in the Solution Explorer window. One such file is the Reference.map file, which is a simple XML file that contains the links to the .disco and .WSDL files of the Web service. The contents of the Reference.map file are as shown:

Visual Studio .NET adds a proxy class for the Web service to the Web References folder. In addition to the proxy class, Visual Studio .NET creates a local copy of the WSDL file for the Web service.

Note?/td>
Because the WSDL file is a local copy, it is not updated if you make a change to the Web service. For example, consider a situation in which you need to add another method to the Web service class. The information about this method will not be automatically updated in the WSDL file. To update the information in the WSDL file, you need to update the Web reference.

After you add a Web reference to your project, add a namespace declaration statement, as shown in the following code: Imports WindowsApplication1.localhost

Next, add the following code to the Click event of the Button control to invoke the Web service from the control:

Run the application by selecting the Start option on the Debug menu and check the functioning of the Web service.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics