Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
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>
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”
/>
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
