Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is MarshalByRefObject special?

Tags:

.net

clr

remoting

.NET has a thing called remoting where you can pass objects around between separate appdomains or even physical machines. I don't fully understand how the magic is done, hence this question.

In remoting there are two base ways of passing objects around - either they can be serialized (converted to a bunch of bytes and the rebuilt at the other end) or they can inherit from MarshalByRefObject, in which case .NET makes some transparent proxies and all method calls are forwarded back to the original instance.

This is pretty cool and works like magic. And I don't like magic in programming. Looking at the MarshalByRefObject with the Reflector I don't see anything that would set it apart from any other typical object. Not even a weird internal attribute or anything. So how is the whole transparent proxy thing organized? Can I make such a mechanism myself? Can I make an alternate MyMarshalByRefObject which would not inherit from MarshalByRefObject but would still act the same? Or is MarshalByRefObject receiving some special treatment by the .NET engine itself and the whole remoting feat is non-duplicatable by mere mortals?

like image 783
Vilx- Avatar asked Apr 27 '10 11:04

Vilx-


People also ask

What is MarshalByRefObject?

MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. Objects that do not inherit from MarshalByRefObject are implicitly marshal by value.

What is marshal by reference in C#?

Marshal by Reference: In this type proxy is created by the reference of the server objects. Class must extend the System. MarshalByRefObject to implement marshal by reference. Here, client keeps server object reference which means round trip to server with each method call.


2 Answers

The magic seems to be in a special TransparentProxy class - the .NET Runtime handles it in a special way.

  • If you want to use it, see an article about RealProxy
  • If you want to read more about the "magic", see an article on TransparentProxy implementation.
  • For a basic overview, try the MSDN article "Remoting: A Technical Overview".

I think that MarshalByRefObject may contain some additional internal information which can be helpful for this mechanism, but I haven't looked much into that.

like image 76
akavel Avatar answered Sep 18 '22 15:09

akavel


I believe MarshalByRefObject isn't all that special. I believe that its whole reason for existence lies with its lifetime management and how it's garbage-collected on the server. There are some good comments on what this is about in the LifetimeServices class documentation.

AFAIK, the real magic of remoting is done by the remoting infrastructure yourself when you set up the hosts. MarshalByRefObject isn't doing any of the real work of marshalling stuff across AppDomains.

like image 24
Dave Markle Avatar answered Sep 19 '22 15:09

Dave Markle