
C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#
.pdf
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.

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

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
