Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
Project 7 Overview
A Web service is any application that has an API that is accessible through the
Web. Web services are also a new standard for creating distributed applications that are language and platform neutral. A Web service achieves this independence from platform and language constraints by using standards such as SOAP, UDDI, and WSDL.
In this section, you will create Web services using Managed Extensions for |
|
C++ and ATL Server library. Managed ExtensionsYfor C++ is used to create |
|
|
M |
Web services when you are creating a LWeb service for the .NET platform. |
|
ATL Server library allows you to createFWeb services for the Windows plat- |
|
E |
|
form without the .NET Framework installed. The ATL Server is also used |
|
T |
|
when you need to create highA-performance Web services and the developer skill set includes the C++ language and the ATL library.
The following are the technologies that I’ll use to build the Web services application (across both the projects):
Managed Extensions for C++
Data access through ADO.NET
ATL Server library
ATL library
Data access using OLE-DB templates
WSDL
UDDI
SOAP
In the first project, you will create a .NET Web service that allows an online store to expose its catalog to members of its affiliate program. The service also allows a member site to place an order for a product on sale at the online store.
Here, you will implement a Web service using Managed Extensions for C++.
In the next project, you will create a Web service that will allow a financial company to expose data on the current exchange rate for currencies of various countries. This Web service will be created using the ATL Server library.
Team-Fly®
Chapter 22
Introduction to
Web Services and
Related
Technologies
Organizations are increasingly relying on the Internet for information and data exchange for online business. In this scenario, developers are constantly faced with the challenge to write applications that enable users to access information and data through any type of device, such as mobile phones and personal digital assistants (PDAs). To enable users to access information and data from any device, various services have been created. However, creating such services for each Web site can be tedious. This problem can be resolved if you can transform existing
Web applications into reusable components.
The introduction of the .NET Framework by Microsoft enables developers to face these challenges and develop applications that can be transformed into reusable, platform-independent Web services so that users can access information and data from Web sites irrespective of the device they are using.
What Are Web Services?
Simply stated, Web services are Web applications that provide particular functionalities, such as application logic, to other applications. You can access a Web service by dissimilar systems by using standard Internet protocols, such as XML and HTTP. This provision enables programs running on any platform and language to access a Web service. A Web service can be deployed over the Internet so that it can be used by other applications. A user can then access the deployed Web service through client applications by using the URL of the Web server where the Web service is deployed. An example of a Web service is a reservation service on a Website of an airlines company that exposes methods, that can be used by third party Web portals to make and cancel reservations on its flights.
Though various component-based object models already exist, such as Remote Method Invocation (RMI), Internet Inter-Orb Protocol (IIOP), and Distributed Component Object Model (DCOM), these models depend on an object-model- specific protocol. This disadvantage has been overcome by the Web service model, which extends these models to communicate with XML and Simple Object Access Protocol (SOAP).
INTRODUCTION TO WEB SERVICES |
Chapter 22 |
673 |
|
|
|
|
|
Web services use various technologies, such as SOAP and Hypertext Transfer Protocol (HTTP), to make data available on the Web. Web services are more associated with business data and they make available all business objects, such as COM, to SOAP calls over HTTP. SOAP calls are basically remote function calls that are used to invoke methods exposed by a Web service. The output, which is in the form of XML data, is then transferred to the users. Figure 22-1 illustrates the functioning of a Web service.
FIGURE 22-1 The functioning of a Web service
You would agree that in this form of data transfer, it is important for a developer who is creating a client for your Web service to know about the functions, parameters, and return values of your Web service. There are several languages, such as Network Accessible Service Specification Language (NASSL), SOAP Contract Language (SCL), and Web Service Description Language (WSDL), that are used as a common standard for Web services to ensure that developers are aware of the Web service semantics. Among these, WSDL is the language that has been chosen by both Microsoft and IBM as the standard Web service language.
Now that you are familiar with Web services, I will explain Web service architecture.
Web Service Architecture
Typically, Web services are built by using programming languages, such as VB.NET, C#, C++, and Java. After Web services are created, they are exposed by using SOAP or the built-in Web services support of .NET. A client, regardless of the language it is written in, can invoke a Web service by processing the WSDL document associated with the desired Web service. Next, based on the description
INTRODUCTION TO WEB SERVICES |
Chapter 22 |
675 |
|
|
|
|
|
Web Services and XML
Extensible Markup Language (XML) provides the basic format for representing data in Web services. XML was chosen for this purpose for the following reasons:
XML is not platform-specific.
XML is not vendor-specific.
XML is easy to create and parse.
However, there is an important disadvantage of using XML for representing data that hinders interoperability. The problem with XML is that it does not account for the standard set of data types and does not provide scope for additional data types. To resolve this problem, W3C XML Schema Definition (XSD) is being used. XSD is a standard for specifying built-in data types. It also provides a language that you can use to define new data types.
XSD is used as a type system for the Web services platform. While building Web services, you must translate data types from the language that you are using to build the services to XSD types. This would ensure that you are conforming to the Web services standard.
I will now discuss XML in detail and then discuss using XML with .NET.
Introduction to XML
The Internet has constantly been improving and gaining importance as an efficient and effective medium for communication and information exchange. Organizations are increasingly depending on the Web to conduct business, and as a result, the need for communicating information across the world is increasing. To find a solution to this, it is imperative that Web applications present data in a standard format that’s compatible with a majority of hardware and software platforms. The World Wide Web Consortium (W3C) has offered the much-needed solution in the form of XML, one of the latest Web technologies. XML is a markup language defined and standardized by the W3C that prescribes a format for structuring and describing data on the Web. The data presented in XML format remains uniform and independent of applications or vendors.
Most new software, including Microsoft’s .NET Framework, provides inherent support for XML.
XML provides a format to present structured data. The data to be presented is stored in XML documents. These XML documents are similar to databases in the
676 Project 7 CREATING WEB SERVICES
sense that they allow you to store data. XML documents, however, store data as plain text to enable multiple platforms to understand and interpret the data. Thus, XML provides a standard interface for interchanging data across Web applications, irrespective of platform.
XML allows Web application designers to create their own customized tags, enabling the definition, transmission, validation, and interpretation of data between applications.
W3C has laid down some basic XML-related specifications. Some of the common specifications related to XML are listed as follows:
Document Type Definition (DTD). DTDs specify the rules for XML documents that make it easier for others to understand the structure and logic of your XML documents.
XML Namespaces. When you define multiple elements in an XML document, you use the XML Namespaces to avoid conflicting names and to assign a unique name to each element.
Document Object Model (DOM). DOM enables navigation and modification in an XML document, including adding, updating, or deleting content of elements. This also allows you to access XML data programmatically.
Extensible Stylesheet Language Transformations (XSL/T). XSL files are stylesheets provided by the W3C to format XML documents.
XML Schemas. XML schemas can be considered as a superset of DTDs and are also used to define the structure of XML documents.
Document Type Definition (DTD)
DTD represents a set of rules that define the structure and logic of XML documents. The documents that store these rules are called DTD documents (DTDs) and have the extension .dtd.
To better understand the concept of DTDs, compare them with the creation of tables in a database. When you create a table in a database system, you specify the columns, the data types for different columns, the validation rules for data within columns, and so on. Similarly, you can specify rules that can be used in XML documents, such as tags and attributes, by using a DTD. DTDs can be considered to be rulebooks for XML documents.
INTRODUCTION TO WEB SERVICES |
Chapter 22 |
677 |
|
|
|
|
|
TIP
ItÕs not essential for you to create a DTD for your XML documents. However, a DTD can be important to users who need to understand the structure of your XML documents or who need to create an XML document similar to the one youÕve already created. These users can refer to your DTD document to understand the structure and logic of your XML documents.
When you create a DTD document for an XML document, the XML document is checked against the rules specified in the DTD document. If the XML document adheres to all the DTD rules, the document is considered valid. Otherwise, the XML document fails to generate the desired output.
To understand how to create a DTD, consider the following DTD document named Employees.dtd. You can create this document by typing the code in Notepad and saving it as a DTD file:
<!ELEMENT Employees (Employee)+>
<!ELEMENT Employee (Id, LastName, FirstName)> <!ELEMENT Id (#PCDATA)>
<!ELEMENT LastName (#PCDATA)> <!ELEMENT FirstName (#PCDATA)>
The DTD document declares five elements: Employees, Employee, Id, LastName, and FirstName. Each element declaration statement has three parameters: the keyword ELEMENT, the name of the element, followed by the content type of the element. Therefore, when you consider the preceding code, the first declaration creates an element called Employees, and defines the content type as (Employee)+. This means that the Employees element can contain one or more Employee elements.
NOTE
The + symbol indicates that the Employees element can contain multiple Employee elements.
678 Project 7 CREATING WEB SERVICES
Similarly, the content type for the Employee element is declared such that it must contain three elements: Id, LastName, and FirstName.
The Id, LastName, and FirstName elements can each contain character data. Character data is represented by the keyword (#PCDATA).
If you want to use the rules declared in a DTD, you must include it with the XML document that you create. To include the DTD document you just created in an XML document, use the following statement:
<!DOCTYPE Employees SYSTEM “Employees.DTD”>
Now I will show you how you can include a DTD in XML code, and also the effect the DTD has on the XML document. Create an XML document (in Notepad) named EmployeeDTD.xml, and type the following code:
<?xml version=”1.0”?>
<!DOCTYPE Employees SYSTEM “Employees.DTD”>
<Employees>
<Employee>
<Id> E001 </Id>
<Name> Robert Brown </Name>
</Employee>
</Employees>
When you view this XML document in a browser, the browser reports an error because the XML document isn’t valid. According to the DTD, the <Employee> element contains three elements: <Id>, <LastName>, and <FirstName>. However, the XML document that you created doesn’t conform to this rule and, therefore, doesn’t pass the validation test.
NOTE
When you view the file in the browser, you might actually see an XML-based tree structure, because MSXML is a DOM-enabled parser. The error will occur if you use XML DOM objects and set the validateOnParse flag to True in a script. Therefore, to access an XML document and to validate it, you need to use the DOM objects provided by MSXML.
INTRODUCTION TO WEB SERVICES |
Chapter 22 |
679 |
|
|
|
|
|
XML Namespaces
You can define your own elements to describe the data while creating XML documents. You can also use elements that you define outside your XML document, such as in a DTD. However, defining multiple elements might create a problem: you might end up defining the same element twice. For example, when defining the data structure to present employee details, you might define the <Name> element twice, once to qualify the employee name and a second time to qualify the department name. This is not unlikely when you have a large number of elements to define. This situation leads to name collisions and your XML document may not be processed correctly. To avoid such situations, W3C recommends the use of XML namespaces. XML namespaces are a collection of unique elements identified by Uniform Resource Identifiers (URIs) and are declared by using the keyword xmlns.
You can declare an XML namespace for the <Name> element that defines the department name, using the following statement:
xmlns:DepartmentName=”http://www.dn.com/dn”
In this statement, DepartmentName is an alias for the <Name> element, and
“http://www.dn.com/dn” is the URI.
Later, when you want to use the <Name> element to qualify a department name, you must prefix it with the alias DepartmentName, as in the following statement:
<DepartmentName: Name>
NOTE
When you specify a namespace URI, the browser does not search the URI or the documents at the specified URL. In fact, the URI just serves as a unique identifier.
XML Document Object Model (DOM)
To access and display XML data in your Web applications, you need to use the XML Web server control and set its specific properties at design time. In certain situations, you might want to display the XML data based on some conditions. In such cases, you’ll have to access the XML data programmatically. To do that, you must employ the XML Document Object Model (DOM). The DOM is an
