Setting Control's parent cross AppDomain.

G

Guest

Hi,

I encountered the following problem when trying to set control parent when
the control reside in a different AppDomain.

Here is the pseudo code descibing the problem:
pay attention that both controls inherits from UserControl which inherites
(indirectly)from MarshalByRefObject.


class Control1:UserControl
{

AppDOmainSetup setup = new AppDomainSetup();
//code that initialize the setup...

AppDomain newAppDomain = AppDomain.CreateDomain("MyDomain",null,setup);

Control c2 =
(Control)newAppDomain.CreateINstanceAndUnwrap(assemblyofcontrol2,Control2);

this.Controls.Add(c2); //<===Exception (see message content below)

}


class Control2:UserControl //this control reside in a different assembly
{
}

The Exception message is:
exception the message is
The type System.Windows.Forms.Form+ControlCollection in Assembly
System.Windows.Forms, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089 is not marked as serializable.

I tried to mark Control2 as Serializable but it didn't help :-(
Any suggestion?

Thanks,
Burgazon
 
M

Michael Nemtsev

Hello Burgazon,

I think that the problem is exactly in the MarshalByRefObject, because CLR
marshal assemblies by value not by ref (because they are copied between the
domaind)
And don't forget to set Serialized attribute for the marshaled object

B> Hi,
B>
B> I encountered the following problem when trying to set control parent
B> when the control reside in a different AppDomain.
B>
B> Here is the pseudo code descibing the problem:
B> pay attention that both controls inherits from UserControl which
B> inherites
B> (indirectly)from MarshalByRefObject.
B> class Control1:UserControl
B> {
B> AppDOmainSetup setup = new AppDomainSetup();
B> //code that initialize the setup...
B> AppDomain newAppDomain =
B> AppDomain.CreateDomain("MyDomain",null,setup);
B>
B> Control c2 =
B> (Control)newAppDomain.CreateINstanceAndUnwrap(assemblyofcontrol2,Cont
B> rol2);
B>
B> this.Controls.Add(c2); //<===Exception (see message content below)
B>
B> }
B>
B> class Control2:UserControl //this control reside in a different
B> assembly
B> {
B> }
B> The Exception message is:
B> exception the message is
B> The type System.Windows.Forms.Form+ControlCollection in Assembly
B> System.Windows.Forms, Version=1.0.5000.0, Culture=neutral,
B> PublicKeyToken=b77a5c561934e089 is not marked as serializable.
B> I tried to mark Control2 as Serializable but it didn't help :-( Any
B> suggestion?
B>
B> Thanks,
B> Burgazon
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Hi Michael and Thank you for the quick response,
Itried to do what you've suggsted but it didn't work still getting the same
message.
It looks that theparents collection is not marked as serialized so it can't
be marsheled.
Do you have any other suggestions?
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top