Share via


Extending RealProxy

Extending the proxy can be useful when you want to participate in the proxy creation process, marshaling, or 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 further details on 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 implementation and generate an IConstructionReturnMessage implementation. You can also use the RealProxy.InitializeServerObject method to create the backing object.

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 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 will call your overridden IObjectReference.GetRealObject on the custom ObjRef. Here, you should delegate to the base ObjRef.GetRealObject method because the base will handle the object identity and set up remoting channels. The base will also call 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 will automatically marshal and unmarshal the object, and the custom proxy will be not be used in the caller's application domain.

See Also

Advanced Remoting | RealProxy | ProxyAttribute | RemotingServices | IMessage | IMethodReturnMessage | IMethodCallMessage