Topaz Filer - Email filing software
Execute code from same Assembly in new AppDomain?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.clr.


Christian Benien
Hi,

I was wondering if it is possible to execute code from my (currently
running) Assembly in another AppDomain. In this new AppDomain I want
to do some reflection on other, unrelated assemblies. Then I want to
unload the AppDomain and unlock those assemblies.

I tried with the following code:

[Serializable] class RemoteClass
{
public void Action()
{
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
AppDomainSetup setupInfo = new AppDomainSetup();
setupInfo.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
AppDomain newDomain = AppDomain.CreateDomain("temporaryAppDomain",
AppDomain.CurrentDomain.Evidence, setupInfo);

RemoteClass remote = newDomain.CreateInstanceAndUnwrap(
Assembly.GetExecutingAssembly().FullName,
"AppDomainTest.RemotingClass") as RemoteClass;

remote.Action();
AppDomain.Unload(newDomain);
}
}

The Action() function executes on the main AppDomain. Is it possible
to get this to work? Or will the Action() function always be executed
on the main AppDomain?

Cheers,
Christian
Reply to this message...
Vote that this is a GOOD answer...
 
New BootFX DBGet build available
Twitter and Snow… simple #uksnow
 
    
Dave
The class object you want to remote must be derived from MarshalByRefObject.
By making it serializable you are creating a pass by value object and you
need one that is passed by reference. So change your definition to

class RemoteClass : MarshalByRefObject

and give it a try.

"Christian Benien" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Windows 7 compatible tool for mounting ISO images
Introduction to BootFX’s Object Relational Mapping Functionality article now live on CodeProject
 
    
Christian Benien
GOOD ANSWER
Thank you! Now it works.

"Dave" <Click here to reveal e-mail address> wrote in message news:<#XK1fj#Click here to reveal e-mail address>...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
 
System.AppDomain
System.AppDomainSetup
System.Console
System.MarshalByRefObject
System.Reflection.Assembly




Topaz Filer - Email filing software
Ad
BootFX
Reliable and powerful .NET application framework.
Looking to invest in a major software project? Technical and commercial advice available here.
Other Helpful Sites
MBR 247
Topaz Filer
SharePoint Email Filing
Software Advisory Services
 
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734