Module 13: Remoting and XML Web Services |
47 |
|
|
|
XML Web Service Discovery
Topic Objective
To introduce the Web Service discovery process and the tools that are available for discovery.
Lead-in
Web Service discovery is the process of dynamically locating and interrogating Web Service descriptions, which is a preliminary step for accessing an XML Web service.
!Discovery – The Process of Locating and Interrogating XML Web Service Descriptions
!Discovery Services Are Evolving and Changing Rapidly
!Disco – Microsoft XML Web Services Discovery Tool
#Disco file is an XML document that links to descriptions of XML Web Services
#Disco file is currently created and used by Visual Studio
!Universal Description, Discovery, and Integration Project
#Open framework for describing services, discovering businesses, and integrating business services that use the Internet
#Cross-industry support and platform independence
#For more information, see http://www.uddi.org
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Web Service discovery is the process of dynamically locating and interrogating Web Service descriptions, which is a preliminary step for accessing an XML Web service. The discovery process allows an XML Web service client that may not know the Web Service’s Uniform Resource Identifier to locate an XML Web service, to determine the capabilities of that Web Service, and to properly interact with it.
Web Service discovery is currently evolving, and the information that is presented in this topic is subject to change.
Disco – Microsoft Web Services Discovery Tool
In the current version of the .NET Framework, you can enable programmatic discovery when an XML Web service publishes a file with a .disco extension.
Disco is the Microsoft XML Web services discovery tool. A .disco file is an XML document that contains links to other resources that describe the XML Web service. Currently, when you create an ASP.NET-hosted XML Web service with Visual Studio .NET, .disco files are automatically generated and used.
For more information about Disco, see the Visual Studio .NET and .NET
Framework SDK documentation.
Universal Description, Discovery, and Integration
The Universal Description, Discovery and Integration (UDDI) project is a cross-industry project that is dedicated to creating a platform-independent, open framework for describing Web Services, discovering businesses, and integrating business services that use the Internet. The project is driven by all of the major platform and software providers, marketplace operators, and e-business leaders.
For more information about UDDI, see the UDDI Web site at
http://www.uddi.org/.
48 |
Module 13: Remoting and XML Web Services |
Lab 13.2: Using an XML Web Service
Topic Objective
To introduce the lab.
Lead-in
In this lab, you will access an XML Web service by creating a proxy using the Web Services Description Language tool.
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Objectives
After completing this lab, you will be able to:
•Access an XML Web service by creating a proxy using the Web Services Description Language tool.
Lab Setup
Starter and solution files are associated with this lab. The starter files are in the folder <install folder>\Labs\Lab13.2\Starter. The solution files for this lab are in the folder <install folder>\Labs\Lab13.2\Solution.
Module 13: Remoting and XML Web Services |
49 |
|
|
|
Scenario
This lab is similar to the scenario used in Lab 13.1, Building an OrderProcessing Application by Using Remoted Servers. That scenario was based on a simple distributed order-processing application, in which a customer specifies a customer ID and an item number for an item that the customer wants to purchase, and the application processes the order. The order processing involves authenticating the customer’s ID and arranging order fulfillment that is to say, having the ordered item sent to the customer.
This lab extends Lab 13.1 by having the client application calculate the cost of the ordered item. Your client will use an XML Web service to calculate the item’s cost including sales tax. This scenario simulates the case where you do not have access to the XML Web service’s source code or assembly.
You will build a MathService XML Web service to calculate the total cost of an item given the pretax cost and fractional tax rate. You will then use the Web Services Description Language tool (Wsdl.exe) to access and create a .NET proxy class for this XML Web service. Then you will build a client called Testclient2.exe that uses this proxy to access the XML Web service’s Service1.SalesTax method to calculate the cost of the ordered item.
Estimated time to complete this lab: 55 minutes
50 |
Module 13: Remoting and XML Web Services |
Exercise 1
Creating the XML Web Service Proxy
In this exercise, you will create the MathService XML Web service and use the Web Services Description Language tool (Wsdl.exe) to create a proxy class to this XML Web service.
!Create the MathService XML Web service using Visual Studio .NET
1.Start Visual Studio, and create a new project.
a.In the New Project dialog box, select Visual C# Projects as the type.
b.Select ASP.NET Web Service as the template. You may need to scroll down to see ASP.NET Web Service.
c.In the Location box, type the name of the Web server, http://localhost/MathService. The grayed out Name box will now contain the text MathService. Then click OK.
2.On the View menu, click Code.
Because the XML Web service in this demonstration is simple, you can work on the code directly. For more complex XML Web services, you can use the Service1.asmx.cs Design palette that is displayed when you create a new project of type XML Web service. The Service1.asmx.cs Design palette enables the drag-and-drop operation of rapid application development that can make complex services easier to set up.
3.In the Service1 class, examine the HelloWorld method in the commentedout WEB SERVICE EXAMPLE.
4.In Service1.asmx.cs, implement the SalesTax method by inserting the following code after the comments for the HelloWorld method. You should leave the HelloWorld code commented out.
[WebMethod] public float SalesTax(
float salesAmount, float fractionalTaxRate) { return salesAmount +
(salesAmount * fractionalTaxRate);
}
5.On the Build menu, click Build Solution to create the XML Web service.
6.Use Internet Explorer to access the XML Web service at the following URL:
http://localhost/MathService/Service1.asmx
Tip To use Internet Explorer to view an XML Web service from within the
Visual Studio environment, right-click the .asmx file in the Solution
Explorer window, and then click View in Browser.
Module 13: Remoting and XML Web Services |
51 |
|
|
|
7.In the Internet Explorer page that is returned in step 6, click the link for the Service1 operation labeled SalesTax.
8.In the Internet Explorer page that is returned in step 7, type some parameters, and then click Invoke to verify that the correct value is returned and displayed in SOAP/XML format.
9.Use Internet Explorer to access and verify that the XML Web service’s description is obtained from the following URL:
http://localhost/MathService/Service1.asmx?wsdl
! Create the XML Web service proxy
Important To use Microsoft Visual Studio .NET tools within a command prompt window, the command prompt window must have the proper environment settings. The Visual Studio .NET Command Prompt window provides such an environment. To run a Visual Studio .NET Command Prompt window: on the Start menu, point to All Programs, point to Microsoft Visual Studio .NET, Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.
•In a Visual Studio .NET Command Prompt window, from the Lab 13.2 Starter directory, create a proxy to the authentication XML Web service named Service1.cs by running the following command:
wsdl http://localhost/MathService/service1.asmx?wsdl
52 |
Module 13: Remoting and XML Web Services |
Exercise 2
Creating the Test Client
In this exercise, you will create a test client. The client calls the authentication server. If the user is authenticated, then the client calls the order fulfillment server. If the order is fulfilled, then the client calls the MathService XML Web service to calculate the total cost including sales tax. For simplicity, the client will assume that all items cost $100 and that the sales tax rate is always 0.1.
!Create the test client
1.Use the editor of your choice to examine the C# source file that is named Testclient2.cs located in the Lab13.2 Starter directory.
2.Add code to Testclient2.cs after the following command that outputs the results from calling the fulfillment service:
Console.WriteLine(
"Fulfillment Server Customer 1234 Item Number 5678 Shipped: {0}", shipped.ToString() );
The code should check the status of fulfillment. If shipped is true, then perform the following steps:
a.Instantiate a new Service1 object named salesTaxService.
b.Call the salesTaxService.SalesTax method, passing 100.0F as its first argument and 0.1F as its second argument.
c.Print out to the console the returned total cost value.
3.Build Testclient2.exe in a Visual Studio .NET Command Prompt window as follows:
a.Reference Fulfillment.exe, and Authenticate.exe.
b.Specify the C# source files, Service1.cs and Testclient2.cs.
c.Specify that the output file should be named Testclient2.exe.
Module 13: Remoting and XML Web Services |
53 |
|
|
|
4.Test the order fulfillment system, by performing the following steps:
a.In a console window, from the Lab 13.2 Starter directory, run Authenticate.exe.
b.In a second console window, from the Lab 13.2 Starter directory, run Fulfillment.exe.
c.In a third console window, repeatedly run Testclient2.exe.
The console output will vary because the random-number generator values cause authentication and fulfillment to succeed or fail 50 percent of the time.
A typical series of test client invocations would produce output similar to the following:
>testclient2
Authentication Server Customer 1234 Authorization: True Fulfillment Server Customer 1234 Item Number 5678! Shipped: True
Total Cost: 110
>testclient2
Authentication Server Customer 1234 Authorization: True Fulfillment Server Customer 1234 Item Number 5678! Shipped: False
54 |
Module 13: Remoting and XML Web Services |
Review
Topic Objective
To reinforce module objectives by reviewing key points.
Lead-in
The review questions cover some of the key concepts taught in the module.
!Remoting
!Remoting Configuration Files
!XML Web Services
*****************************ILLEGAL FOR NON-TRAINER USE******************************
1.Can two application domains that are on the same computer each have a channel that listens on the same port number?
Ports are a machine-wide resource; therefore, on one computer, it is illegal to register multiple channels that listen on the same port number, even if the channels are registered in different application domains.
2.What is the purpose of a proxy?
The proxy object acts as a representative of the remote object and ensures that all calls that are made on the proxy are forwarded to the correct remote object instance. All methods that are called on the proxy are automatically forwarded to the remote class, and any results are returned to the client.
3.Can a remotely instantiated object be returned by value?
No. All objects that are instantiated remotely are returned by reference.
4.What determines whether a remotely instantiated object’s parameters and return values are passed by reference or by value?
Objects whose classes are marked with the SerializableAttribute are marshal-by-value, and objects that inherit from System.MarshalByRefObject are marshal-by-reference.
Module 13: Remoting and XML Web Services |
55 |
|
|
|
5.What file extension is typically used by ASP.NET-hosted XML Web services?
ASP.NET provides support for XML Web services with the .asmx file.
6.In ASP.NET, how do you specify that a service is defined in a prebuilt assembly, and where should that assembly’s DLL be located in relation to the ASP.NET application?
The .asmx file should contain the single line:
<%@ WebService Class="<namespace>.<classname>" %>
The assembly library DLL should be in the application’s \Bin subdirectory.
7.How can a client invoke an XML Web service that is not implemented by using the .NET Framework or in which the XML Web service’s assembly or source code is not available?
The Web Services Description Language tool (Wsdl.exe) can be used to read the WSDL description of an XML Web service and create a proxy class. The client can use the proxy class to invoke the methods of the XML Web service.
56 |
Module 13: Remoting and XML Web Services |
Course Evaluation
Topic Objective
To direct students to a Web site to complete a course evaluation.
Lead-in
Between now and the end of the course, you can go to the Web site listed on this page to complete a course evaluation.
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Your evaluation of this course will help Microsoft understand the quality of your learning experience.
To complete a course evaluation, go to http://www.metricsthatmatter.com/ survey/.
Microsoft will keep your evaluation strictly confidential and will use your responses to improve your future learning experience.