Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

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.