Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать
☆

730 Project 7 CREATING WEB SERVICES

Creating the ATL Server Web Service

Once the database table and the stored procedures are ready, the next step is to create the Web service.

1. Start Visual Studio.NET and create a new Visual C++.NET project. Select ATL Server Web Service as the project type as shown in Figure 24-1.

 

 

 

Y

2. Enter the project name as EXRateWS and enter the path. Click on the

OK button.

 

L

 

 

F

 

 

M

 

 

A

 

 

E

 

T

 

 

FIGURE 24-1 The Visual C++ New Project dialog box

This starts the ATL Server Project Wizard. The contents of the Overview page shown in Figure 24-2 displays the summary of the default selections.

The Overview page describes the default settings of the project options and tells you that, when compiled, the solution will create two DLLs. One will be a Web service DLL and the other will be a standard ISAPI extension DLL. It also tells you that once built, the services will be deployed for you, and that the code makes use of attributes and some other support features.

3.Click on the Project Setting link to display the settings for the project, shown in Figure 24-3.

Team-Fly®

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

731

 

 

 

 

FIGURE 24-2 The Visual C++ ATL Server Project Wizard – Overview page

FIGURE 24-3 The Visual C++ ATL Server Project Wizard – Project Settings page

If you would rather have a combined DLL created comprising the functionality of both the Web service DLL and ISAPI extension DLL, then you can select the Generate combined DLL check box. It might ease up

732 Project 7 CREATING WEB SERVICES

the process of actually installing the application, but the DLL created will be heavier and the performance will diminish.

4. Click on Server Options to display the page shown in Figure 24-4.

FIGURE 24-4 The Visual C++ ATL Server Project Wizard – Server Options page

This page lists a few important additional support features that you can add to your application at the click of a check box.

5.Click on Application Options which displays the Application Options page shown in Figure 24-5.

This page contains a check box named Create as Web Service. By default, this option is selected if you select the ATL Web service project type. On the other hand, if you select ATL Server application as the project type, you need to select this check box to convert the application into a Web service.

After the project has been created, you will find that the IDE has created a HelloWorld Web service. The project has a host of files created. Next, you will browse through the list of files appearing in the Solution Explorer window shown in Figure 24-6.

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

733

 

 

 

 

FIGURE 24-5 The Visual C++ ATL Server Project Wizard – Application Options page

FIGURE 24-6 The Solution Explorer window

734 Project 7 CREATING WEB SERVICES

Two projects are created in the solution:

ExRateWS

ExRateWSIsapi

The ISAPI extension DLL project, ExRateWSIsapi, doesn’t require any modifications. So, let me concentrate on explaining the ExRateWS project. Some of the files that are part of the project, ExRateWS, are listed here:

ExRateWS.cpp. Contains the method declarations necessary for the DLL, such as DllMain.

ExRateWS.h. Contains the code required for implementing the default Web service.

ExRateWS.Disco. Contains the data required for the dynamic discovery of the Web service. In the previous chapter, you saw that the IDE created a DISCO file. The DISCO file is an earlier version of the VSDISCO file.

ExRateWS.htm. Contains the path to the WSDL file for the Web service. This file describes the operations available in the default Web service (i.e., HelloWorld). However, this piece of information is hard coded and doesn’t change when you add more operations or delete the default operation. So, do not get confused if you connect to this page after it has been deployed and you see a listing for HelloWorld.

Among these files, the ExRateWS.h file hosts the functionality of the Web service:

// ExRateWS.h : Defines the ATL Server request handler class

//

#pragma once

namespace ExRateWSService

{

//all struct, enum, and typedefs for your webservice should go inside the //namespace

//IExRateWSService - Web service interface declaration

//

[

uuid(“D935E244-26D2-4DCC-9FC7-FB0DF6E61E94”),

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

735

 

 

 

 

object

]

__interface IExRateWSService

{

//HelloWorld is a sample ATL Server web service method. It shows how to

//declare a web service method and its in-parameters and out-parameters [id(1)] HRESULT HelloWorld([in] BSTR bstrInput, [out, retval] BSTR *bstrOutput);

//TODO: Add additional web service methods here

};

// ExRateWSService - web service implementation

//

[

request_handler(name=”Default”, sdl=”GenExRateWSWSDL”), soap_handler(

name=”ExRateWSService”,

namespace=”urn:ExRateWSService”,

protocol=”soap”

)

]

class CExRateWSService :

public IExRateWSService

{

public:

//This is a sample web service method that shows how to use the

//soap_method attribute to expose a method as a web method

[ soap_method ]

HRESULT HelloWorld(/*[in]*/ BSTR bstrInput, /*[out, retval]*/ BSTR *bstrOutput)

{

CComBSTR bstrOut(L”Hello “); bstrOut += bstrInput; bstrOut += L”!”;

*bstrOutput = bstrOut.Detach();

736 Project 7 CREATING WEB SERVICES

return S_OK;

}

// TODO: Add additional web service methods here }; // class CExRateWSService

} // namespace ExRateWSService

This Web service simply accepts a String and appends it to the string Hello and returns the concatenated string.

__interface IExRateWSService

{

[id(1)] HRESULT HelloWorld([in] BSTR bstrInput, [out, retval] BSTR *bstrOutput);

};

The IExRateWSService interface for the component declares just one method, HelloWorld. The method is defined as follows:

[ soap_method ]

HRESULT HelloWorld(/*[in]*/ BSTR bstrInput, /*[out, retval]*/ BSTR *bstrOutput)

{

//code edited out

return S_OK;

}

The soap_method attribute tells the compiler that this method is for an operation that will be exposed by the Web service. Also, the parameters accepted and returned by this attribute will be SOAP-encoded.

Now you will modify the code for the Web service by adding two methods — methods that will accept the name of a country and return the exchange rate and the denomination used.

In the interface, add the following code declarations for the two methods:

__interface IExRateWSService

{

[id(1)] HRESULT getDenom([in] BSTR bstrInput, [out, retval] BSTR *bstrOutput);

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

737

 

 

 

 

[id(2)] HRESULT getRate([in] BSTR bstrInput, [out, retval] BSTR *bstrOutput); };

Next, the following methods need to be defined:

[ soap_method ]

HRESULT getDenom(/*[in]*/ BSTR bstrInput, /*[out, retval]*/ BSTR *bstrOutput)

{

return S_OK;

}

[ soap_method ]

HRESULT getRate(/*[in]*/ BSTR bstrInput, /*[out, retval]*/ BSTR *bstrOutput)

{

return S_OK;

}

At present, these methods don’t have any functionality. You need to code these to access the database, execute the stored procedures, and return the values from the database. To do so, perform the following steps:

1.Right-click on the project name in the Solution Explorer window and choose Add, Add Class. This will open the Add Class dialog box, shown in Figure 24-7.

FIGURE 24-7 The Add Class dialog box

738Project 7 CREATING WEB SERVICES

2.In the dialog box, select ATL OLEDB Consumer.

3.Click on the Open button. This opens the ATL OLEDB Consumer Wizard, shown in Figure 24-8.

FIGURE 24-8 ATL OLEDB Consumer Wizard

4.Click on the Data Source button. This opens the Data Link Properties dialog box, shown in Figure 24-9.

5.In the displayed list of OLEDB providers, select Microsoft OLEDB Provider for SQL Server.

6.Click on the Next button. On the Connection tab of the Data Link Properties dialog box, you need to enter the details for connecting to the SQL Server, as shown in Figure 24-10.

7.Select the name of your SQL server and also the authentication type as appropriate to your local SQL server installation. Also select the name of the database you have created.

8.Click on the Test Connection button to validate the connection string. If the testing is successful, then click on the OK button. This will open the Select Database Object dialog box shown in Figure 24-11.

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

739

 

 

 

 

FIGURE 24-9 The Data Link Properties dialog box

FIGURE 24-10 The Connection tab of the Data Link Properties dialog box