Tài liệu Creating and Managing Microsoft XML Web Services - Pdf 90

5
Creating and
Managing
Microsoft XML
Web Services
CERTIFICATION OBJECTIVES
5.01 Creating and Consuming an XML
Web Service
5.02 Controlling Characteristics of Web
Methods by Using Attributes
5.03 Creating and Using SOAP Extensions
5.04 Creating Asynchronous
Web Methods
5.05 Controlling XML Wire Format
for an XML Web Service
5.06 Instantiating and Invoking an XML
Web Service

Two-Minute Drill
Q&A
Self Test
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:11 AM
Color profile: Generic CMYK printer profile
Composite Default screen
I
n this chapter, you will learn how to design, create, and use XML web services, and most
importantly from the exam point of view, you will learn how to control the environment
of particular XML web services. You will work with both static and dynamic discovery of

Color profile: Generic CMYK printer profile
Composite Default screen
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
XML Web Services Explained
3
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
Loose Coupling
One of the important concepts in computer
engineering is that software components
should be loosely coupled. That translates
into a design where the components have
knowledge only of each other’s public
methods, and the calls to those methods are
asynchronous.
In order to be able to set up asynchronous
method calls, you need to be able to register
a callback method—that is, a method that
is called when the work is complete. One
option for referring to methods and data items
uses the address where the item is stored in
memory. This concept of being able to refer
to a function by its address (the function
pointer) is a very powerful one, but it is also
fraught with dangers. For example, if the
address changes, the call to the location that
the address points to will most likely end up
running code that will crash the application,
or even the operating system. The designers of

This technique of loosely coupling
components is very resistant to network
problems and can be used to provide scalable
solutions where the objects must be load
balanced.
FROM THE CLASSROOM
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:11 AM
Color profile: Generic CMYK printer profile
Composite Default screen
4
Chapter 5: Creating and Managing Microsoft XML Web Services
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 5
Web Standards and XML
RPC-based environments have been successfully implemented by many different
organizations in the form of the Distributed Component Object Model (DCOM)
from Microsoft, the Common Object Request Broker Architecture (CORBA) from
HP et al., and Remote Method Invocation (RMI) from Sun. These implementations
are designed around binary protocols; however, binary protocols as a group have
inherent problems:

Interoperability The binary protocols are not interoperable, because they
were developed to be monolithic standards within the context of the specific
distributed environment. Translation services can and have been developed,
but these services are not only unwieldy, but they also tend to lose some
information as is normal in any translation process. The problems arise
when different partners have selected a different binary protocol, resulting
in translation problems.


document. XML documents offer the following benefits:

It is easy to use across the Internet.

It is easy to process.

It is easy to create.

It is extensible.

It is platform independent.

It is easy to localize.

It offers a clear data model.
For a full discussion of the protocols, refer to Appendix D.
The adoption of XML by web server and web solution vendors has brought XML
to the forefront as the most important web technology of this decade. XML is also
the technology that is the solution to transmitting documents between partners in
most e-commerce scenarios. Some developers think that XML has yet to prove itself,
but the web world seems to have adopted XML, and you are not likely to go back to
the monolithic environments of the past, with their vendor-specific protocols.
That being said, there are some problems involved in transmitting information on
the Internet that must be solved. Two of the most important concerns are

Performance The client still connects to the Internet mostly through dial-up
connections, resulting in the need to send small amounts of data back and forth
between the client and the web server. This is not a major concern when you
develop for intranets, as they run on high-bandwidth networks; however, it
should be considered when developing for Internet or extranet use.

and the data of the XML web service, thus making the XML web service suitable for
building distributed applications.
An XML web service can be a very simple static service that provides information
to the user, or a fully aggregated system of XML web services that provide a dynamic,
complex software system. Aggregated XML web services are also known as federated
XML web services.
The standards that support XML virtually guarantee that XML web services will
be one of the major development environments for years to come. The level of
adoption of the XML standard and of the technologies that support XML has not
been seen in the Information Systems sector before. For example, an XML web
service written in Visual Basic .NET and exported in IIS can be used by a Common
Gateway Interface (CGI) application written in C++, and the usage is seamless.
Microsoft has made tools and technologies available that enable developers to take
software components and expose them (make them available) as XML web services
without rewriting them through Visual Studio .NET and the .NET Framework.
The use of SOAP guarantees that XML web services are interoperable with
CORBA, DCOM, and any other binary protocols. XML web services can be
hosted and accessed by any computer that supports HTTP and XML. HTTP is
the only communication protocol that is needed, and XML is a markup language
that allows the XML web services to communicate with the XML-based protocols
such as SOAP.
6
Chapter 5: Creating and Managing Microsoft XML Web Services
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 5
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:12 AM
Color profile: Generic CMYK printer profile
Composite Default screen
XML Web Services Architecture

The architecture used for XML Web Services is one in which the XML web service
is loosely coupled to the clients that will use it—the resources of the service and the
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:12 AM
Color profile: Generic CMYK printer profile
Composite Default screen
client are separate and distinct. The communication to and from the service must
meet the Internet standards, and the methods that will be called from a client of
the XML web service must be published for public use and be publicly accessible.
There are three services in the XML Web Services architecture, as is shown in
Figure 5-1.
The service provider hosts the XML web service and is responsible for providing
access to the public interface of the software service. The service consumer is
the client that will bind to the interface of the service provider. Note that in this
architecture, the service consumer is not the end user—it is a software node in an
application. The service broker is a node that is used to locate the service provider
of a specific XML web service.
The interactions in Figure 5-1 are as follows:

Publish service The service provider publishes the XML web service to
a service broker.

Find service The service consumer uses the service broker to find the
service provider.

Bind to service The service consumer binds to the service from the service
provider.
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 5
8

another XML web service.
XML Web Services Architecture
9
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
FIGURE 5-2
The protocols in
the XML Web
Services
architecture
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:12 AM
Color profile: Generic CMYK printer profile
Composite Default screen
10
Chapter 5: Creating and Managing Microsoft XML Web Services
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 5
In the following sections, you will look more closely at the nodes in the XML
Web Services architecture.
XML Web Service Provider
The central role of the XML Web Services architecture is that of an XML web
service provider (I will use the term service provider for short). The service provider
should supply HTTP protocol handling and authentication services. If the service
provider can’t supply these infrastructure services, the XML web service must
implement them on behalf of the provider.
The minimum requirement for the service provider is that it must supply a
protocol listener for the HTTP protocol. A protocol listener is a software component
that waits (listens) for connections using a specific protocol, in this case HTTP. The
service provider must also be able to distinguish between calls to different XML web

how to use UDDI, see Chapter 9.
The service consumer implements a proxy class that is used on the consumer to
hide the details of the XML web service. This makes it possible for the developer to
use the methods of the XML web service as if they were local methods.
XML Web Service Broker
XML web service brokers (service brokers) are used by service providers to
publish the XML web services in the UDDI registry. The service broker provides
the following:

Contact information for the XML web service

A text description for the XML web service

Classification of the XML web service

Links to documentation about the XML web service

The location of the endpoints of the XML web service, stored as URLs
The service consumer uses the service broker to search for an XML web service
and then discover the information that is needed to bind to that XML web service.
The method used by the service broker to make XML web service information
available to service consumers uses the UDDI, which is a distributed registry. It allows
service providers to publish their XML web services, and service consumers to find
information about those published services. UDDI consists of three parts—business
addresses, a list of categories, and technical information. Any XML web service can be
described using these three parts.
XML Web Services Programming Model
The programming model used to build XML web services is based on some key features:

Statelessness The XML web service is stateless. By not storing information

Although you’ll be focusing on the use of VISUAL BASIC .NET for your examples,
XML web services can be built using any of the .NET languages. An XML web
service is an ASP.NET project saved with the file extension .asmx, for which the
methods have been marked to be published as web methods.
An XML web service is made up of four separate parts:

The processing directive

The namespaces

The public class

Methods that are web-callable
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:13 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Creating an XML Web Service
13
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
In the next sections, you will look at how to create an XML web service, as well
as identify the parts of the project. You will create an XML web service that will
convert between metric and imperial measurements.
EXERCISE 5-1
The first XML Web Service
In this exercise, you will build and test a default XML web service; you are going
to continue using this XML web service in other exercises in this chapter.
Step 1. Create a new Visual Basic .NET Project in Visual Studio .NET.
Step 2. Select the ASP.NET Web Service template.

be imported as needed to support additional services.
<WebService(Namespace := "http://tempuri.org/")> _
Public Class Service1
Inherits System.Web.Services.WebService
The preceding segment declares the public class Service1 and inherits
from System.Web.Services.WebService. This makes the class an
implementation of an XML web service.
Public Sub New()
MyBase.New()
'This call is required by the Web Services Designer.
Creating an XML Web Service
15
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:14 AM
Color profile: Generic CMYK printer profile
Composite Default screen
16
Chapter 5: Creating and Managing Microsoft XML Web Services
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 5
InitializeComponent()
'Add your own initialization code after the InitializeComponent() call
End Sub
The constructor in Visual Basic .NET is the
New()
method; use the constructor
to initialize the class.
The preceding constructor calls the base class’s constructor (MyBase.New())

CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
The preceding code is the clean-up code that will iterate through the IComponent
member, calling the Dispose() method on all components that have been added to the
project. Finally the base class’s Dispose() method is called.
' WEB SERVICE EXAMPLE
' The HelloWorld() example service returns the string Hello World.
' To build, uncomment the following lines then save and build the project.
' To test this web service, ensure that the .asmx file is the start page
' and press F5.
'
'<WebMethod()> Public Function HelloWorld() As String
' HelloWorld = "Hello World"
' End Function
The preceding code is the last part of the generated code. It is a sample declaration
of a web method that will return the string “Hello World” to the caller of the method.
The <WebMethod> attribute marks the public HelloWorld() method to be published
and callable. Let’s continue the exercise to see the XML web service in action.
Step 7. Remove the comments from the HelloWorld() method as shown in the
following code segment:
...
' WEB SERVICE EXAMPLE
' The HelloWorld() example service returns the string Hello
World.
' To build, uncomment the following lines then save and build
the project.
' To test this web service, ensure that the .asmx file is the
start page
' and press F5.
'

the HelloWorld link to review the SOAP request and response headers that are used
to call the web method; you can use these headers to build custom communication
between your XML web service and the consumer application, as shown next.
The XML Web Service help pages are great for getting the WSDL document
(the WSDL document will be explained in the “WSDL” section of this chapter).
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:15 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Creating an XML Web Service
19
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
Click Invoke to see the XML document that represents the return value from the
web method.
The default behavior when you call an XML web service directly from a browser
is for the .NET Framework to render the service as an information page, listing all
the web methods that are defined in the service. This Service page forms the basis
for unit testing of the XML web service. For more information on unit testing and
debugging of XML web services, see Chapter 7. One thing you need to do with the
XML web service is to change the namespace from http://tempuri.org/ to http://
xxx.yyy/, where xxx.yyy represents your domain name; you would replace the URI
with one for your organization to ensure that you have a unique namespace.
Step 9. Open the code editor, and locate the <WebService...> element.
Step 10. Modify the <WebService> element by adding the attribute shown
in bold in the following listing to the class definition. This will change the
namespace of the XML web service.
...
<WebService(Namespace:="http://xxx.yyy/")> _
Public Class Service1

Chapter 5: Creating and Managing Microsoft XML Web Services
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 5
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:15 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Creating an XML Web Service
21
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
3.
Add three more web methods after the definition of Cmi, they are named
Ckm (Convert to kilometers), Cfa (Convert to Fahrenheit), and CCe
(Convert to Celsius). The following code segment shows how the code
should look:
<WebMethod()> Public Function Ckm(ByVal mi As Double) As Double
Return (1.609344 * mi)
End Function
<WebMethod()> Public Function CFa(ByVal c As Double) As Double
Return (((c * 9) / 5) + 32)
End Function
<WebMethod()> Public Function CCe(ByVal f As Double) As Double
Return (((f - 32) * 5) / 9)
End Function
4.
The methods must be public in scope and marked with the <WebMethod>
attribute to be available to XML web service consumers.
The conversions used are: 1 mi = 1.609344 km, 1 km = 0.621371 mi, 1 F =
(C*9/5) + 32, and 1 C = (F – 32) * 5/9.

Setting the Web Method Attributes
The <WebMethod> attribute you used in the preceding section has a number of
properties that are used to customize the way the <WebMethod> operates. Table 5-1
describes those properties.
Setting the Web Method Attributes
23
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
Property Description
BufferResponse The BufferResponse property controls how the response from the
<WebMethod> is returned to the client. If the property is set to True
(the default setting), ASP.NET will buffer the entire response before
returning anything to the client. This type of buffering is very efficient.
If the BufferResponse property is set to False, ASP.NET will send
the response in 16KB packets.
CacheDuration The CacheDuration property controls the lifetime of the cached
response. The default setting is 0, which means that caching is disabled
for results. The property is in seconds with 0 turning it off, and any other
value indicating the amount of time the result should be cached.
Description This property supplies the description of the <WebMethod> that will
be supplied on the XML web service Help page.
EnableSession When the EnableSession property is set to True, the <WebMethod>
can use the Session object of WebService.Session to maintain
state between calls. The default setting is False.
MessageName You can uniquely identify an overloaded <WebMethod> by using the
MessageName as an alias. The default value of the MessageName
property is the name of the WebMethod, and changing the property will
publish the new name.
TransactionOption This property enables the XML web service method to participate as the
root object in a transaction using the Microsoft Distributed Transaction

Add to the web method definition a description for the Ckm() method to
read “ This is the miles to km int conversion”.
5.
Add to the web method definition a MessageName to CkmInt.
6.
The code for the Integer version of Ckm should look like the following
code listing:
<WebMethod(CacheDuration:=3, _
Description:="This is the miles to km Integer Conversion", _
MessageName:="CkmInteger")> _
Public Function Ckm(ByVal mi As Integer) As Integer
Return (1.609344 * mi)
End Function
7.
Modify the original Ckm web method by adding attributes to make it unique
as is shown in the following code segment:
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:17 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Setting the Web Method Attributes
25
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 5
<WebMethod(CacheDuration:=3, _
Description:="This is the miles to km Double Conversion", _
MessageName:="CkmDouble")> _
Public Function Ckm(ByVal mi As Double) As Double
Return (1.609344 * mi)
End Function

Public Function CFa(ByVal c As Integer) As Integer
Return (((c * 9) / 5) + 32)
End Function
<WebMethod(CacheDuration:=3, _
Description:="This is the Celsius to Fahrenheit Double Conversion", _
MessageName:="CFaDouble")> _
P:\010Comp\CertPrs8\653-6\ch05.vp
Wednesday, October 30, 2002 9:42:17 AM
Color profile: Generic CMYK printer profile
Composite Default screen


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status