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

C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects

.pdf
Скачиваний:
475
Добавлен:
12.02.2016
Размер:
14.7 Mб
Скачать

638 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

Testing the SampleWebService Web Service

As already discussed, the Web service does not have an interface. However, to test the Web service, Visual Studio .NET launches the Web service in Internet Explorer. I will now discuss the steps to test SampleWebService.

To test the SampleWebService Web service, click on the Debug menu, and then select the Start option. Alternatively, you can press the F5 key to debug and run

the Web service.

 

 

 

Y

 

 

 

L

Visual Studio .NET launches the Web service in the Internet Explorer window,

as shown in Figure 28-8.

 

F

 

 

 

 

 

 

M

 

 

 

A

 

 

E

 

 

T

 

 

FIGURE 28-8 The Service1.asmx page for SampleWebService

In addition to the description of the Web service and the Web method, the Service1.asmx page contains a link to call the GetDay() method. To access the Web method, click on the link.

When you click on the link in the Service1.asmx page, the Service1.asmx page for the GetDay() method is displayed. To execute the GetDay() method, you need to pass a date as a parameter to the GetDay() method and click on the Invoke button. Figure 28-9 shows a parameter passed to the GetDay() method.

Team-Fly®

EXPLORING ASP.NET WEB SERVICES

Chapter 28

639

 

 

 

 

FIGURE 28-9 The parameter specified in the GetDay() method

The result returned by the SampleWebService Web service is displayed. Figure 28-10 shows the result returned by the Web method.

FIGURE 28-10 The result returned by the GetDay() method

640 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

Summary

In this chapter, you learned about distributed applications. Distributed applications are scalable applications in which data is shared across applications. Therefore, distributed applications include applications built on different platforms or by using different programming languages. To create a large-scale business solution, it is essential that you integrate these applications. Integration of applications built on various platforms is made simpler with the use of Web services.

A Web service is a reusable component, such as a method, that can be used by any Web application running on the Internet. These applications are called Web service client applications. An application that hosts the Web service is called a Web service provider application.

Next, you learned about the architecture of a Web service.The Web service architecture includes a four-layered model. These layers are the data layer, the data access layer, the business layer, and the listener layer. Based on this architecture, you learned about the working of the Web service.

Next, you learned about the role of XML, WSDL, SOAP, and UDDI in a Web service.Based on this knowledge about a Web service, you learned to create a simple Web service using Visual Studio .NET.

Chapter 29

Developing

Web Services

642 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

In the preceding chapters, you looked at the case study and design of the Web site of Bookers Paradise. In addition, you were introduced to the basics of an ASP.NET Web service. Based on the knowledge about Web services, you learned to create a sample Web service by using Visual Studio .NET. This chapter dis-

cusses how to create a Web service for Deepthoughts Publications.

Creating a Web Service for

Deepthoughts Publications

A Web service for Deepthoughts Publications provides information about books published at its publishing house. This information is displayed on the Web site of Bookers Paradise. In addition to searching for information about all the books published by Deepthoughts Publications, a user can search for selected books based on criteria.

To start with, you can create a Web service by using the ASP.NET Web Service template provided by Visual Studio .NET. Refer to this Web service as DTWebService. To create the Web service, perform the following steps:

1.On the File menu, point to the New option.

2.In the displayed list, select the Project option. The New Project dialog box is displayed.

3.In the Project Types: pane, select the Visual C# Projects option.

4.In the Templates: pane, select the ASP.NET Web Service option.

5.In the Location: text box, type the name of the Web service as

DTWebService.

6. Click on the OK button to create the DTWebService Web service.

Figure 29-1 shows the New Project dialog box for DT WebService.

DEVELOPING WEB SERVICES

Chapter 29

643

 

 

 

 

FIGURE 29-1 The New Project dialog box for DTWebService

Visual Studio .NET creates the Web service and the default files in the Web service. I have discussed the default files generated by Visual Studio .NET for a Web service in Chapter 28, “Exploring ASP.NET Web Services,” in the section “Web Services in the .NET Framework.”

After creating the Web service, add a short description of the Web service. This will help any user looking for a similar Web service. To write a short description, add the following code at the beginning of the Web service.

[WebService (Namespace=”http://LocalHost/DTWebService/”, Description=”A service

displaying catalogs of Deepthoughts publisher”)]

Because the Web service that you create needs to connect to a database, you need to create a data connection object. To create a data connection object to connect to a database, perform the following steps:

1.In the Server Explorer window, expand the Servers node.

If the Ser ver Explorer window is not displayed, you can select the Server Explorer option on the View menu.

When you expand the Servers node, a list of the available SQL servers is displayed.

644 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

2.Browse for the DTDB database and drag the DTCatalog table to the Service1.asmx page.

Visual Studio .NET automatically adds sqlConnection and sqlDataAdapter components to the Service1.asmx page. When the sqlConnection and sqlDataAdapter components are added, you need to create a dataset that accesses data from the DTDB database. To do this, perform the following steps:

1.On the Data menu, select the Generate Dataset option. The Generate Dataset dialog box is displayed.

2.In the Choose a dataset: group box, select the New radio button.

3.Specify the name of the dataset as dsDetails1.

4.Check the Add this dataset to the designer window check box.

5.Click on the OK button to close the Generate Dataset dialog box.

The dataset with the name dsDetails1 is added to the Service1.asmx page. Figure 29-2 shows the sqlConnection, sqlDataAdapter, and dataset components added to the Service1.asmx page.

FIGURE 29-2 The Service1.asmx page with the components added

Once a Web service is created and the components are added, you can add Web methods to it. Web methods are required to provide the functionality to the Web site. The following section discusses the Web methods in the Web service of Deepthoughts Publications.

DEVELOPING WEB SERVICES

Chapter 29

645

 

 

 

 

Creating the SearchAll() Web Method

Creating a Web service involves coding for the Web methods in the Web service. In this case, you need to create a Web method that returns information about all the books published at Deepthoughts Publications. Before creating the Web method, add a short description about the Web method.

[WebMethod(Description=”This method searches for the details of all books published

by Deepthoughts Publications”)]

Now add the code for the Web method. The code for the Web method is as displayed:

[WebMethod(Description=”This method searches for the details of all books published by Deepthoughts Publications”)]

public DataSet SearchALL()

{

string SelStr;

SelStr = “Select * from DTCatalog”;

SqlCommand SelCom;

SelCom = new SqlCommand(SelStr, sqlConnection1); sqlDataAdapter1.SelectCommand = SelCom; sqlConnection1.Open(); sqlDataAdapter1.SelectCommand.ExecuteNonQuery(); sqlDataAdapter1.Fill(dsDetails1,”Details”); sqlConnection1.Close();

return dsDetails1;

}

The preceding code declares a string variable with the name SelStr. Next, the code initializes the SelStr variable to a SQL statement that retrieves all the records from the DTCatalog table. Next, an instance of the SqlCommand class is created with the name SelCom. The SqlCommand class is present in the System.Data.SqlClient namespace. The SqlCommand class is used to represent a SQL statement that is executed against a SQL server database. You can derive a class from a SqlCommand class.

After declaring the SelCom instance, you can initialize it. In the constructor of the SqlCommand class, you need to pass two parameters, SelStr and sqlConnection1. Because the SqlCommand class represents all the SQL statements executed against a SQL ser ver database, you need to specify the SQL statement by passing it as a

646 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

parameter to the constructor. In addition, you need to specify the sqlConnection object for executing the SQL command. This object is passed as a parameter to the constructor of the SqlCommand class.

Next, the SelectCommand property of the SqlDataAdapter class is used to select the records stored in the SelCom object.The SqlDataAdapter class is present in the System.Data.SqlClient namespace and represents the SQL commands used to modify the SQL ser ver database.

Then, the Open() method of the SqlConnection class is used to establish a connection with the DTDB database. When a connection with the DTDB database is established, the records in the DTCatalog table are retrieved and stored in the dsDetails1 dataset. To do this, the ExecuteNonQuery() method of the SqlCommand class is executed to return the records that are affected by the SQL command stored in the SelStr variable. In this case, all the records in the DTCatalog table are returned by the ExecuteNonQuery() method. The records returned are then stored in the dataset by using the Fill() method of the DbDataAdapter class.

Once the records are retrieved, you can close the connection to the DTDB database by using the Close() method of the SqlConnection class. Finally, the dataset, dsDetails1, is returned when the SearchAll() method is called by a Web service client application. To return the dataset, you use the return keyword.

After creating the Web method, you can test the Web method by pressing the F5 key. The Service1.asmx page is displayed as shown in Figure 29-3.

FIGURE 29-3 The Service1.asmx page

DEVELOPING WEB SERVICES

Chapter 29

647

 

 

 

 

The Service1.asmx page contains a link to the SearchAll() method. Click on the link to view the Service1.asmx page for the SearchAll() method. This page contains an Invoke button. When the user clicks on the Invoke button, the SearchAll() method is executed and the results are displayed as shown in Figure 29-4.

FIGURE 29-4 The results returned by the SearchAll() method

Creating the SrchISBN() Web Method

You can now create a Web method that returns the records from the DTCatalog table with the ISBN number that is passed as a parameter to the Web method. First, write a description for the Web method.

[WebMethod(Description=”This method searches for the details of the book based on

the “ + “ ISBN Number of the book”)]

After writing the description, you need to write the code for the Web method SrchISBN(). The code for the Web method is as follows:

[WebMethod(Description=”This method searches for the details of the book based on the “ + “ ISBN Number of the book”)]

public DataSet SrchISBN(string ISBN)

{

string SelStr;