Search code examples
.netremoting

What are real and transparent proxies


In .NET remoting, there are two kind of proxies:

  1. Transparent Proxies
  2. Real Proxies

What is the difference between them?


Solution

  • From MSDN:

    The TransparentProxy is an internal class that cannot be replaced or extended. On the other hand, the RealProxy and ObjRef classes are public and can be extended and customized when necessary. The RealProxy class is an ideal candidate for performing load balancing for example, since it handles all function calls on a remote object. When Invoke is called, a class derived from RealProxy can obtain load information about servers on the network and route the call to an appropriate server. Simply request a MessageSink for the required ObjectURI from the Channel and call SyncProcessMessage or AsyncProcessMessage to forward the call to the required remote object. When the call returns, the RealProxy automatically handles the return parameter.

    Here's a code snippet that shows how to use a derived RealProxy class.

    MyRealProxy proxy = new MyRealProxy(typeof(Foo));
    Foo obj = (Foo)proxy.GetTransparentProxy();
    int result = obj.CallSomeMethod();
    

    The TransparentProxy obtained above can be forwarded to another application domain. When the second client attempts to call a method on the proxy, the remoting framework will attempt to create an instance of MyRealProxy, and if the assembly is available, all calls will be routed through this instance. If the assembly is not available, calls will be routed through the default remoting RealProxy.

    An ObjRef can easily be customized by providing replacements for default ObjRef properties TypeInfo, EnvoyInfo, and ChannelInfo. The following code shows how this can be done.

    public class ObjRef {
      public virtual IRemotingTypeInfo TypeInfo 
      {
        get { return typeInfo;}
        set { typeInfo = value;}
      }
    
      public virtual IEnvoyInfo EnvoyInfo
      {
        get { return envoyInfo;}
        set { envoyInfo = value;}
      }
    
      public virtual IChannelInfo ChannelInfo 
      {
        get { return channelInfo;}
        set { channelInfo = value;}
      }
    }