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

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

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

720 Project 7 CREATING WEB SERVICES

This allows the previously mentioned port types to be bound to the protocol, which is used to transport the messages.

Consider the functionalities of the following WSDL elements:

The <operation> element specifies the operations supported by a Web service.

The <port> element groups these operations.

 

Y

The <type> element lists the data types used in the service.

 

The <binding> element matches <port> elements to protocols.

Together, all of these elements tell a Web client what your Web service does.

 

M

Getting back to your client application,Lselect the ProdService Web service and

A

click on the Add Reference button.FCorrespondingly, two files are added to the

E

Solution Explorer window:

 

T

 

A local copy of the WSDL file

A new header file, WebService.h

Also, if you check the Debug folder of your project, you will find a proxy assembly for the ProdService.dll created. The content of the header file is as follows:

#using <System.DLL>

#using <ProdService.dll>

#using <System.Web.Services.DLL>

#using <System.Data.DLL>

Now the Web service is accessible from your application. The next step is to add the code required for displaying a form in the Managed C++ application and a ListView control on that form.

Add the following declarations to the WebServiceClient.cpp file to support the GUI elements (user interface elements, such as ListView controls):

#using <System.Windows.Forms.dll>

using namespace System::Windows::Forms;

Next, create a class that inherits from the Form class, as shown here:

__gc class MainForm : public Form

{

public:

MainForm();

Team-Fly®

CREATING A WEB SERVICE USING MANAGED EXTENSIONS Chapter 23 721

private:

ListView *lv;

ListViewItem *lvi;

};

Add variables that will allow you to add and manipulate list view objects. Implement the constructor for the form class:

lv=new ListView();

lv->Dock=DockStyle::Top;

lv->View=View::Details;

In the list box, you will display the ID, type, name, and price of various products available at art-shop.com. Add the column titles to the list view and display the list view:

lv->Columns->Add(“Product ID”,45,HorizontalAlignment::Left); lv->Columns->Add(“Product Type”,160,HorizontalAlignment::Left ); lv->Columns->Add(“Product Name”,160,HorizontalAlignment::Left ); lv->Columns->Add(“Product Price”,100,HorizontalAlignment::Left );

this->Controls->Add(lv);

The main function of the application will simply instantiate the form:

int _tmain(void)

{

// TODO: Please replace the sample code below with your own.

Application::Run(new MainForm()); return 0;

}

The application and the user interface are ready. Next, you need to add the code to access the Web service. To do so, add the following code snippet:

int i=0;

//pointer to an array of ArtData objects ArtData *lst[];

722 Project 7 CREATING WEB SERVICES

//Class1 Is the class In the Web service

Class1 *Ws=new Class1();

lst=Ws->getArtList();

Once a reference to the array returned by the Web service is obtained, you can access each ArtData object as follows:

lst[0]->ProdID //The prodID of the first art object

The following is the complete code for the constructor to access the details of all the art objects and display them in a list view:

MainForm::MainForm()

{

ArtData *lst[]; int i=0;

Class1 *Ws=new Class1(); lst=Ws->getArtList();

lv=new ListView(); lv->Dock=DockStyle::Top; lv->View=View::Details;

lv->Columns->Add(“Product ID”,45,HorizontalAlignment::Left); lv->Columns->Add(“Product Type”,160,HorizontalAlignment::Left ); lv->Columns->Add(“Product Name”,160,HorizontalAlignment::Left ); lv->Columns->Add(“Product Price”,100,HorizontalAlignment::Left );

for(i=0;i<lst->Count;i++)

{

lvi=lv->Items->Add(lst[i]->ProdID); lvi->SubItems->Add(lst[i]->ProdType); lvi->SubItems->Add(lst[i]->ProdName); lvi->SubItems->Add(lst[i]->ProdPrice);

}

this->Controls->Add(lv);

}

CREATING A WEB SERVICE USING MANAGED EXTENSIONS Chapter 23 723

Summary

In this chapter, you created a simple Web service using Managed Extensions for C++. You saw how Visual C++ .NET encapsulates most of the difficult details of creating a Web service, thereby increasing developer productivity. You have also learned how to access the Web service from a Managed C++ client.

This page intentionally left blank

Chapter 24

Creating a Web

Service Using ATL

Server

AWeb service is a way for a Web site to expose programmatic functionality. Web services accept input in the form of messages and reply through messages. These messages can be sent and received using HTTP-POST, HTTP-

GET, or SOAP. Visual C++.NET makes it very simple to create a Web service.

In the last chapter, you created a Web service using Managed Extensions for C++. However, that Web service was created on the .NET platform. What about a Windows installation that is not .NET based? Well, you can create Web services for such platforms by using the ATL Server library.

In Chapters 20 and 21, you worked with ATL Server to create a Web application. Using the same ATL Server library, you will now create a Web service.

However, creating a Web service using ATL Server is not as simple and clean as doing it in any of the other .NET languages, such as Visual Basic and C#. Then, why would anyone want to use the ATL Server library? Here are some of the reasons why you would prefer the ATL Server:

ATL Server Web services offer very high performance.

You have existing skills and competency in using ATL-based libraries. (This is directed to the chores of VC++ programmers out there!)

So, get set to try out ATL Server programming to create Web services. As a starter, I present a scenario wherein you can implement the ATL Server Web services. Later, I will guide you through the steps to be performed to create the ATL Server Web service.

Exchange Rate Web Service

NJ Finances (NJF) is a reputed financial services company based in New York. It has a Web site that, among other things, offers public articles on finance, descriptions of the company’s mutual funds, and a stock ticker.

NJ Finances now is adding a few more features to its Web site:

Free access to articles on investing and financial management written by the company’s financial consultants.

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

727

 

 

 

 

A complete tutorial on money trading and investments in foreign countries.

Business news from various countries sourced and syndicated from local newspapers.

A Web service that gives the rate of exchange for currencies of different countries updated on a daily basis.

The company’s MIS and software division are involved in upgrading the site.

Project Life Cycle

In the project-initiation phase, the project plan was prepared and the development team for the project was also identified. The development team further prepared a comprehensive list of tasks involved in the execution and the deployment phases of the project’s life cycle.

Moving further, you will look at the tasks performed by the development team in the following stages of the project-execution phase:

Requirements analysis

Design

Construction

Testing

What follows is a description of the tasks performed by the development team in each of these stages.

Requirements Analysis

During the requirement analysis phase, the development team at NJF identified the need to use a Web Service application and came up with the following requirements:

The Web service should run on a server that does not have the .NET platform installed.

The Web service should expose methods that return the current exchange rate and the denomination for a given country.

728 Project 7 CREATING WEB SERVICES

Design

During this phase, the functionality to be implemented and the technology to be used are determined. It was decided that the data should be stored in a SQL Server database where the data for the rest of the Web site is also stored. The database will be accessed using two stored procedures. One of the stored procedures returns the rate for a given country and the other returns the corresponding denomination.

Construction

During this phase, the NJF team will construct the application. The primary tasks that the team needs to complete are the following:

Design and create the table that will contain the data.

Create the stored procedures.

Create a Web service application using the ATL Server library in Visual C++. NET.

Testing

The team has decided to check the functionality of the Web service by invoking it from within a C# client application. C# has been selected as the client application because a Web service should be compatible across languages and platforms. This Web service should work from any language, provided the corresponding language can support SOAP messages.

Creating the Web Service

As it was decided earlier, the very first step in this project is to create a table that will hold the exchange rates for various currencies. To do this, you need to use the Microsoft SQL Server as the back end.

Creating the Database, Table, and Stored Procedures

In your local installation of SQL Server, create a database, and add a new table IMFRates. Table 24-1 lists the structure of the IMFRates table.

CREATING A WEB SERVICE USING ATL SERVER

Chapter 24

729

Table 24-1 Structure of the IMFRates Table

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Field

Data type

Size

 

 

 

 

 

 

 

 

 

 

 

Country

Varchar

20

 

 

 

 

Denomination

Varchar

10

 

 

 

 

ExRate

Varchar

10

 

 

 

 

 

 

 

 

 

 

 

Once the table is ready, you need to populate it with some data. The International Monetary Fund publishes representative exchange rates daily at its site. To pick up values from there:

1.Go to the site www.imf.org.

2.Navigate to the site map page.

3.On the site map page, click on the link representative rates for selected currencies. On this page, you find the exchange rates for most of the countries.

You can populate the database table from data on this page.

Besides this table, you also need to create two stored procedures:

The getDenom stored procedure accepts the name of the country as a parameter and returns the corresponding currency denomination.

The getRate stored procedure accepts the name of a country and returns the exchange rate.

Next, you will create these two stored procedures.

The code for creating the getDenom stored procedure is as follows:

CREATE PROCEDURE getDenom

@country varchar(20)

AS

select Denomination from rates where country=@country

GO

The code for creating the getRate stored procedure is as follows:

CREATE PROCEDURE getRate

@country varchar(20)

AS

select ExRate from rates where country=@country

GO