Remoting with Generics


Quite possibly one of the most powerful new features of remoting in the .NET Framework v2.0 is the support for generics. This chapter has illustrated that client code can create an instance of a remote object and manipulate that instance either as a serialized copy or as a reference to the remote object via the MarshalByRefObject class. What makes remoting even more powerful is that remote objects can make use of generics and still be fully supported on the client.

To illustrate how this works, you can rewrite the first IPC example in this chapter to work with generics. By now you should be pretty familiar with the mechanics of creating a remoting solution using the "Shared Library" pattern. The following code provides a generic interface for the shared library:

using System; using System.Collections.Generic; using System.Text; namespace SharedLibrary {     public interface ISharedObject<T>     {         void PrintMessage(T msg);     } } 


The data type of the message being sent to the remote object is now controlled by the generics type argument T. The following code shows the implementation of the shared interface in the server project:

using System; using System.Collections.Generic; using System.Text; using SharedLibrary; namespace IpcGenerics { public class SharedObjectImplementation<T>: MarshalByRefObject, ISharedObject<T> { public SharedObjectImplementation() {     Console.WriteLine("Shared Object instantiated: " + this.GetType().ToString()); } #region ISharedObject<T> Members public void PrintMessage(T msg) {     Console.WriteLine("Incoming Message: " + msg.ToString()); } #endregion } } 


Listings 41.6 and 41.7 show the Program.cs code for the server and client respectively.

Listing 41.6. Program.cs for Generics IPC Sample Server

using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Ipc; using System.Collections.Generic; using System.Text; namespace IpcGenerics { class Program { static void Main(string[] args) {     // init channel     IpcChannel ipc = new IpcChannel("Generics-Remoting");     ChannelServices.RegisterChannel(ipc);     RemotingConfiguration.RegisterWellKnownServiceType(         typeof(SharedObjectImplementation<string>),         "StringObject.rem",         WellKnownObjectMode.SingleCall);     RemotingConfiguration.RegisterWellKnownServiceType(         typeof(SharedObjectImplementation<DateTime>),         "DateObject.rem",         WellKnownObjectMode.SingleCall);     Console.WriteLine("Multiple objects hosted on channel. Press enter to stop.");     Console.ReadLine(); } } } 

Listing 41.7. Program.cs for IPC Generics Client Sample

using System; using System.Runtime.Remoting; using System.Collections.Generic; using System.Text; using SharedLibrary; namespace IpcClient { class Program { static void Main(string[] args) {     ISharedObject<string> stringObject =         (ISharedObject<string>)         Activator.GetObject(typeof(ISharedObject<string>),         "ipc://Generics-Remoting/StringObject.rem");     ISharedObject<DateTime> dateObject =         (ISharedObject<DateTime>)         Activator.GetObject(typeof(ISharedObject<DateTime>),         "ipc://Generics-Remoting/DateObject.rem");     stringObject.PrintMessage("Hello from IPC client");     dateObject.PrintMessage(DateTime.Now);     Console.WriteLine("Remote messages sent.");     Console.ReadLine(); } } } 

Each concrete type (for example, ISharedObject<string> or ISharedObject<DateTime>) gets its own URI and the client can then create instances of each of the remotely hosted concrete types. When the application is put together and both sides are executed, the following is the output from the server console:

Multiple objects hosted on channel. Press enter to stop. Shared Object instantiated: IpcGenerics.SharedObjectImplementation '1[System.String] Incoming Message: Hello from IPC client Shared Object instantiated: IpcGenerics.SharedObjectImplementation '1[System.DateTime] Incoming Message: 1/29/2006 9:45:54 AM 


In the .NET Framework 2.0, when you print the name of a generic type implementation, a '1 appears and then the name of the type argument is included in square brackets. So in the preceding output, you can see that Generics.SharedObjectImplementation is the generic class and System.String and System.DateTime are both type arguments.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net