Extending RealProxy

This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the Windows Communication Foundation (WCF).

Extending the proxy can be useful when you want to participate in the proxy creation process, marshal remote method calls, or determine object identity, among other things. You can build your own proxy using the extensible RealProxy class. To build a custom proxy involves deriving a class from the RealProxy class and overriding the RealProxy.Invoke method. The new (New in Visual Basic) statement can be intercepted by deriving from the ProxyAttribute attribute and applying the attribute to a child of ContextBoundObject. (Applying the proxy attribute to a child of MarshalByRefObject is not supported.) When new is called, the derived ProxyAttribute creates an instance of the custom proxy. An application can also create a custom proxy instance directly.

When the application code calls a method on a custom proxy, the overridden RealProxy.Invoke method is called with an object that implements IMessage. The IMessage implementation provides an IDictionary implementation that provides name/value pairs of information about the method. For more information about particular entries in the dictionary, see the reference documentation for the IMessage interface and related interfaces (IMethodCallMessage and IMethodReturnMessage).

A real object can be called from the overridden Invoke method by calling RemotingServices.ExecuteMessage, which dispatches the call to the real object.

You can also use the EnterpriseServicesHelper.CreateConstructionReturnMessage method to process an IConstructionCallMessage object and generate an IConstructionReturnMessage object. You can also use the RealProxy.InitializeServerObject method to create the backing object (the object represented by the proxy).

When using the derived ProxyAttribute, you can create a real object in the overridden ProxyAttribute.CreateInstance method and store it as a field in the custom proxy. The custom proxy can marshal the real object to obtain the ObjRef object that contains the Uniform Resource Identifier (URI). The URI must be stored in the proxy, because the "__Uri" entry in the collection returned by the IMessage.Properties property must be set in the IMessage implementation to dispatch a call to a real object.

Of course, the message does not have to be dispatched on a real object; you can perform some task with it in Invoke and generate and return an object that implements the IMethodReturnMessage) interface.

To participate in marshaling, override RealProxy.CreateObjRef and provide a custom ObjRef that extends ObjRef. If you want to add custom data to the custom ObjRef, override ObjRef.GetObjectData. You add your custom data and delegate to the ObjRef.GetObjectData method to modify the object identity capabilities of the .NET remoting marshaling system.

During deserialization, the remoting system calls your overridden GetRealObject on the custom ObjRef. Here, you should delegate to the base GetRealObject method because the base handles the object identity and sets up remoting channels. The base also calls your overridden ProxyAttribute.CreateProxy method to allow you to set up your custom proxy.

ObjRef.IsFromThisAppDomain and ObjRef.IsFromThisProcess can be used to determine how you want to unmarshal.

Note that if you do not provide a custom ObjRef during marshaling, the remoting system automatically marshals and unmarshals the object and the custom proxy is not used in the caller's application domain.

See Also

Reference

RealProxy
ProxyAttribute
RemotingServices
IMessage
IMethodReturnMessage
IMethodCallMessage

Other Resources

Advanced Remoting
Custom Proxies Technology Sample