delegate and AppDomains
-
Hi! I have an application with two AppDomains (say A and B) where an object X that lives in AppDomain A is passed to AppDomain B where a method subscribes for an event of object X. This means that when object X fires its event, the delegate is called over AppDomain boundaries. This works well as long as the method the event-delegate points to (in AppDomain B) is public. If the method is anything but public (e.g. internal, private or protected) I get an SerializationException saying that it the serialization does not serialize delegates pointing to non-public members. I find this strange and annoying as I don't want to make the method in question public. Any suggestions? Thanks in advance Regards, Andre Loker
-
Hi! I have an application with two AppDomains (say A and B) where an object X that lives in AppDomain A is passed to AppDomain B where a method subscribes for an event of object X. This means that when object X fires its event, the delegate is called over AppDomain boundaries. This works well as long as the method the event-delegate points to (in AppDomain B) is public. If the method is anything but public (e.g. internal, private or protected) I get an SerializationException saying that it the serialization does not serialize delegates pointing to non-public members. I find this strange and annoying as I don't want to make the method in question public. Any suggestions? Thanks in advance Regards, Andre Loker
While it may be annoying, it certainly isn't strange. When crossing application boundaries (including distinct AppDomains), .NET Remoting marshals the call to another context, similar in concept (regarding this discussion) to your code calling another class's code. Depending on derivation and location, the method must be public in order to call it. If you absolutely don't want to make it public, there is a way although it is difficult. You can extend
RealProxy
and theProxyAttribute
(with which you attribute your class that defines the handler). TheRealProxy
derivative can catch method calls (as well as other types), which you can use to override the default behavior so that the delegate is serialized. It's tricky, and if you're not too familiar with .NET Remoting I suggest you pick up a book like "Microsoft .NET Remoting" from MS Press[^] or "Advanced .NET Remoting" from Ingo Rammer[^].Microsoft MVP, Visual C# My Articles