Hi Expert,
We are trying to create an object in secondary appdomain from default
appdomain using CreateInstanceAndUnwrap api, But this call seems to be
taking lot of time to return, I know the call involve loading of required
assembly of the class/type and create object of type and return the proxy
object to the default domain. But I look for any other alternative way of
doing this???
// Sample c# code
class Test
{
static void Main()
{
CreateInstanceInSecondaryAppdomain(); // OK!
}
static void CreateInstanceInSecondaryAppdomain()
{
try
{
// Create a new AppDomain.
AppDomain ad =
AppDomain.CreateDomain("SecondaryAppDomain");
MarshalByRefType instance = (MarshalByRefType)
ad.CreateInstanceAndUnwrap(
Assembly.GetCallingAssembly().FullName,
"CreateInstanceAndUnWrap.MarshalByRefType",
true,
BindingFlags.Default,
null,
null,
null,
null,
null
);
// I'm done using the other AppDomain, so
// I'll unload it and all its assemblies.
AppDomain.Unload(ad);
}
catch (TypeLoadException e)
{
Console.WriteLine(e.Message);
}
}
}
class MarshalByRefType : MarshalByRefObject
{
//System.Runtime.Remoting.Activation.IActivator.
// This instance method can be called via a proxy.
public MarshalByRefType()
{
}
public void SomeMethod(String sourceAppDomain)
{
// Display the name of the calling AppDomain and my AppDomain.
// NOTE: The application's thread has transitioned between
AppDomains.
Console.WriteLine(
"Code from the '{0}' AppDomain\n" +
"called into the '{1}' AppDomain.",
sourceAppDomain,
Thread.GetDomain().FriendlyName);
}
}
Thanks,
GP.
Mujtaba Syed - 29 Dec 2004 16:37 GMT
Hi:
> We are trying to create an object in secondary appdomain from default
> appdomain using CreateInstanceAndUnwrap api, But this call seems to be
> taking lot of time to return, I know the call involve loading of required
How much time?
You are using late binding and that too across AppDomains. It's definitely
not going to be as fast as a new ().
Mujtaba.
Gnanaprakash Rathinam - 30 Dec 2004 04:04 GMT
It is somewhere around 15-20 milliseconds on uniprocessor pentium 2.6GHz
with 1GB RAM
> Hi:
>
[quoted text clipped - 7 lines]
>
> Mujtaba.
David Levine - 30 Dec 2004 11:35 GMT
Does it take that long each time you call CreateInstanceAndUnwrap or only
the 1st time you call it? It is a fairly heavy operation to create and
destroy an appdomain. When you destroy the appdomain all objects that are
rooted in it are garbage collected, and all finalizers of those objects run
to completion before the destroy call completes - this is time consuming.
Try running another timing test but don't include the appdomain
create/destroy in the test.
> It is somewhere around 15-20 milliseconds on uniprocessor pentium 2.6GHz
> with 1GB RAM
[quoted text clipped - 12 lines]
>>
>> Mujtaba.
Gnanaprakash Rathinam - 30 Dec 2004 12:48 GMT
The timing include only the call to CreateInstanceAndUnwrap, but the second
call to CreateInstanceAndUnwrap takes very less time, close to 1
millisecond, so for first call this long time (~15ms) is required to load an
assembly into appdomain?
> Does it take that long each time you call CreateInstanceAndUnwrap or only
> the 1st time you call it? It is a fairly heavy operation to create and
[quoted text clipped - 21 lines]
> >>
> >> Mujtaba.
David Levine - 30 Dec 2004 21:18 GMT
> The timing include only the call to CreateInstanceAndUnwrap, but the
> second
> call to CreateInstanceAndUnwrap takes very less time, close to 1
> millisecond, so for first call this long time (~15ms) is required to load
> an
> assembly into appdomain?
That's probably the case. Loading an assembly is considered to be a "heavy"
operation, since it is a container for types, acts as a security boundary,
etc. There's a lot of overhead in loading the assembly, but once it is
loaded the results are cached.