During this phase, the functionality to be implemented and the technology to be used are determined. Accordingly, the Code-Forge team identified the functionality to expose the catalog using Web services. It was decided that the Web service should allow an associate of art-shop.com to access the product details of the products on sale at art-shop.com and integrate the product details, with content
of their site. The Web service will have another operation that allows any of the
Y
affiliate Web sites to place an order with art-shop.com.
L
Construction
F
During this phase, the Code-Forge team will construct the application. The fol-
lowing are the primary tasks that the team needs to complete:
E
Create a Web service that is used to expose the catalog.
T
Construct the Web serviceMsuch that it allows affiliate sites to place an
order.
A
Testing
The team decided to test the component by accessing it from a client application. The team decided that it would test the application using a Managed C++ client application.
Now that you know about the background work done by the Code-Forge team, you are ready to try your hand at creating a Web service. In the following sections, I will guide you in the process of creating a Web service.
As stated earlier, the Web service is being designed to provide a catalog of the products currently available at Art-Shop’s gallery and on its Web site. To do this, the Web service must be able to access the table containing the required data. The next section details the structure of the table that acts as the data source for the Web service.
The Product Table in the ArtShop Database
The Product table in the ArtShop database contains all the data about the various art items on sale at art-shop.com. Figure 23-1 displays the structure of the Product table. This table will act as the data source for your Web service.
Team-Fly®
CREATING A WEB SERVICE USING MANAGED EXTENSIONS Chapter 23 701
FIGURE 23-1The structure of the Product table
Creating the Basic Managed C++ Web Service
To create a Managed C++ Web service, perform the following steps:
1.Start Visual Studio .NET and create a new Visual C++ .NET project. Select Managed C++ Web Service as the project type from the Visual C++ Projects dialog shown in Figure 23-2.
2.Enter the project name as ProdService and enter the path. Click on the OK button.
FIGURE 23-2The Visual C++ projects dialog box
After the project has been created, you will find that the IDE has created a Hello World Web service. First take a look at some of the files created by the IDE:
702Project 7 CREATING WEB SERVICES
ProdService.cpp. This is the source file for the Web service. This is where the various Web service methods that provide the functionality will be implemented.
ProdService.asmx. This is the source file for the ASP.NET Web service file. This file simply redirects the requests to the DLL containing the Web service functionality.
AssemblyInfo.cpp. This file contains general information about the assembly, such as the build number and copyright. This information is created when you build your application.
Stdafx.h and Stdafx.cpp. These files contain the standard declarations.
ProdService.h. This file contains the declaration of a class, which derives from the WebService class.
Global.asax. This file redirects to Global.asax.h as the code behind its functionality.
Global.asax.h. This file has handlers for Web service–wide event notifications provided by the HttpApplication class.
ProdService.vsdisco. DISCO files are files used by Microsoft for identifying the Web services on the local system.
You will next look at the code that the IDE has added to two of the files, ProService.h and ProdService.cpp:
//ProdService.h
using namespace System; using namespace System::Web;
using namespace System::Web::Services;
namespace ProdService
{
public __gc
class Class1 : public WebService
{
public:
//WEB SERVICE EXAMPLE
//The HelloWorld() example service returns the string “Hello, World!”.
//To test this Web service, ensure that the .asmx file in the
CREATING A WEB SERVICE USING MANAGED EXTENSIONS Chapter 23 703
deployment path is
//set as your Debug HTTP URL, in project properties.
//The HelloWorld() example service returns the string “Hello, World!”.
//To test this Web service, ensure that the .asmx file in the //deployment path is
//set as your Debug HTTP URL, in project properties.
//and press F5.
String __gc* Class1::HelloWorld()
{
// TODO: Add the implementation of your Web Service here
return S”Hello World!”;
}
};
704 Project 7 CREATING WEB SERVICES
Next, you can try running the Web service. To do that, build the application and select Start from the Debug menu. You will see the screen shown in Figure 23-3 in a browser window.
FIGURE 23-3Connecting to the Web service
Click on the HelloWorld link. The resulting page shows sample SOAP messages that will be used to invoke the Web service and also contains an Invoke button as shown in Figure 23-4.
The Web service function returns an XML document, which is displayed in the browser window as shown in Figure 23-5.
Modifying the Web Service
You have created and executed a Web service that does nothing more than return a string. You will now modify this sample to add the functionality required for the site art-shop.com:
1.From the Solution Explorer, open the file ProdService.asmx. This file is an ASP.NET file, which will be opened whenever the Web service is invoked. The file contains a single line of code:
CREATING A WEB SERVICE USING MANAGED EXTENSIONS Chapter 23 705
FIGURE 23-4Invoking the Web service
FIGURE 23-5Response from the Web service
<%@ WebService Class=ProdService.Class1 %>
The ASMX file simply points to a class in an assembly. This class will contain all the code to support the functionality required of the Web service. You will need to take a closer look at the class Class1 declared in the ProdService.h file:
public __gc
class Class1 : public WebService
{
}
Inheriting the class from System::Web::Services::WebService allows access to built-in objects of ASP.NET. These objects include the Session, Server, and
Application objects.
706 Project 7 CREATING WEB SERVICES
In the ProdService.h file, add a method called getArtList to the Class1 class as indicated in the following code:
public __gc
class Class1 : public WebService
{
public:
//[System::Web::Services::WebMethod]
//String __gc* HelloWorld();
ArtArray getArtList();
};
The IDE would have added a method HelloWorld (commented in the preceding code). Note the attribute WebMethod used before the method declaration. The WebMethod attribute allows you to declare a method as being one of the operations exposed by the Web service.
[System::Web::Services::WebMethod]
Also, this allows you to enable buffering of response, caching of response, adding textual description (which will be written to the WSDL file for service requestor use), session management, and transaction handling. Add the attribute before the declaration of the getArtList method.
You can use the Description property of the attribute to document the description about your Web service in the WSDL document that will be generated for your Web service:
[WebMethod(Description=”This method returns a list of products available at art-
shop.com”)]
ArtArray getArtList();
Now, you will take a look at the entry for the getArtList in the WSDL file for this Web service. The WSDL file is located at the following URL:
EnableSession. This property is used to enable the ASP.NET session state, which is disabled by default.
BufferResponse. This property specifies whether the value returned by the service should be buffered or not. The property can be set to true or
false.
In the preceding discussion, you saw that the sample Web service created by the IDE uses a namespace http://tempuri.org (refer to Figure 23-3). The test pages that are generated inform you about this and also show the way to change the default namespace.
You will next change the default namespace to www.art-shop.com. To do this, you will use another attribute, [WebService].
Add the following code before the declaration of the class Class1 in ProdService.h:
[WebService(Namespace=”http://www.art-shop.com”)]
public __gc
class Class1 : public WebService
{
After adding this attribute, when you run the application, by pressing F5, you find that the test pages no longer ask you to change the namespace. Also, take a look at the definitions tag of WSDL created after you have built the application:
Next, you will implement the functionality of the Web service.
To add the required functionality, you need to create a structure that can hold all the data contained in the Product table. This structure will be used to display products from the art-shop.com site. Add the following code to the ProdService.h file:
__value public struct ArtData
{
String* ProdID;
String *ProdType;
String *ProdName;
String *Name;
String *ProdPrice;
String *PictureFile;
};
As you can see, the structure declaration mimics the Product table except for the data types. For simplicity, we have declared all the variables as of type String. The Web service will return an array of ArtData objects. For easier use and handling of the array, add the following declaration:
typedef ArtData ArtArray[];
From the Class1 declaration, remove the declaration for the HelloWorld method and a declaration for the getArtList method:
[System::Web::Services::WebMethod]
ArtArray getArtList();
The getArtList method is denoted as a method exposed by the Web service using the WebMethod attribute. The method returns an array of ArtData objects.
CREATING A WEB SERVICE USING MANAGED EXTENSIONS Chapter 23 709
Next, open the file ProService.cpp by double-clicking on it from the Solution Explorer window.
From the ProdService namespace declaration, remove the declaration for the HelloWorld method and add an empty implementation of the getArtList method. This will remove the HelloWorld method from the list of operations supported by the Web service and add getArtList to the list.
ArtArray Class1::getArtList()
{
}
Accessing Data Using the SQL Server.NET Data Provider
For accessing the objects and methods of the SQL Server.NET Data Provider, add the following declarations to the ProdService.h file:
#using <System.Data.dll>
using namespace System::Data;
using namespace System::Data::SqlClient;
First, you need to connect to the database, for which you will use SqlConnection object. Next, add the following code to the getArtList method implemented in the ProService.cpp file: