tags:

views:

126

answers:

3

When we call any method that's outside the machine boundaries ( remote methods ) , how does the CLR know this and then serialize ( or make copies of ) actual objects and not the memory addresses ( references to the actual objects ) to send when any method is called crossing the machine boundaries ?

This question came to my mind realizing that always a reference is sent when method is called, instead of its copy.


well, I am talking about any kind of remote call, be it a remoting or web service. I mean, the client has to call a method and send the parameters, now if it's local, it'll send just memory address of the objects to work with them, else it'll either make copy or serialize it in XML or alike. So, how does it know what to do ?

A: 

If an object inherits from MarshalByRefObject it will be remote invoked. If not it is serialized and sent.

Jonathan C Dickinson
"Client" objects can also inherit MarshalByRefObject. In this case they will be "proxy-ed", as opposed being passed "by value".
DreamSonic
Okey. This should be applicable in case of Remoting. Other kinds of RPC are also involved.
Kunal S
+4  A: 

All remote interactions are based on contracts.

  • In case of raw sockets its an application level protocol. (I'll omit this one.)
  • In case of remoting it's mostly interface-based contracts. Client knows the methods and properties of the server it's talking to. Data is sent over the wire (or just across appdomain boundaries) in serialized form (xml or binary). As Jonathan had noted, there's an exception to this rule: MarshalByRefObject. It's inheritors are not serialized, but passed "by reference" (marshaled, "proxy-ed") instead.
  • In case of web-services (and mostly WCF) it's WSDL-based contracts. Client knows the XML-schema - a language the server understands. Data is serialized in SOAP and is sent over the wire.

As you can see there's no magic there.

DreamSonic
+1  A: 

In remoting the actual object reference you have on the client is a fake object called a "transparent proxy", when you call a method on that object the CLR detects it's a proxy, packages all the parameters and call a special method.

This special method then serializes all the parameters, send them over the wire, wait for the reply message and de-serialize the return value.

See documentation of the RealProxy class for more details (You can create a proxy for any object that inherits from MarshelByRefObject, the special object that handles the call behind the scenes inherits from RealProxy)

In the case of web services the process is similar but instead of using the special CLR proxy you use a code generated proxy created by Visual Studio.

Nir
@Nir: Thank you for elaborated explaination !
Kunal S