Tải bản đầy đủ (.pdf) (34 trang)

Tài liệu Module 9: Implementing a Nonstandard Web Services doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (887.78 KB, 34 trang )









Contents
Overview 1
HTML Screen Scraping Web Services 2
Aggregating Web Services 13
Lab 9: Implementing an Aggregated Web
Service 17
Review 28

Module 9: Implementing
a Nonstandard Web
Service


Information in this document, including URL and other Internet Web site references, is subject to
change without notice. Unless otherwise noted, the example companies, organizations, products,
domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious,
and no association with any real company, organization, product, domain name, e-mail address,
logo, person, places or events is intended or should be inferred. Complying with all applicable
copyright laws is the responsibility of the user. Without limiting the rights under copyright, no
part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
otherwise), or for any purpose, without the express written permission of Microsoft Corporation.


Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.

 2001 Microsoft Corporation. All rights reserved.

Microsoft, MS-DOS, Windows, Windows NT, Active Directory, Authenticode, Biztalk,
Intellisense, Jscript, MSDN, PowerPoint, Visual Basic, Visual C++, Visual C#, Visual Studio,
Win32, and Windows Media are either registered trademarks or trademarks of Microsoft
Corporation in the U.S.A. and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.


Module 9: Implementing a Nonstandard Web Service iii

Instructor Notes Module 9: Implementing a Nonstandard
Web Service
This module covers two examples of Web Services that are not simple stand-
alone network accessible application programming interfaces (APIs).
After completing this module, students will be able to:
!
Explain how to implement a HTML screen scraping Web Service.
!
Identify the scenarios where aggregated Web Services are appropriate.
!
Implement an aggregating Web Service that uses multiple Web Services.


Materials and Preparation
This section provides the materials and preparation tasks that you need to teach
this module.
Required Materials
To teach this module, you need Microsoft
®
PowerPoint
®
file 2524A_09.ppt.
Preparation Tasks
To prepare for this module:
!
Read all of the materials for this module.
!
Practice all of the demonstrations.
!
Complete the lab.

Demonstration
This section provides demonstration procedures that will not fit in the margin
notes or are not appropriate for the student notes.
Example of an Aggregated Web Service
!
To demonstrate the NorthwindClient application
1. Start the application NorthwindClient.exe, which can be found in the folder
<install folder>\Labfiles\Lab09\Solution\NorthwindClient\bin\Debug.
2. In the From list, click Woodgrove Online Bank.
3. In the To list, click Contoso Micropayments.
4. Click Transfer.
5. Explain that $100 has been transferred from an account at the Woodgrove

bank to an account at the micropayment service, named Contoso.
6. Explain that the Northwind Traders Web Service took care of all the details
of managing the transfer, including retrieving routing numbers, etc.

Presentation:
45 Minutes

Lab:
90 Minutes
iv Module 9: Implementing a Nonstandard Web Service

!
To explain the Northwind Traders Web Service implementation
1. In Microsoft Visual Studio .NET, open the <install
folder>\WebServicesSolution\Northwind\Northwind project.
2. Open Traders.asmx.cs.
3. Explain the implementation of GetTModelEndPoints, GetTransferSinks
and GetTransferSources methods.
4. Explain the EFTTransfer method.
a. Describe how the Northwind Traders Web Service interacts with the
Contoso and Woodgrove Web Services.
b. Explain how the binding information is used.

!
To explain the implementation of the CreditAccount method in the
Contoso Web Service
1. Open the <install folder>\WebServicesSolution\Contoso\Contoso project.
2. Open Micropayment.asmx.cs.
3. Explain the implementation of CreditAccount method.
a. Explain routing information. (It is information required by a financial

institution for electronically transferring funds to an account at another
financial institution.)
b. Explain that the Contoso Web Service is a consumer of the Woodgrove
Web Service.

!
To explain the implementation of the AuthorizeFundsTransfer method
in the Woodgrove Web Service
1. Open the <install folder>\WebServicesSolution\Woodgrove\Woodgrove
project.
2. Open Bank.asmx.cs.
3. Explain the implementation of AuthorizeFundsTransfer method.
• Explain the information that is contained in the EFTBindingInfo class.

!
To show that money is transferred between the accounts
1. Run <install folder>\Labfiles\Lab07\Solution\Woodgrove and Contoso
Account Manager\bin\Debug\WebServiceClient.exe.
Leave the default accounts selected and note the account balance.
2. Switch to the NorthwindClient.exe application.
3. Transfer funds from the Woodgrove Online Bank to the Contoso
Micropayment Service.
4. Switch to the WebServiceClient.exe application.
5. Click Update Account Info.
6. Point out that the balance has been reduced by $100.

Module 9: Implementing a Nonstandard Web Service v

Module Strategy
Use the following strategy to present this module:

!
HTML Screen Scraping Web Services
For many students the concept of a virtual Web Service will not be intuitive.
Explain that this section is important because it is unlikely that owners of
most of the data on the Internet will ever provide access to their data
through Web Services. However, making this data accessible to clients
through a Web Service is a useful paradigm. Because consumers interact
with a Web Service through a proxy class, if the proxy class simply
retrieves the raw data and the parses the data locally, the consumer need not
be aware that the processing takes place on the client and not on the server.
A detail that you should emphasize is that screen scraping Web Service
proxies can only communicate using the HTTP-GET protocol.
!
Aggregating Web Services
Explain that Web Services can be viewed as sets of functionality. There is
no reason why these sets of functionality should not be aggregated to
provide richer functionality. The module discusses a number of models for
aggregating Web Services. Each model should be analyzed and its areas of
application should be discussed. You must teach this section by basing it on
the final lab scenario.


Module 9: Implementing a Nonstandard Web Service 1

Overview
! HTML Screen Scraping Web Services
! Aggregating Web Services

*****************************
ILLEGAL FOR NON

-
TRAINER USE
******************************
In some scenarios, it may be unnecessary to actually implement a standard Web
Service. In other scenarios your Web Service might to reuse the functionality of
other Web Services as part of its implementation, and thereby assume the role
of a Web Service consumer. This module shows you how to implement Web
Service based solutions for such special scenarios.
After completing this module, you will be able to:
!
Explain how to implement a HTML screen scraping Web Service.
!
Identify the scenarios where aggregated Web Services are appropriate.
!
Implement an aggregating Web Service that uses multiple Web Services.

Topic Objective
To provide an overview of
the module topics and
objectives.
Lead-in
In this module, you will learn
about implementing
nonstandard Web Services
such as HTML screen
scraping and aggregating
Web Services.
2 Module 9: Implementing a Nonstandard Web Service

"

""
"

HTML Screen Scraping Web Services
! Regular Expressions in the .NET Framework
! Using Regular Expressions in WSDL Documents
! Demonstration: Screen Scraping an HTML Document

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Most data on the Web today is available only as unstructured HTML
documents. Many of the Web pages contain valuable information, but retrieving
information from these pages requires that you parse HTML documents to
extract data.
Writing code to parse data is a tedious and error prone process, if the data is not
organized in well-defined fields. A useful way to reduce the effort involved is
to use an engine that can support regular expressions, which can be used to
parse data.
Microsoft
®
has extended the standard Web Service Description Language
(WSDL) grammar to allow the use of regular expressions to specify the
information in a document that is to be extracted. The Web Services defined by
these WSDL documents do not actually exist. Instead, after you create the
WSDL file, you can generate a Web Service proxy using that file. The client
that uses this proxy class can now retrieve information from a Web page as if
the page is a real Web Service. In such cases, there is no code to be written or

maintained on the Web Server. From the perspective of the Web Server, only
Web pages are served to clients. When you extract information from an HTML
document using this technique, it is known as HTML screen scraping.
Topic Objective
To introduce the topics in
this section.
Lead-in
In this section, you will look
at how to build HTML
screen scraping Web
Services that extract
information from HTML
documents.
Delivery Tip
The information in the
student notes is more than
introductory material.
Therefore, ensure that you
cover this material.
Module 9: Implementing a Nonstandard Web Service 3

Regular Expressions in the .NET Framework
! The Regex Class
! The Match Class
! The MatchCollection Class

*****************************
ILLEGAL FOR NON
-
TRAINER USE

******************************
Regular expressions provide a powerful, flexible, and efficient method for
processing text. The extensive pattern-matching notation of regular expressions
allows you to quickly parse large amounts of text to find specific character
patterns; to extract, edit, replace, or delete text substrings; or to add the
extracted strings to a collection in order to generate a report. Regular
expressions are an indispensable tool for many applications that manipulate
strings, such as HTML processing, log file parsing, and HTTP header parsing.
Microsoft .NET Framework regular expressions are designed to be compatible
with Perl 5.0 regular expressions. The .NET Framework regular expression
classes are found in the System.Text.RegularExpressions namespace.
The Regex Class
The Regex class represents a read-only regular expression. The following
example creates an instance of the Regex class and defines a simple regular
expression when the object is initialized.
Regex r;
r = new Regex(@"\s2000");

In the preceding code, the @ prefix is added to the string to prevent the \
(backslash) character from being treated as an escape character.
Topic Objective
To introduce the regular
expressions supported in
the .NET Framework.
Lead-in
Regular expressions provide
a powerful, flexible, and
efficient method for
processing text.
Delivery Tip

Emphasize that screen
scraping technology is not
restricted in its use to Web
pages.
4 Module 9: Implementing a Nonstandard Web Service

The Match Class
The Match class represents the first match when a regular expression is applied
to an input string. In the following example, the instance of the Match class is
returned by the Match method of the Regex class. The Match method finds the
first match in the input string. The example uses the Success property of the
Match class to find out if a match was found.
Regex r = new Regex("abc");
Match m = r.Match("123abc456");
if (m.Success)
{
Console.WriteLine("Found match at position " + m.Index);
}

Escaped characters
Most of the important regular expression language operators are unescaped
single characters. The escape character \ (a single backslash) notifies the regular
expression parser that the character following the backslash is not an operator.
For example, an unescaped
*
(
asterisk
) is a repeating quantifier, but an escaped
*
is treated as the literal character

*
.
The following table lists the character escapes that are recognized in regular
expressions.
Escaped character Meaning

ordinary characters Characters other than ., $, ^, {, [, (, |, ), *,
+, ?, and \ match themselves.
\a Matches a bell alarm (\u0007).
\b Matches backspace (\u0008) if in [ ]
character classes; otherwise, in a regular
expression, \b denotes a word boundary
(between \w and \W characters).
\t Matches a tab (\u0009).
\r Matches a carriage return (\u000D).
\v Matches a vertical tab (\u000B).
\f Matches a form feed (\u000C).
\n Matches a new line (\u000A).
\e Matches an escape (\u001B).
\040 Matches an ASCII character as octal (up
to three digits). For example, \040
represents a space.
\x20 Matches an ASCII character using
hexadecimal representation (exactly two
digits).
Module 9: Implementing a Nonstandard Web Service 5

(continued)
Escaped character Meaning


\cC Matches an ASCII control character; for
example, \cC is CTRL+C.
\u0020 Matches a Unicode character by using a
hexadecimal representation (exactly four
digits).
\ When \ (backslash) is followed by a
character that is not recognized as an
escaped character, the \ matches that
character. For example, \* is the same as
\x2A.

Character classes
Character classes are the set of characters that define the substring to match.
The following table summarizes character matching syntax.
Character class

Meaning


.

Matches any character except \n unless the
Singleline option is specified.

[aeiou]

Matches a single character included in the
specified set of characters.

[^aeiou]


Matches any single character that is not in
the specified set of characters.

[0-9a-fA-F]

Matches any character in the specified
ranges.

\w

Matches any word character. Same as [a-
zA-Z_0-9].

\W

Matches any nonword character. Same as
[^a-zA-Z_0-9].

\s

Matches any white-space character. Same
as [\f\n\r\t\v].

\S

Matches any non-white-space character.
Same as [^\f\n\r\t\v].

\d


Decimal digit. Same as [0-9].

\D

Nondigit. Same as [^0-9].


For more information, see the topic Regular Expression Language Elements
in the .NET Framework SDK documentation.
6 Module 9: Implementing a Nonstandard Web Service

The MatchCollection Class
The MatchCollection class represents a sequence of successful non-
overlapping matches when a regular expression is applied to an input string.
Instances of MatchCollection are returned by the Regex.Matches property.
The following example uses the Matches method of the Regex class to fill an
instance of the MatchCollection class with all the matches found in the input
string. The following example code copies the match collection to a string array
that holds all of the matches, and an integer array that indicates the position of
each match in the input string:
MatchCollection mc;
string[] results = new String[20];
int[] matchposition = new int[20];

Regex r = new Regex("abc");
mc = r.Matches("123abc4abcd");
for (int i = 0; i < mc.Count; i++)
{
results[i] = mc[i].Value;

matchposition[i] = mc[i].Index;
}

Module 9: Implementing a Nonstandard Web Service 7

Using Regular Expressions in WSDL Documents
! Create a WSDL Document Manually
! Specify the Output Message Format in the output
Element Within the binding Element
! Example
<urt:text>
<urt:match name="LineItem"
pattern="&lt;tr&gt;(.*)?&lt;/tr&gt;"
repeats="*"/>
</urt:match>
</urt:text>
<urt:text>
<urt:match name="LineItem"
pattern="&lt;tr&gt;(.*)?&lt;/tr&gt;"
repeats="*"/>
</urt:match>
</urt:text>
<urt:text>
<urt:match name="nameOfElement"
pattern="regular expression "/>

</urt:text>
<urt:text>
<urt:match name="nameOfElement"
pattern="regular expression "/>


</urt:text>

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
If you want to consume a HTML file or any flat file as if it were a Web Service
by creating a Web Service proxy, you need a service description. In cases where
you apply screen scraping to flat files, a WSDL document cannot be
automatically generated for creating the proxy. Therefore, you have to manually
construct the WSDL document.
The part that is of interest to us in a manually created WSDL document is the
format of the output messages that will be generated by the proxy. The output
message format is specified in the output element within the binding element.
The output is simply an XML document with the following structure:
<urt:text>
<urt:match name="nameOfElement"!
pattern="regular expression ">

</urt:text>

The pattern attribute of the match element is a Perl 5.0 compatible regular
expression. The match elements can also have nested match elements to
perform further pattern matching within a match.
The following is an example of a regular expression code in a WSDL
document:
<urt:text>
<urt:match name="LineItem"

pattern="&lt;tr&gt;(.*)?&lt;/tr&gt;"
repeats="*"/>
</urt:match>
</urt:text>

In the preceding code, the rows from an HTML table are extracted.
Topic Objective
To explain how regular
expressions can be used in
WSDL documents.
Lead-in
If you want to consume a
flat file as if it were a Web
Service by creating a Web
Service proxy, you need a
service description.
Example
8 Module 9: Implementing a Nonstandard Web Service

Demonstration: Screen Scraping an HTML Document

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
In this demonstration, you will see how to screen scrape an HTML document
using WSDL and Web Service proxies.
Consider the following HTML document:


Topic Objective
To demonstrate how to
screen scrape an HTML
document.
Lead-in
In this demonstration, you
will see how to screen
scrape an HTML document
using WSDL and Web
Service proxies.
Delivery Tip
Show the example XML
document to students using
the file <install
folder>\Democode\Mod09\
sales.html.
Module 9: Implementing a Nonstandard Web Service 9

1. <html>
2. <body>
3. <center>
4. <h1>Northwind Traders Financial Report</h1>
5. <table border="1" cellpadding="5">
6. <tr><th>Region</th><th>Q1</th><th>Q2</th><th>Q3</th>
<th>Q4</th></tr>
7. <tr><td>NorthWest</td><td>10,000</td><td>13,500</td>
<td>14,200</td><td>11,000</td></tr>
8. <tr><td>Midwest</td><td>81,100</td><td>76,500</td>
<td>82,500</td><td>81,000</td></tr>
9. <tr><td>South</td><td>66,400</td><td>69,100</td>

<td>72,500</td><td>75,300</td></tr>
10. <tr><td>NorthEast</td><td>97,100</td><td>99,400</td>
<td>102,900</td><td>98,200</td></tr>
11. </table>
12.
13. </center>
14. </body>
15. </html>

10 Module 9: Implementing a Nonstandard Web Service

To mimic a Web Service that can provide a list of sales information by region,
you can have the following WSDL document:



1. <?xml version="1.0"?>
2. <definitions standard namespaces omitted for brevity
3. xmlns:urt=!
"
4. <types />
5. <message name="GetSalesHttpGetIn"/>
6. <message name="GetSalesHttpGetOut" />
7. <portType name="SalesHttpGet">
8. <operation name="GetSales">
9. <input message="s0:GetSalesHttpGetIn"/>
10. <output message="s0:GetSalesHttpGetOut"/>
11. </operation>
12. </portType>
13. <binding name="SalesHttpGet" type="s0:SalesHttpGet">

14. <http:binding verb="GET"/>
15. <operation name="GetSales">
16. <http:operation location=""/>
17. <input>
18. <http:urlEncoded/>
19. </input>
20. <output>
21. <urt:text>
22. <urt:match name="SalesData"
pattern="&lt;/th.*?&gt;&lt;/tr.*?&gt;(.*)
&lt;/table.*?&gt;" ignoreCase="1">
23. <urt:match name="Sales" type="RegionSales"
pattern="&lt;tr.*?&gt;(.*?)&lt;/tr.*?&gt;"
repeats="*">
24. <urt:match name="Region"
25. pattern="(?:&lt;td.*?&gt;(.*?)
&lt;/td.*?&gt;.*?){5}"
26. group="1" capture="0"/>
27.
28. </urt:match>
29. </urt:match>
30. </urt:text>
31. </output>
32. </operation>
33. </binding>
34. <service name="Sales">
35. <port name="SalesHttpGet" binding="s0:SalesHttpGet">
36. <http:address location="http://localhost/Scrape
/Sales.html"/>
37. </port>

38. </service>
39. </definitions>

Delivery Tip
Explain the WSDL code
sample using the file
<install
folder>\Democode\Mod09\
sales.wsdl.
Module 9: Implementing a Nonstandard Web Service 11

After processing the preceding service description to generate a proxy, the
following proxy class is produced:



1. public class Sales : HttpGetClientProtocol {
2. public Sales() {
3. this.Url = "http://localhost/Scrape/Sales.html";
4. }
5.
6. [HttpMethod(typeof(TextReturnReader),
typeof(UrlParameterWriter))]
7. public GetSalesMatches GetSales() {
8. return ((GetSalesMatches)(this.Invoke("GetSales",
(this.Url + ""), new object[0])));
9. }
10.
11. }
12.

13. public class GetSalesMatches {
14. [Match("</th.*?></tr.*?>(.*)</table.*?>", IgnoreCase=true)]
15. public SalesData SalesData;
16. }
17.
18. public class SalesData {
19. [MatchAttribute("<tr.*?>(.*?)</tr.*?>")]
20. public RegionSales[] Sales;
21. }
22.
23. public class RegionSales {
24. [Match("(?:<td.*?>(.*?)</td.*?>.*?){5}")]
25. public string Region;
26.
27. }

The preceding proxy class can be used as follows:
1. Sales s = new Sales();
2. GetSalesMatches sm;
3. sm = s.GetSales();
4. RegionSales [] rs = sm.SalesData.Sales;
5. foreach (RegionSales sale in rs)
6. {
7. Console.WriteLine("{0}: Q1 Sales -
{1:C}",sale.Region,sale.Q1);
8. }
Delivery Tip
Explain the C# proxy code
sample using the file
<install

folder>\Democode\Mod09\
sales.cs.
12 Module 9: Implementing a Nonstandard Web Service

The resulting output is:
NorthWest: Q1 Sales - 10,000
Midwest: Q1 Sales - 81,100
South: Q1 Sales - 66,400
NorthEast: Q1 Sales - 97,100

Press any key to continue

Module 9: Implementing a Nonstandard Web Service 13



 Aggregating Web Services
 Aggregated Web Service Scenarios
 Demonstration: Example of an Aggregated Web
Service

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
Developers are constantly looking for ways to reuse code. Web Services present
opportunities for code reuse. The reuse model in Web Services is different than
that of the code libraries, because of the loose coupling between Web Services
and their consumers. As a result, the reuse scenarios for Web Services include
inter-organization workflows and service aggregation, among others. A detailed
discussion of workflow applications is beyond the scope of this course, but it is
useful for us to examine service aggregation as a way of reusing Web Services.

Web Service aggregation is a technique where a Web Service consumes other
Web Services in order to reuse their functionality.
Topic Objective
To introduce the topics in
this section.
Lead-in
Developers are constantly
looking for ways to reuse
code.
14 Module 9: Implementing a Nonstandard Web Service

Aggregated Web Service Scenarios
! Gateways to Web Services
! Simple Interfaces to Complex Web Services
! Portals to Web Services

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
There are many common scenarios where aggregating Web Services can be
useful to consumers.
Gateways to Web Services
Often it might be unnecessary to secure the node that hosts a specific Web
Service.
Consider a scenario where a Web Service is typically used behind a corporate
firewall. Because of the firewall protection, access to the Web Service is not
authenticated. However, if a decision is made at a later point to allow external
clients, such as trading partners to use the Web Service, then they need to be

authenticated before they can use the service.
A simple way to handle the preceding scenario is to implement a Web Service
with exactly the same interface as the unsecured Web Service, and place the
new Web Service outside the firewall. The new Web Service would provide a
secure gateway to the Web Service behind the firewall by authenticating all
requests, and then forwarding them to the unsecured Web Service.
There are many other scenarios where one Web Service can act as a gateway to
other Web Services. For example, a Web Service might forward requests to
various Web Services based on metrics like response time, geographic location,
user role, etc.
Topic Objective
To describe different
scenarios where an
aggregated Web Service
might be an appropriate
solution.
Lead-in
There are many common
scenarios where
aggregating Web Services
can be useful to consumers.
Module 9: Implementing a Nonstandard Web Service 15

Simple Interfaces to Complex Web Services
Some Web Services might expose very rich functionality. Also, the interface to
the Web Service might be very complex. One such example is the Microsoft
TerraServer .NET Web Service, which returns photographic images,
topographic maps, and relief maps, by using the IDs of entities known as tiles.
The tiles are sections of a map and can be located using latitude and longitude
information. Suppose you wanted to retrieve an image of Redmond,

Washington. You would first have to find the latitude and longitude
information, then determine the tile ID, and then retrieve the image. However,
at the time of this writing, the TerraServer Web Service does not provide the
capability of determining the latitude and longitude of a city based on its name.
It would be useful to have a Web Service that had an operation (method) that
accepted the names of a city, state, or province, and country as arguments; and
returned the image of the appropriate tile. This Web Service can retrieve the
latitude and longitude from a Web Service that provides this information, and
then uses this information to find the tile ID and the image using the
TerraServer Web Service.
Portals to Web Services
A Web Service can aggregate other Web Services to provide additional value to
consumers. Instead of a consumer having to interact with multiple Web
Services, you can have an aggregating Web Service that acts as a portal to other
Web Services.
Consider the following scenario. A Web Service named Northwind Traders
Web Service acts a portal, using which you can transfer funds electronically
between financial institutions that provide Web Services to transfer funds. The
advantage of using this portal Web Service is that it handles the details of
obtaining routing information, and initiating the electronic funds transfer. This
portal Web Service communicates with two other Web Services that act as the
source and recipient of the funds being transferred. All a client of the
Northwind Traders Web Service must do is select the source and destination
financial institution, and supply the amount for the transfer and security
credentials for the source financial institution. This is considerably simpler than
having to request routing information from one financial institution Web
Service and then manually deliver that information to another financial
institution Web Service.
You will implement the preceding Northwind Traders Web Service in the lab
for this module.


If your Web Service is designed to work with interactive clients, then it
will probably not perform well in a scenario where the Web Service is
aggregated by other Web Services.

Note
16 Module 9: Implementing a Nonstandard Web Service

Demonstration: Example of an Aggregated Web Service
Internet
Web Service Consumer
UDDI
Registry
Contoso Micropayment
Web Service
Firewall
Firewall
Firewall
Woodgrove Bank
Web Service
Northwind Electronic Funds
Transfer Web Service

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
In this demonstration, you will look at an example of an aggregated Web
Service, which acts as a portal to other Web Services. You will implement this
aggregated Web Service in the lab for this module.
Topic Objective
To demonstrate an

aggregated Web Service.
Lead-in
In this demonstration, you
will look at an example of an
aggregated Web Service,
which acts as a portal to
other Web Services.
Delivery Tip
Refer to the instructor notes
for this demonstration.
Module 9: Implementing a Nonstandard Web Service 17

Lab 9: Implementing an Aggregated Web Service
Internet
Web Service Consumer
UDDI
Registry
Contoso Micropayment
Web Service
Firewall
Firewall
Firewall
Woodgrove Bank
Web Service
Northwind Electronic Funds
Transfer Web Service

*****************************
ILLEGAL FOR NON
-

TRAINER USE
******************************
Objective
After completing this lab, you will be able to implement an aggregating Web
Service that uses multiple Web Services.
Lab Setup
There are starter and solution files are associated with this lab. The starter files
are in the folder <install folder>\Labfiles\Lab09\Starter. The solution files for
this lab are in the folder <install folder>\Labfiles\Lab09\Solution.
If you did not complete Lab 7, Securing Web Services, in Module 7, “Securing
Web Services,” in Course 2524A, Developing XML Web Services Using
Microsoft Visual C# .NET Beta 2, copy the <install
folder>\Labfiles\Lab07\Solution\Contoso project to the C:\inetpub\wwwroot
folder, overwriting your existing Contoso project.
Scenario
In this lab, you will implement an aggregating Web Service named Northwind
Traders and a client to consume this service. The Northwind Traders Web
Service will be a portal to facilitate electronic funds transfer between financial
institutions. It will locate Web Services for financial institutions that can be
either a source or a destination of the funds transfer. It will allow funds transfer
between any of the located financial institutions.
You will also implement a client that uses the Northwind Trader Web Service.
Estimated time to complete this lab: 90 minutes
Topic Objective
To introduce the lab.
Lead-in
In this lab, you will
implement an aggregated
Web Service.
Explain the lab objectives.

18 Module 9: Implementing a Nonstandard Web Service

Exercise 1
Extending Contoso Micropayment Web Service
In this exercise, you will add a CreditAccount method to the Contoso
Micropayment Web Service. This method will invoke the Woodgrove Online
Bank TransferFunds method, which transfers funds from a Woodgrove
account to a Contoso account.
!
To add the CreditAccount method
1. Open the Contoso project in Microsoft Visual Studio
®
.NET.

If you did not complete the modifications to the Contoso project
in Lab 7, refer to the Lab Setup section at the beginning of this lab for
additional instructions.

2. Open micropayment.asmx.cs.
3. To provide information to allow Contoso Web Service to bind to the
Woodgrove Web Service, add the following structure to the Contoso
namespace:
public struct EFTBindingInfo
{
public string token;
public string endPoint;
}

The endPoint string provides the URL that the CreditAccount method will
use to call the Woodgrove TransferFunds method. The token string

provides an encrypted string returned by the Woodgrove
AuthorizeFundsTransfer method. The TransferFunds method uses this
token to verify that the transfer has been authorized, and that the
authorization has not expired.
4. Begin creating the CreditAccount method, by adding the following method
signature to the MicroPaymentService class:
public void CreditAccount(EFTBindingInfo bindingInfo,
decimal amount)

5. In the CreditAccount method, add attributes that do the following:
a. Expose the method as a Web Service method.
b. Use the authInfo data member in the header.
c. Decrypt the request header.

!
To call the Woodgrove TransferFunds method in CreditAccount
1. Add a Web Reference to the Woodgrove Online Banking Web Service.
2. Rename the localhost namespace to Bank.
3. Add code to use the Contoso.Bank namespace to Micropayment.asmx.cs.
4. To invoke the Woodgrove TransferFunds method, within the
CreditAccount method, create a WoodgroveOnlineBank proxy class
object.
Importan
t

Module 9: Implementing a Nonstandard Web Service 19

5. Set the Url data member of the proxy class to the endPoint string provided
by the EFTBindingInfo parameter.
6. Open the Class View.

7. Expand the Contoso.Bank namespace.
8. Expand the EFTRoutingInfo class.
Notice that the TransferFunds method takes an EFTRoutingInfo object.
The EFTRoutingInfo class provides routing information for the Contoso
Micropayment financial institution and the Contoso target account that
Woodgrove bank requires to complete a transfer.
9. Create an EFTRoutingInfo object.
10. Set the EFTRoutingInfo data members as follows.
Data Member Value

ABA_RoutingNo “12345”
AccountName “Contoso Micropayments”
AccountNumber “12345678”
SubAccountName Name of the Contoso account holder
SubAccountNumber The Contoso account number

To obtain the SubAccountName and SubAccountNumber, invoke the
Contoso GetAccount method. Although this method is exposed as a Web
Service method, it can also be invoked locally.
Recall that GetAccount returns an AccountDataSet. The
SubAccountName is provided by the _GetAccount[0].Name data member.
The SubAccountNumber is provided by the _GetAccount[0].AccountID
data member.
11. Invoke the Woodgrove proxy class TransferFunds method. Set the token
parameter to the token given by the EFTBindingInfo parameter.
12. Save the EFTConfirmation object that is returned.

Lastly, you need to add a credit transaction to the TransactionLog table of the
Contoso database. The _CreateTransaction stored procedure is provided to do
this. Note that, unlike the other stored procedures that you have used in Contoso

Web Service, _CreateTransaction does not return a DataSet.
!
To record the Contoso transaction in the database
1. To invoke the _CreateTransaction stored procedure, you need to create a
SqlConnection object and set its ConnectionString property. You also
need to create a SqlCommand object and configure it to invoke the stored
procedure with the expected parameters.

To easily add the code to create and set SqlConnection and
SqlCommand objects, copy the code related to sqlConnection1 and
sqlSelectCommand1 objects from the Contoso GetAccount method. Do
not copy the code related to sqlDataAdapter1 because the
_CreateTransaction method does not need an adapter because it does not
return a DataSet.

2. Set the SqlCommand object CommandText to "_CreateTransaction".
Ti
p

×