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:
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:
The Products Table.
The Category Table
The Manufacturer Table
The Supplier Table
The Orders Table
The Customer Table
The relationship between these tables is shown in Figure.
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:
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
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
As you can see, Visual Studio .NET creates two projects as listed:
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
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:
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:
The Add Class Dialog Box
The ATL OLE DB Consumer Wizard
The Data Link Properties Dialog Box
The Connection Page of the Data Link Properties Dialog Box
The Select Database Object Dialog Box
The ATL OLE DB Consumer Wizard Dialog Box
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:
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:
Testing the Application
A client application tests a Web service. To create a client application, perform the following steps:
The Add Web Reference Dialog Box
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.
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.
XML Related Interview Questions
|Soap Tool Interview Questions||HTML Interview Questions|
|PHP Interview Questions||ASP.NET Interview Questions|
|PHP5 Interview Questions||Java Interview Questions|
|CSS Interview Questions||XSLT Interview Questions|
|Java XML Interview Questions||XMLHttpRequest (XHR) Interview Questions|
|ebXML Interview Questions||XML DOM Interview Questions|
|XML-RPC Interview Questions||XSD Interview Questions|
|Soap Web Services Interview Questions||XSL Interview Questions|
|Xml Publisher Interview Questions|
Basics Of Xml
Basics Of Web Services
Introduction To Soap
Introduction To Uddi
Introduction To Wsdl
Creating A Web Service Using The Microsoft Soap Toolkit
Building Web Applications On The .net Platform
Creating An Asp.net Web Service
Creating A Web Service From An Interface
Introduction To The Atl Server
Creating A Web Service Using The Atl Server Library
Design And Creation Of The Knowledge Share Web Service
Introduction To Java Xml Technologies
Developing Java Web Services
Design And Creation Of A Web Service Using The Ibm Toolkit
Introduction To Mobile Applications
Creating A Mobile Application That Consumes A Web Service
Web Services Development With Jdeveloper
Creating Web Services Using Perl
Integration Of Xml Web Services With The Office Xp And Sql
Server 2000 Toolkits
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.