12.8 Register All the Remotable Classes in an Assembly


Problem

You want to register all the remotable classes that are defined in an assembly without having to specify them in a configuration file.

Solution

Load the assembly with the remotable classes using reflection. Loop through all its types, and use the RemotingConfiguration.RegisterWellKnownServiceType method to register every remotable class.

Discussion

.NET makes it equally easy to register remotable classes through a configuration file or programmatically with code. Consider the example from recipe 12.7. To use programmatic registration, you would first remove the class declarations from the server configuration file and leave it as shown here:

 <configuration>   <system.runtime.remoting>     <application>              <channels>         <channel ref="tcp" port="9080" />       </channels>              </application>   </system.runtime.remoting> </configuration> 

Now you can combine reflection with the RegisterWellKnownServiceType method to programmatically register all remotable types. However, first you'll need to add a reference to the System.Runtime.Remoting.dll assembly, which contains additional Remoting types.

The following server searches for remotable classes in the RemoteObject.dll assembly, registers each one, and then displays the channel where it's available.

 using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Reflection; public class Server {     private static void Main() {         // Use the configuration file to define networking options.         RemotingConfiguration.Configure("Server.exe.config");         // Get the registered Remoting channel.         TcpChannel channel =           (TcpChannel)ChannelServices.RegisteredChannels[0];         // Create an Assembly object representing the assembly         // where remotable classes are defined.         Assembly assembly = Assembly.LoadFrom("RemoteObject.dll");         // Process all the types in this assembly.         foreach (Type type in assembly.GetTypes()) {             // Check if the type is remotable.             if (type.IsSubclassOf(typeof(MarshalByRefObject))) {                 // Register each type using the type name as the URI                 // (like ProductsDB).                 Console.WriteLine("Registering " + type.Name);                 RemotingConfiguration.RegisterWellKnownServiceType(                    type, type.Name, WellKnownObjectMode.SingleCall);                 // Determine the URL where this type is published.                 string[] urls = channel.GetUrlsForUri(type.Name);                 Console.WriteLine(urls[0]);             }         }         Console.WriteLine("Press a key to shut down the server.");         Console.ReadLine();     } } 
Note  

The preceding code determines whether or not a class is remotable by examining whether it derives from MarshalByRefObject . This approach always works, but it could lead you to expose some types that you don't want to make remotable. For example, the System.Windows.Forms.Form object derives indirectly from MarshalByRefObject . That means that if your remote object library contains any forms, they will be exposed remotely.

To avoid this problem, don't include remotable types in your assembly unless you want to make them publicly available. Or, identify the types you want to register with a custom attribute. You could then check for this attribute before registering a type.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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