Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Exception when DefineDynamicAssembly() is called in Another AppDomain

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
uberfan@gmail.com - 04 Aug 2005 18:54 GMT
I keep getting the same exception ("The type
System.Reflection.Emit.AssemblyBuilder in Assembly mscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable.") when I try create a dynamicassembly from
a new appdomain. I works when I use the current domain but since I need
to unload these assemblies after I use them, I need them to be in their
own appdomain.
Could someone please help me out with this.. It's been driving nuts for
a few days now. Is it a bug in the 1.1?
thanks

private AssemblyBuilder MakeAssemblyBuilder()
{
 // Build a dynamic assembly using Reflection Emit API.

 AppDomain dynamicAppDomain =null;
 AssemblyBuilder dynamicAsm      = null;
 AssemblyName assemblyName = new AssemblyName();

 assemblyName.Name = "MyDynamicAssembly";
 dynamicAppDomain =
AppDomain.CreateDomain(System.Guid.NewGuid().ToString());
 dynamicAsm = m_dynamicAppDomain.DefineDynamicAssembly(assemblyName,
AssemblyBuilderAccess.Run);
}
David Levine - 04 Aug 2005 22:49 GMT
I assume that you mean to write that dynamicAppDomain and m_dynamicAppDomain
are the same field....

It looks like even though you are attempting to create it in the context of
another appdomain, the method DefineDynamicAssembly is returning a reference
to the dynamic assembly back to the default appdomain, and since this
reference is derived from the Assembly class, that causes the assembly to
loaded into both appdomains.  It is likely that that the exception occurs
when the reference to the dynamic assembly is attempted to be serialized
across the appdomain boundary back to the default appdomain.

You can create a MBRO object and create an instance of it (a remoted object)
in the remote appdomain, and then instantiate your dynamic assembly in a
method in that object. That will maintain the isolation between the
appdomains.

>I keep getting the same exception ("The type
> System.Reflection.Emit.AssemblyBuilder in Assembly mscorlib,
[quoted text clipped - 21 lines]
> AssemblyBuilderAccess.Run);
> }
uberfan - 09 Aug 2005 21:05 GMT
David, thanks for the sugestion. I tried your suggestion and it worked
out. Thing is I'm back to the original problem.  I can't unload a
dynamically loaded dll. After the assembly is created I define a
dynamic module within it. Then a Platform Invocation method. I execute
the method and Unload the new appdomain. Which I would expect to unload
the assembly and all references to the dll which was loaded by
DefinePInvokeMethod. Unfortunatly the file is still locked and cannot
be deleted.

Here's some sample code:

// From Assembly loaded in newly created appdomain
dynamicMod = dynamicAsm.DefineDynamicModule( "tempModule");

// Dynamically construct a global PInvoke signature
// using the input information
MethodBuilder dynamicMethod = dynamicMod.DefinePInvokeMethod("Foo",
  "Cpp.DLL",
  MethodAttributes.Static | MethodAttributes.Public |
MethodAttributes.PinvokeImpl,
  CallingConventions.Standard,
  returnType,
  parameterTypes,
  CallingConvention.Winapi,
  CharSet.Ansi);

// This global method is now complete
dynamicMod.CreateGlobalFunctions();

// Get a MethodInfo for the PInvoke method
MethodInfo mi = dynamicMod.GetMethod(entryPoint);

// Invoke the static method and return whatever it returns
Object pRetObj = mi.Invoke(null, parameterValues);

Any idea as why the module doesn't unload. I guess I create another
thread for this new problem..
David Levine - 10 Aug 2005 00:19 GMT
Are you executiong all these  within the context of the remote appdomain? In
other words, did you create a remoted object that implemented a method that
actually does the work?

> David, thanks for the sugestion. I tried your suggestion and it worked
> out. Thing is I'm back to the original problem.  I can't unload a
[quoted text clipped - 33 lines]
> Any idea as why the module doesn't unload. I guess I create another
> thread for this new problem..
uberfan - 10 Aug 2005 16:39 GMT
Yes..   Here is the code:

 public class CLocalLoader : MarshalByRefObject
 {
   private AppDomain     m_dynamicAppDomain  = null;
   private CRemoteLoader m_RemoteLoad       = null;
   public  String        strErrorMsg         = "";

   public CLocalLoader()
   {
     LoadUp();
   }

   public void Unload()
   {
     if (m_dynamicAppDomain != null)
     {
       AppDomain.Unload(m_dynamicAppDomain);
       m_dynamicAppDomain = null;
     }
     if (m_RemoteLoad != null)
       m_RemoteLoad = null;
   }

   public void LoadUp()
   {
     try
     {
       // Set up the AppDomainSetup
       AppDomainSetup setup = new AppDomainSetup();
       setup.ApplicationName = "MyTestApp";
       setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
       setup.PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory;

       m_dynamicAppDomain = AppDomain.CreateDomain("MySecondDomain",
null, setup);

       m_RemoteLoad = (CRemoteLoader)
m_dynamicAppDomain.CreateInstanceAndUnwrap("InvokeVadminHook",
"InvokeTest.CRemoteLoader");

     }

     catch (Exception pError)
     {
       strErrorMsg = pError.Message;
     }
   }

   public object DynamicPInvoke(string dll, string entryPoint, Type
returnType, Type [] parameterTypes, object [] parameterValues)
   {
     if (m_RemoteLoad != null )
       m_RemoteLoad.CallDynaMethod("tempAssembly", dll, entryPoint,
returnType, parameterTypes, parameterValues);

     return null;
   }
 }

//=============================================================================
 /// <summary>This class is used to  </summary>
 public class CRemoteLoader : MarshalByRefObject
 {
   String          strErrorMsg  = "";

   public CRemoteLoader()
   {
   }

   public void CallDynaMethod(String strAssemblyName, string dll,
string entryPoint, Type returnType, Type [] parameterTypes, object []
parameterValues)
   {

     try
     {
       AssemblyName asmName  = new AssemblyName();
       asmName.Name          = strAssemblyName;

       AssemblyBuilder dynamicAsm =
AppDomain.CurrentDomain.DefineDynamicAssembly(asmName,
AssemblyBuilderAccess.Run);

       //Create a temp module in the assembly
       ModuleBuilder dynamicMod = dynamicAsm.DefineDynamicModule(
"tempModule");

       // Dynamically construct a global PInvoke signature using the
input information
       MethodBuilder dynamicMethod =
dynamicMod.DefinePInvokeMethod(entryPoint,
         dll,
         MethodAttributes.Static | MethodAttributes.Public |
MethodAttributes.PinvokeImpl,
         CallingConventions.Standard,
         returnType,
         parameterTypes,
         CallingConvention.Winapi,
         CharSet.Ansi);

       // This global method is now complete
       dynamicMod.CreateGlobalFunctions();

       // Get a MethodInfo for the PInvoke method
       MethodInfo mi = dynamicMod.GetMethod(entryPoint);

       // Invoke the static method and return whatever it returns
       Object pRetObj = mi.Invoke(null, parameterValues);
     }

     catch (Exception pError)
     {
       strErrorMsg = pError.Message;
       throw pError;
     }
   }
 }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.