Copies and References

In common with RMI, remoting provides the distinction between classes that will be referenced remotely and classes that will be copied across the network via serialization. In the example, the CountState class is annotated with the Serializable attribute, indicating that it will be copied and sent to the client as the result of a call to the CountServer.SumNumbers method; all of the .NET primitive types are annotated with the Serializable attribute. We can see the effect of serialization if we modify the CountClient.DoRemoteSummation method:

private void DoRemoteSummation(CountServer p_server) {
    for (int i = 0; i < 2; i++) {
        // clear the state total
        Console.WriteLine("Clearing state.");
        p_server.State.Clear();
        // send some numbers to be added to the total
        CountState x_state = p_server.SumNumbers(1, 2, 3, 4, 5);
        // print out the total
        Console.WriteLine("Sum: {0}", x_state.GetTotal());
    }
}

In this version, we invoke Clear on the CountState instance returned by the CountServer.State method before sending the array of integers to be summed. The output from this follows:

Clearing state.
Sum: 15
Clearing state.
Sum: 30

Although the Clear method is being invoked on the CountState type, the Serializable attribute means that the instance that receives the Clear method call is contained in the local process, copied from the instance of the server, and returned as the result of the SumNumbers method.

By contrast, classes that inherit from System.MarshalByRefObject will be handled as references and the remoting system will ensure that a proxy is used to dispatch method calls to the instance hosted by the server.

A proxy class is one that appears as the remote type to the client but that dispatches member calls across the network to the remoting server; the client isn’t aware that the remoting system is being used to service the type. The CountServer type is presented to the client via a proxy; all of the members of the type are available to the client, but when a method is invoked, the remoting system will be used to transparently perform the operation on the instance hosted by the server. See the Publishing Limitations and Scope section later in this chapter for details of members that will not be invoked remotely.

By removing the Serializable attribute and deriving the CountState class from System.MarshalByRefObject, we change the way that the remoting system handles the CountState type; instead of creating serialized copies of the class and sending them to the client, a proxy will transparently dispatch member calls to the instance maintained on the remote server. The revised declaration for the class is shown here:

public class CountState : MarshalByRefObject {

These changes result in the Clear method behaving as expected and resetting the total on the server, as shown by the following output:

Clearing state.
Sum: 15
Clearing state.
Sum: 15

For more information about attributes, see Chapter 6. For more information about serialization, see Chapter 10.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset