[Mono-list] Creating new appdomain in mono embedding

Robert Jordan robertj at gmx.net
Thu Aug 11 16:59:03 EDT 2011


On 11.08.2011 20:59, Salamat, Babak wrote:
> I have been trying to create a new appdomain in mono embedding, but I have not been successful. In fact, no matter what appdomain I create, on the C# side I get the same appdomain that is initially created by mono_jit_init. Here is what I do:
>
> 1. On the C++ side, I initially create an appdomain using mono_jit_init:
> MonoDomain* abc_domain = mono_jit_init_version("abc", "v4.0");
>
> 2. then I load the assembly using mono_domain_assembly_open and everything works so far.
>
> 3. later on I create a new appdomain using mono_domain_create_appdomain and the domain is created successfully:
> MonoDomain* xyz_domain = mono_domain_create_appdomain("xyz", NULL);
>
> 4. I create new objects in this new domain:
> MonoObject* xyz_object = mono_object_new (xyz_domain, MyClass);
> mono_runtime_invoke(ctorMethod, xyz_object, args, NULL);
>
> However, on the C# side when I print the AppDomain.CurrentDomain.FriendlyName in the constructor of the xzy_object I still get the "abc" which is the domain initially created by mono_jit_init. I need to create new appdomains to have independent static variables in different domains.
>
> I appreciate it if someone help me resolving this issue and let me know what I am doing incorrectly.

You must explicitly set the app domain before invoking any methods
on the object:

MonoDomain *old_domain = mono_domain_get ();
mono_domain_set (xyz_domain);
mono_runtime_invoke(ctorMethod, xyz_object, args, NULL);
mono_domain_set (old_domain);

Robert



More information about the Mono-list mailing list