ObjectHandle and SerializationException
-
The following code throws a SerializableException where it unwraps the ObjectHandle, but why? I can't find it in any of the MSDN docs and it doesn't even go away when I mark the Task as Serializable? Here is the code: --- public ArrayList GetAssemblyNamesFromHandle( AppDomain domain, string[] assemblyNames ) { ArrayList names = new ArrayList(); foreach( string assembly in assemblyNames ) { System.IO.FileInfo file = new System.IO.FileInfo( assembly ); if( file.Exists ) { string name = assembly.Replace( file.Extension, String.Empty ); System.Runtime.Remoting.ObjectHandle oh = domain.CreateInstanceFrom( assembly, String.Format( "{0}.Task1", name ) ); // Exception thrown here. Task task = (Task) oh.Unwrap(); if( task.Succeeded ) names.Add( assembly ); } } return names; } --- Here is the Exception: --- An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional information: The type TaskLib.Task1 in Assembly TaskLib, Version=1.0.1326.35718, Culture=neutral, PublicKeyToken=null is not marked as serializable. ---
-
The following code throws a SerializableException where it unwraps the ObjectHandle, but why? I can't find it in any of the MSDN docs and it doesn't even go away when I mark the Task as Serializable? Here is the code: --- public ArrayList GetAssemblyNamesFromHandle( AppDomain domain, string[] assemblyNames ) { ArrayList names = new ArrayList(); foreach( string assembly in assemblyNames ) { System.IO.FileInfo file = new System.IO.FileInfo( assembly ); if( file.Exists ) { string name = assembly.Replace( file.Extension, String.Empty ); System.Runtime.Remoting.ObjectHandle oh = domain.CreateInstanceFrom( assembly, String.Format( "{0}.Task1", name ) ); // Exception thrown here. Task task = (Task) oh.Unwrap(); if( task.Succeeded ) names.Add( assembly ); } } return names; } --- Here is the Exception: --- An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional information: The type TaskLib.Task1 in Assembly TaskLib, Version=1.0.1326.35718, Culture=neutral, PublicKeyToken=null is not marked as serializable. ---
By digging deeper into the sample code at ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncscol/html/csharp05162002.htm, I found that the type they load dynamically descends from System.MarshalByRefObject. This makes sense to me, so I just made a loader that is a MarshalByRefObject which encapsulates all the dynamic loading of Tasks within itself. Now, I instantiate the Loader in the other AppDomain and have it do its diagnostics and return a list of assembly names that contain tasks to instantiate from the current domain.