Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#

.pdf
Скачиваний:
194
Добавлен:
12.02.2016
Размер:
16.87 Mб
Скачать

Module 13: Remoting and XML Web Services

27

 

 

 

Building and Executing the Server and Client

To build the server and the client in the same directory, type the following command at the Microsoft Visual Studio .NET Command Prompt command prompt:

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.

csc server.cs

csc /r:server.exe client.cs

To execute the application, start the server application from a console window, and then start the client application from another console window.

Note The counter values on the two client calls should have the same value because the server object’s activation mode is SingleCall.

Changing the Activation Mode

To change the server’s activation mode from SingleCall to Singleton, change the RegisterWellKnownServiceType call in the server as follows:

RemotingConfiguration.RegisterWellKnownServiceType(

typeof(HelloServer),

"SayHello",

WellKnownObjectMode.Singleton);

Rebuild and execute as in the preceding example. Note the counter values on both calls. They should increase after each call.

28

Module 13: Remoting and XML Web Services

Lab 13.1: Building an Order-Processing Application by Using Remoted Servers

Topic Objective

To introduce the lab.

Lead-in

In this lab, you will create an XML Web service hosted in a .NET executable file, create a TCP server, and create a client that uses

.NET Remoting to access an XML Web service and a TCP server.

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

Objectives

After completing this lab, you will be able to:

!Create an XML Web service hosted in a .NET executable file.

!Create a TCP server.

!Create a client that uses .NET Remoting to access both an XML Web service and a TCP server.

Lab Setup

Only solution files are associated with this lab. The solution files for this lab are in the folder <install folder>\Labs\Lab13.1\Solution.

Module 13: Remoting and XML Web Services

29

 

 

 

Scenario

This lab is based on a scenario of 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.

In this lab, you will create a distributed solution that uses .NET Remoting. You will use .NET Remoting support of SOAP over HTTP to create a server application that exports an authenticate method as an open standards-based XML Web service. You will also use .NET Remoting support of TCP with binary formatting to create a remote order fulfillment server application that trades off the advantages of the open standards-based flexible XML Web services protocol for improved performance.

In addition, you will create a simple test client to exercise these servers. Because the focus of this lab is on .NET Remoting, the functionality that is specific to authentication and fulfillment will be minimal.

Estimated time to complete this lab: 50 minutes

30

Module 13: Remoting and XML Web Services

Exercise 1

Creating an Authentication XML Web Service

In this exercise, you will create an XML Web service that provides simple authentication. Simple authentication will consist of using a random-number generator to authenticate the user successfully 50 percent of the time.

!Create the authentication server

1.Using the editor of your choice, create a C# source file named Authenticate.cs, and save it in the Lab13.1 directory.

2.To simplify coding, add using statements to import the namespaces System,

System.Runtime.Remoting, System.Runtime.Remoting.Channels, and System.Runtime.Remoting.Channels.Http.

3.Specify the namespace Lab13, and in it, create a public class named AServer that is marshal-by-reference.

4.Add a field named aRandom to hold a reference to a random number generator object of class System.Random.

5.In the Main method of AServer:

a.Create and register a channel that supports the protocol and format for an XML Web service. Specify port 8086.

b.Invoke the RemotingConfiguration.RegisterWellKnownServiceType method to register the authentication server whose full type name is

Lab13.AServer, and whose endpoint is DoAuthentication.

To retain the object’s state, that is to say, the random-number generator object, over multiple client calls, the well-known object’s mode should be stateful.

c.Print to the console a message that tells the user to press any key so the server exits.

The program should wait for this user action before continuing.

6.Add a public default constructor that creates a new instance of the Random class, assigns it to the field that was created in step 4, and prints a message to the console that states that AServer is running.

Module 13: Remoting and XML Web Services

31

 

 

 

7.Add a public method named Authenticate that takes a customer ID of type int and returns a bool value.

a.Assign to a variable named aRandomNumber of type double a random number that is greater than or equal to 0 and is less than 1, by using the following code:

double aRandomNumber = aRandom.NextDouble();

b.Using the value from step a, assign to a variable of type bool named passed the value true for 50 percent of the time and the value false for 50 percent of the time.

c.Print to the console a message that identifies the customer and states whether the customer is authenticated on the basis of the value of passed.

d.Return passed.

8.In a Visual Studio .NET Command Prompt window, build Authenticate.exe.

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.

32

Module 13: Remoting and XML Web Services

Exercise 2

Creating a Fulfillment Server

In this exercise, you will create a server that provides simple order fulfillment functionality by using TCP and a binary format to provide fast communication. Simple fulfillment will consist of using a random-number generator to successfully fulfill the order 50 percent of the time.

!Create the fulfillment server

1.Using the editor of your choice, create a C# source file that is named Fulfillment.cs, and save it in the Lab13.1 directory.

2.To simplify coding, add using statements to import the namespaces System,

System.Runtime.Remoting, System.Runtime.Remoting.Channels, and System.Runtime.Remoting.Channels.Tcp.

3.Specify the namespace Lab13, and in it, create a public class named FServer that is marshal-by-reference.

4.Add a field named aRandom to hold a reference to a random-number generator object of class System.Random.

5.In the Main method of FServer:

a.Create and register a channel that uses the TCP protocol and binary formatting. Specify port 8085.

Note You cannot have two channels on one computer that are using the same port number; therefore you cannot use port 8086.

b.Invoke the RemotingConfiguration.RegisterWellKnownServiceType method to register the fulfillment server whose full type name is

Lab13.FServer, and whose endpoint is DoFulfillment.

To retain the object’s state, that is to say, the random-number generator object, over multiple client calls, the well-known object’s mode should be stateful.

c.Print to the console a message that tells the user to press any key so the server exits.

The program should wait for this user action before continuing.

6.Add a public default constructor that creates a new instance of the Random class, assigns it to the field that was created in step 4, and prints a message to the console that states that FServer is running.

Module 13: Remoting and XML Web Services

33

 

 

 

7.Add a public method named Fulfill that takes a customer ID of type int, an item number of type int, and returns a bool value.

a.Assign to a variable named aRandomNumber of type double a random number that is greater than or equal to 0 and less than 1, by using the following code:

double aRandomNumber = aRandom.NextDouble();

b.Using the value from step a, assign to a variable of type bool named shipped the value true for 50 percent of the time and the value false for 50 percent of the time.

c.Print to the console a message that identifies the customer and the item number and states whether the item was shipped on the basis of the value of shipped.

d.Return shipped.

8.In a Visual Studio .NET Command Prompt window, build Fulfillment.exe.

34

Module 13: Remoting and XML Web Services

Exercise 3

Creating a Test Client

In this exercise, you will create a test client application to test the orderprocessing servers. The client calls the authentication XML Web service. If the user is authenticated, the client calls the order fulfillment server.

!Create the test client

1.Using the editor of your choice , create a C# source file named Testclient.cs, and save it in the Lab13.1 directory.

2.To simplify coding, add using statements to import the namespaces System,

System.Runtime.Remoting, System.Runtime.Remoting.Channels, System.Runtime.Remoting.Channels.Http, System.Runtime.Remoting.Channels.Tcp, and System.IO.

3.Specify the namespace Lab13, and in it, create a class named TestClient. In subsequent steps of this exercise, you will code the Main method.

4.In the Main method of TestClient:

Create and register an HTTP channel and a TCP channel.

5.Invoke the Activator.GetObject method to create an AServer instance of type Lab13.AServer named aServer at the following URL:

http://localhost:8086/DoAuthentication

6.Invoke the Activator.GetObject method to create an FServer instance of type Lab13.FServer named fServer at the URL:

tcp://localhost:8085/DoFulfillment

7.In the try section of a try/catch block:

a.Invoke the Authenticate method of the aServer object, and use 1234 as the customer’s ID. Print a message with the result to the console.

b.If authentication passes, call the fServer object’s Fulfill method. Use 1234 as the customer’s ID and 5678 as the item number. Print a message with the result to the console.

8.In the catch section of the try/catch block:

a.Catch any exceptions of type IOException, and print out their content.

b.Return a value of 1.

9.In a Visual Studio .NET Command Prompt window, build Testclient.exe.

Important Do not forget to reference Authenticate.exe, and Fulfillment.exe in the command line.

Module 13: Remoting and XML Web Services

35

 

 

 

10.Test the order system.

a.Ensure that no server application is running and listening on ports 8085 and 8086.

Tip You can display your machine’s current TCP/IP network connections by running the program netstat in a command prompt window. The following example shows that port 7 is in use by a process with PID 1504:

C:\>netstat -o -n -a

Active Connections

Proto

Local Address

Foreign Address

State

PID

TCP

0.0.0.0:7

0.0.0.0:0

LISTENING

1504 …

b.Open two separate console windows. In one window, run Fulfillment.exe, and in the other window, run Authenticate.exe.

c.Open a third console window and repeatedly run Testclient.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:

>testclient

Authentication Server Customer 1234 Authorization: False

>testclient

Authentication Server Customer 1234 Authorization: False

>testclient

Authentication Server Customer 1234 Authorization: False

>testclient

Authentication Server Customer 1234 Authorization: True Fulfillment Server Customer 1234 Item Number 5678 Shipped: False

>testclient

Authentication Server Customer 1234 Authorization: True Fulfillment Server Customer 1234 Item Number 5678 Shipped: False

>testclient

Authentication Server Customer 1234 Authorization: True Fulfillment Server Customer 1234 Item Number 5678 Shipped: True

36

Module 13: Remoting and XML Web Services

" XML Web Services

Topic Objective

To provide an overview of the topics covered in this section.

Lead-in

In this section, you will learn how to use Visual Studio

.NET to implement an ASP.NET XML Web service and how to access the XML Web service from a Web browser and a client application.

!ASP.NET XML Web Services Overview

!ASP.NET Features

!Consuming an XML Web Service

!XML Web Service Discovery

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

XML Web services can be provided by and accessed from applications that reside in a variety of hosts. Hosts include, but are not limited to, ASP.NET, Microsoft Internet Explorer, executable files, Microsoft Windows® NT® Server, and Microsoft Windows 2000 Component Services, also known as COM+ Services.

In the preceding sections, you have learned how to use .NET Remoting to create executable files that host XML Web services by using the default HTTP channel with SOAP formatting.

In this section, you will learn how to use Visual Studio .NET to implement an ASP.NET XML Web service and how to access this XML Web service from both a Web browser and a client application.

Соседние файлы в папке c#