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

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

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

740 Project 7 CREATING WEB SERVICES

 

Y

L

F

AM

 

9.In the SelectTDatabase Object dialog box, expand the tree for stored procedures, select the getDenom stored procedure, and click on the OK button. This will bring you back to the ATL OLEDB Consumer Wizard, shown in Figure 24-12, with the names for the files filled in. If required, you can change the names of the files and the class.

10.Click on the Finish button. You will find that a header file has been added to your project.FIGURE E

Open the getDenom.h file. It will have the following code:

//getDenom.h : Declaration of the CgetDenom

#pragma once

//code generated on Wednesday, February 06, 2002, 6:01 PM

[

db_source(L”Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ARTFinance;Data Source=Server1;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=SAIK-D185;Use

Team-Fly®

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

741

 

 

 

 

Encryption for Data=False;Tag with column collation when possible=False”),

db_command(L”{ ? = CALL dbo.getDenom(?) }”)

]

class CgetDenom

{

public:

//In order to fix several issues with some providers, the code below may

//bind

//columns in a different order than reported by the provider

[ db_column(1, status=m_dwDenominationStatus, length=m_dwDenominationLength) ] TCHAR

m_Denomination[51];

//The following wizard-generated data members contain status

//values for the corresponding fields. You

//can use these values to hold NULL values that the database

//returns or to hold error information when the compiler returns

//errors. See Field Status Data Members in Wizard-Generated

//Accessors in the Visual C++ documentation for more information

//on using these fields.

//NOTE: You must initialize these fields before setting/inserting data!

DBSTATUS m_dwDenominationStatus;

//The following wizard-generated data members contain length

//values for the corresponding fields.

//NOTE: For variable-length columns, you must initialize these

//fields before setting/inserting data!

DBLENGTH m_dwDenominationLength;

[ db_param(1, DBPARAMIO_OUTPUT) ] LONG m_RETURN_VALUE; [ db_param(2, DBPARAMIO_INPUT) ] TCHAR m_country[21];

void GetRowsetProperties(CDBPropSet* pPropSet)

{

742 Project 7 CREATING WEB SERVICES

pPropSet->AddProperty(DBPROP_CANFETCHBACKWARDS, true,

DBPROPOPTIONS_OPTIONAL);

pPropSet->AddProperty(DBPROP_CANSCROLLBACKWARDS, true,

DBPROPOPTIONS_OPTIONAL);

}

};

You will find that the CgetDenom class neatly encapsulates your stored procedure. It even has variables declared for the input and the output values.

[ db_column(1, status=m_dwDenominationStatus, length=m_dwDenominationLength) ] TCHAR m_Denomination[51];

[ db_param(2, DBPARAMIO_INPUT) ] TCHAR m_country[21];

FIGURE 24-12 The ATL OLEDB Consumer Wizard

In the preceding code:

m_country is the variable for the input value to the stored procedure

m_Denomination is the variable for the value returned by the stored pro-

cedure

Similarly, create a class that encapsulates the other stored procedure. Once done, you will have two classes, CgetDenom and CgetRate (or whatever names you have used), that encapsulate the two stored procedures.

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

743

 

 

 

 

Now all that is left is to use these classes in the Web service methods previously created. Open the file ExRateWS.h and add the following include statements:

#include “getDenom.h”

#include “getRate.h”

Now, modify the implementation of the two Web service methods that you have created earlier in the same file:

[ soap_method ]

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

{

CgetDenom *p=new CgetDenom();

Convert(bstrInput,p->m_country,11);

p->OpenAll(); p->MoveFirst();

CComBSTR tempbstr = p->m_Denomination ;

tempbstr.CopyTo(bstrOutput);

return S_OK;

}

[ soap_method ]

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

{

CgetRate *p=new CgetRate();

Convert(bstrInput,p->m_country,11);

p->OpenAll(); p->MoveFirst();

CComBSTR tempbstr = p->m_ExRate ; tempbstr.CopyTo(bstrOutput);

return S_OK;

}

744 Project 7 CREATING WEB SERVICES

In the preceding code, the variable declared by the ATL OLEDB Consumer Wizard to hold the input value to the stored procedure is an array of the type TCHAR, while the value accepted by the method is a BSTR. So, to do the conversion, I have added a helper function Convert, which is defined here:

LPTSTR Convert(BSTR pStr,LPTSTR szStr,INT nSize)

{

DWORD dwRet = WideCharToMultiByte(

CP_ACP,0,(LPCWSTR)pStr,-1,szStr,nSize,NULL,NULL);

return szStr;

}

Well, that’s it. Your Web service is now ready to be built, so build it. After it is built, the IDE will automatically deploy it under a virtual directory of the same name as the Web service.

You will not be able to test this Web service the way you did with the .NET-based Web service in the last chapter, because HTML files that connect to the Web service are not created as they were in the previous chapter. Instead, a single HTM file created will simply connect to the WSDL of the Web service.

The following will be the messages in the WSDL and consequently the operations supported by the Web service:

<message name=”getDenomIn”>

<part name=”bstrInput” type=”s:string” /> </message>

<message name=”getDenomOut”>

<part name=”return” type=”s:string” /> </message>

<message name=”getRateIn”>

<part name=”bstrInput” type=”s:string” /> </message>

<message name=”getRateOut”>

<part name=”return” type=”s:string” /> </message>

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

745

 

 

 

 

As you can see, the messages clearly describe the operation names and the types of data required by them. In the next section, you will test this Web service from a C# application. You might as well implement the client using Managed C++ or Visual Basic.NET with a few changes in the syntax.

Testing the Application

To test this application, you need to create a C# application. To do so, perform the following steps:

1.Choose File, Add Project, New Project. Create a new Visual C# project of the type Windows Application. This will add one more project to the solution.

2.In the form of the Windows Application, add a button from the Toolbox.

3.Right-click on the name of the C# project in the Solution Explorer window and select Add Web Reference. This will open the Add Web Reference dialog box.

4.In the Add Web Reference dialog box, click on the Web Services on the Local Web server link. This will display a list of all Web services on the local Web server as shown in Figure 24-13.

FIGURE 24-13 The Add Web Reference dialog box

746 Project 7 CREATING WEB SERVICES

5.Select the Web service that you have created and click on the Add Reference button.

You will find a lot of items being added to the Solution Explorer window. What really happens is that a proxy referencing the Web service is created and a local copy of the WSDL is added to the project.

NOTE

Since this WSDL is a local copy, it is not updated if you make any change to the Web service. Suppose you add another method to the Web service class, along with the attribute SOAP method, you will find that the name of the method is not automatically displayed by the Intellisense feature . You will need to follow the Add Web Reference dialog box steps again to update the WSDL.

In the Solution Explorer window, under the Local host node, you will find a list of files added to the project. Following is a discussion about some of the files generated:

The Reference.map file is a simple XML file that contains the links to the DISCO and WSDL files of the Web service:

<?xml version=”1.0” encoding=”utf-8”?> <DiscoveryClientResultsFile

xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”> <Results>

<DiscoveryClientResult

referenceType=”System.Web.Services.Discovery.ContractReference”

url=”http://server1/ExRateWS/ExRateWS.dll?Handler=GenExRateWSWSDL” filename=”ExRateWS.wsdl” />

<DiscoveryClientResult

referenceType=”System.Web.Services.Discovery.DiscoveryDocumentReference”

url=”httˆ://localhost/ExRateWS/ExRateWS.disco”

filename=”ExRateWS.disco”

/>

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

747

 

 

 

 

</Results>

</DiscoveryClientResultsFile>

The DISCO file is also simple and contains only a reference to the WSDL of the Web service. It is very different from the VSDISCOs files you encountered in the previous chapter.

<?xm_cf5 version=”1.0” enc_u100 ?ing=”utf-8”?>

<discovery xm_u110 ?s:xsd=”http://www.w3.org/2001/XMLSchema” xm_u110 ?s:xsi=”http://www.w3.org/2001/XMLSchema-instance” xm_u110 ?s=”http://èchemas.xmlsoap.org/disco/”>

<contrYctRef ref=”http://saikd185/ExRateWS/ExRateWS.dll?Handler=GenExRateWSWSDL”

docRef=”http://èaikd185/ExRateWS/ExRateWS.htm” xm_u110

?s=”http://www.schemas.xmlsoap.org/disco/scl/” /> </discovery>

Now that a reference to the Web service has been added, you can implement the code. First, add a using namespace declaration for the namespace of the Web service, as follows:

using YourWindowsApplicationName.localhost;

If you remember, you have already added a button to the form of the Windows application. In the Click event handler of the button, add the following code:

private void button1_Click(object sender, System.EventArgs e)

{

ExRateWSService p=new ExRateWSService(); try

{

MessageBox.Show(p.getDenom(“Guyana”));

}

catch(Exception m)

{

MessageBox.Show(m.ToString() );

}

try

748 Project 7 CREATING WEB SERVICES

{

MessageBox.Show(p.getRate(“Guyana”));

}

catch(Exception m)

{

MessageBox.Show(m.ToString() );

}

}

In the preceding code, we are just creating an instance of the Web service and calling its methods. Test the Web service by retrieving the denominations and exchange rates for countries whose data has been entered in the database table.

Summary

In this chapter, you created a Web service using the ATL Server library and tested it from a C# application. The ATL Server library allows you to create high-per- formance Web services for all those systems that do not have the .NET Framework installed. It also allows you to leverage existing competencies in C++ and ATL Server programming.

PARTIXProfessional

Project – 8