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

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

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

618 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

Flowchart for the Search Form

In the Search page, the user selects a radio button to choose criteria to search for information about the site. In addition, the user specifies the value for the criteria. Figure 27-12 shows this functionality of the Search page in the form of a flowchart.

FIGURE 27-12

The

 

 

Y

 

L

 

F

 

M

 

A

 

E

 

T

 

 

flowchart for the Sear ch form

 

Summary

In this chapter, you learned about the case study and design of the Web portal project. The design of the project includes creating the design of the application and the supporting databases. Based on the design of the application, you created the flowcharts for the Web forms. You will learn to create the application in the forthcoming chapters.

Team-Fly®

Chapter 28

Exploring ASP.NET

Web Services

620 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

The ever-changing business scenario has become more and more dependent on the Web for any data transaction or for communication between applications.

Because of this dependency, the focus of developers is shifting from creating a desktop application to an application that can access data through the Internet. These applications are mainly distributed applications. Distributed applications are scalable applications in which data is shared across applications.

For example, a distributed application consists of a client application that interacts with a middleware application, which contains the business logic for the entire business solution that you create. This intermediate application in turn interacts with the underlying databases that store the data for the application. Therefore, as you can see, a business solution on the whole comprises a number of applications and databases.These applications and databases may be present on a single computer. However, in large-scale business operations, these applications are generally distributed across different computers connected over a network. In such cases, these applications may be created using different programming languages and, in the worst scenario, on different platforms.

To build a complete 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.

In this chapter, you will be introduced to the basics of ASP.NET Web services. In addition, you will learn about the architecture and working of a Web service. Next, you will be introduced to the technologies used in a Web service. These technologies include XML (Extensible Markup Language), SOAP (Simple Object Access Protocol ), WSDL (Web Services Description Language), and UDDI (Universal Description Discovery and Integration). Finally, you will learn to create a simple Web service in a Visual Studio .NET.

EXPLORING ASP.NET WEB SERVICES

Chapter 28

621

 

 

 

 

Introduction to ASP.NET Web Services

You were introduced to Web services in Chapter 1, “Overview of the .NET Framework,” in the section “Introduction to the .NET Framework.” In this chapter, I will discuss Web services in detail.

As discussed earlier, a Web service is used to integrate different applications that access data through the Internet. To do this, methods in a Web service are called over the Internet, which can then be accessed by applications developed on different platforms.This implies that a Web service is a reusable component, such as a method, that can be used by any Web application running on the Internet. In addition, a Web service can be used by a Windows application.These applications are called Web service client applications.

Before developing Web services, DLL (Dynamic Link Library) files or components were used to create distributed applications.However, to communicate with a client application, these components use protocols such as RPC (Remote Procedure Call ), DCOM (Distributed Component Object Model ), RMI (Remote Method Invocation), or IIOP (Internet Inter-ORB Protocol ). Therefore, communication between a client application and a component depends on various factors, such as hardware platform, programming languages, vendor implementations, and dataencryption schemes.This implies that transferring data between two applications requires a similar infrastructure at the two application sites. However, this scenario cannot be obtained while working with Internet applications. An Internet application can be accessed by various client applications.Therefore, it is essential to build components that can be used to create distributed applications that can be accessed from various platforms. To do this, you can use Web services. Web services allow you to create platform independent distributed applications. The ability to create distributed applications that are independent of the platform is mainly due to the support of a Web service for Internet standards, such as HTTP and XML.

In addition to integrating applications built on different platforms, a Web service allows you to integrate business solutions for one or more organizations. You can create a Web service specific for your organization or customize a Web service created by another organization to your specific requirements. You can also create a Web service that can be used by a single application or be called on the Internet to be used by multiple applications. To call a Web service from the Internet, the

622 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

Web service client needs to know the location of the Web service and the input and output information required for accessing the Web service.

A Web service that you create can be a simple one-method service. For example, consider a situation in which you want to know the current time in a particular state. In this case, you can create a method in a Web service that returns the current time in the state that you choose. You can pass the state for which you want to know the current time as a parameter to the method. The method created in a Web service is called a Web method. You will learn to about Web methods in detail later in this chapter.

In addition to performing simple tasks by using a Web service method, you can create Web methods that perform complex tasks. In such cases, a Web service may consist of several Web methods performing complex tasks. For example, consider a situation in which you need to validate the username and password entered by a user to log on to a site.This is a very common scenario, as almost all Web sites require a method to validate the username and password. Therefore, in such a case, you can create a Web service that performs data validations. In addition, the Web service that you create can be used to validate data for various Web sites. You can then customize the Web service according the requirements based on your database schema. In this case, the Web site that uses the Web service to perform data validations is called a Web service client application, and the application that hosts the Web service is called a Web service provider application.

The data validation scenario that I discussed involves various applications and an underlying database. For example, the Web site that needs to perform data validation is a Web application, which interacts with a database. The database may be created using SQL, Access, Oracle, or any other RDBMS (Relational Database Management System). In addition, for the Web application to perform validations based on the data in the database, the Web application uses another application. In this case, another application required to perform validations is a Web service. Therefore, as you can see, multiple applications are involved in a complete business solution. To integrate these applications, a Web service can be used. The next sections will show how a Web service can provide integration of multiple applications.

A Web service uses XML and any other Internet standard, such as HTTP, to create an infrastructure that helps you to integrate applications build on multiple platforms. Because of the support of Web services for XML, these Web services are often referred to as XML Web services.

EXPLORING ASP.NET WEB SERVICES

Chapter 28

623

 

 

 

 

An XML Web service uses SOAP messaging to communicate and transfer data across applications. In addition, SOAP messaging allows a great deal of abstraction between a Web service client and a Web service provider. This implies that using the XML messaging technique allows you to create a client and a service provider independent of each other.

By now, you must have got an idea of the need for a Web service. I will now discuss the architecture of a Web service.

Web Service Architecture

As discussed earlier, a Web service can be an intermediate application that allows a Web service client application to access data from an underlying database. To do this, the Web service architecture internally consists of four layers.These layers are explained in the following bulleted list.

The data layer. The data layer is the first layer in the Web service architecture. This layer contains the data that the Web client application needs to access.

The data access layer. The layer above the data layer is the data access layer. The data access layer contains the business logic or the code that allows the Web client application to access the data in the data layer. In addition to storing data, the data access layer is used to secure the data present in the data layer.

The business layer. The third layer in the Web service architecture is the business layer. This layer contains the code required for implementing the Web service. The business layer in turn is divided into business logic and business façade layers.The business logic layer contains all the services provided in a Web service. However, the business façade layer acts as an interface of the Web service.

The listener layer. The layer closest to the Web service client is the listener layer. It is the main layer used by the Web service client to communicate with the Web service. When a Web service client wants to access a Web method present in a Web service, the Web service client sends in a request. This request is received by the listener layer. The listener layer then interprets the request sent by the Web service client application. When the client request is processed and the Web service returns the response in the form of an XML message, the listener layer forwards this XML message to the Web service client.

624 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

The Web service architecture is explained in Figure 28-1.

FIGURE 28-1 The Web service architecture

After discussing the four-layered structure of a Web service, I will look at the working of the Web service based on the Web service architecture.

Working of a Web Service

The working of a Web service involves the client application sending a request for a service. The request made to the Web service is in the form of an XML message using a transfer protocol, such as HTTP. This scenario is somewhat similar to a method call statement that you use to call a particular method. The request for the service is passed to the listener layer, which forwards the request to the Web service provider application. The request is then processed by the Web service provider application. Processing of the request includes the data access layer to retrieve the data requested by the client application.This data is then passed to the listener layer, which in turn forwards the data to the client application. Figure 28-2 shows the working of a Web service.

EXPLORING ASP.NET WEB SERVICES

Chapter 28

625

 

 

 

 

FIGURE 28-2 The working of a Web service

I will now discuss the working of a Web service in detail. When a client application sends a request for a service, you may need to pass arguments. To pass arguments over the network, the arguments are packaged as a SOAP message and passed to the Web method by using a network protocol. You will learn about SOAP in detail later in this chapter.

Then, the Web service decodes the SOAP message to retrieve the arguments passed to the Web method. Once the arguments are passed to the Web method, the method is executed and the return value is passed to the Web client application.

Having learned about the working of a Web service, you can look at the technologies that are used by a Web service.

Technologies Used in Web Services

You can create Web services by using any language provided by the .NET Framework, such as Visual C# .NET, Visual Basic .NET, and Visual C++ .NET. However, for an application to be able to access a Web service, the client application needs to meet certain requirements. These requirements include a standard

626 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

format for describing Web services, a standard format for representing data transfer, and a standard for sending methods and the results returned by the methods across the network. In addition, to be able to access a Web service, the Web client application needs to identify a method for locating the Web service and passing inputs to the Web methods.

As a solution to these requirements, technologies such as XML, WSDL, and SOAP were developed. The following sections discuss these technologies in detail.

XML in a Web Service

XML is a markup language used to describe data in a particular format.This data can be accessed by any application built on any platform. XML allows you to transfer data in a format that is independent of the platform. Therefore, XML is a widely used technology that transfers data across the Internet applications. XML documents store data in the form of text. This makes the XML document easily understood by applications built on different platforms. Moreover, content stored in an XML document is easily transferred over the network.

Having discussed XML in general, you can see how a Web service uses XML. When a Web service client application calls a Web service, the client application passes arguments to the Web method.The Web service processes the Web methods and returns a result to the client application. Because the client application can be built using any platform, the data returned by the Web service is in the form of XML.

WSDL in a Web Service

WSDL is a markup language that defines a Web service. WSDL is an XML file that contains information about a Web service. This information includes the Web services called by a Web site, the methods included in each of the Web services, and the parameters that you need to pass to the Web methods. In addition, WSDL includes information about the results returned when a request is processed by a Web service. For example, WSDL defines the type of the values returned by a Web method.Therefore, WSDL is a vocabulary defined for the creation of a Web service that the developer may need to use while creating a Web service.

EXPLORING ASP.NET WEB SERVICES

Chapter 28

627

 

 

 

 

In addition to storing information about the Web methods, WSDL stores information about the format used by a user to access a Web service and specifies the location at which the Web service is available. Therefore, WSDL describes the entire mechanism involved in the transfer of data from a Web service client to the Web service and vice versa.

For example, a Web service client application needs to call a Web method that validates the username and password entered by the user. The Web method is created in a Web service. To call this Web method, the Web service client sends a request to the Web service. The request that is sent to the Web method is specified by WSDL.The request is sent to the Web service in the form of XML messages. In this case, WSDL stores the format in which the request is sent.

In addition, when a Web method is called, you need to pass the username and password as parameters. The information about the type and the format of the parameters is stored in a WSDL file. When the request is processed and the result is returned, WSDL stores the format and other information about the results returned.

SOAP in a Web Service

To transfer data from a Web service client to a Web service and vice versa, the transfer protocol used is SOAP. SOAP is a protocol based on XML that is used by a client application to access a Web service. In addition to XML, SOAP uses HTTP for the transfer of data. When a client sends a request, the request is in the form of a SOAP message. The SOAP message also includes the parameters and the method call statement. Based on this information in the SOAP message, the appropriate Web method is called.

As discussed earlier, SOAP is a standard protocol used for communication between a Web service client and a Web service. However, SOAP does not define syntax to be followed while transferring data. Instead, SOAP provides a mechanism for packaging data to be transferred across a network. In addition, SOAP is a transfer protocol based on simple Internet standards. The transfer of data using SOAP takes place in the form of a SOAP package. A SOAP package includes an envelope that encapsulates the data to be exchanged.

In addition to these technologies, Web services uses UDDI to identify the Web services provided by various Web service providers. I will now discuss UDDI in detail.