Non-remoting proxies

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a proper way to use the proxy classes to set up a proxy for a local object?

The code below is what I have tried. This seems at first to work, as the object I create is a transparent proxy, however, my overridden Invoke() is never called after the initial construction

I tried removing the attribute, and creating an instance of the proxy directly. This worked, however, at certain points in code execution the object returned would no longer be a transparent proxy, and instead the actual object itself. This at first seemed to only happen when passing the object to a method via the "this" keyword inside of the object. However, more testing showed that this was happening at other times as well

Is there a way to intercept all calls to a class? This is neccassary in my project to insure that an object written by any author cannot be used after it is no longer valid, since we are unable to delete objects explicitly.

Thank
[Master.MProxy
public abstract class Master : ContextBoundObjec

[AttributeUsage(AttributeTargets.Class)
internal class MProxyAttribute : ProxyAttribut

public override MarshalByRefObject CreateInstance(Type type

MarshalByRefObject m = base.CreateInstance(type)
MasterProxy proxy = new MasterProxy(type, m)
return (MarshalByRefObject)proxy.GetTransparentProxy()



/// <summary
/// Intercepts calls to make sure the object is valid
/// </summary
internal class MasterProxy : RealProx

private Master proxyObject

public MasterProxy(Type type, MarshalByRefObject obj) : base(type

proxyObject = (Master)obj

public override IMessage Invoke(IMessage message

if(message is IConstructionCallMessage


IConstructionCallMessage ccm = (IConstructionCallMessage)message

//initialize the proxy objec
return base.InitializeServerObject(ccm)


IMethodCallMessage methodMessage = (IMethodCallMessage)message

string methodName = methodMessage.MethodBase.Name

return RemotingServices.ExecuteMessage(proxyObject, methodMessage)


}
 
Not something you want to tackle. Try implementing IDisposable instead.
In the members of your class you can then check to see if the object is
disposed (in other words, someone called the Dispose method), and if so,
throw a new ObjectDisposedException.
There are a few samples in the MSDN about properly implementing IDispose.

-Rob Teixeira [MVP]

Yagi said:
Is there a proper way to use the proxy classes to set up a proxy for a local object?

The code below is what I have tried. This seems at first to work, as the
object I create is a transparent proxy, however, my overridden Invoke() is
never called after the initial construction.
I tried removing the attribute, and creating an instance of the proxy
directly. This worked, however, at certain points in code execution the
object returned would no longer be a transparent proxy, and instead the
actual object itself. This at first seemed to only happen when passing the
object to a method via the "this" keyword inside of the object. However,
more testing showed that this was happening at other times as well.
Is there a way to intercept all calls to a class? This is neccassary in my
project to insure that an object written by any author cannot be used after
it is no longer valid, since we are unable to delete objects explicitly.
Thanks
[Master.MProxy]
public abstract class Master : ContextBoundObject
{
[AttributeUsage(AttributeTargets.Class)]
internal class MProxyAttribute : ProxyAttribute
{
public override MarshalByRefObject CreateInstance(Type type)
{
MarshalByRefObject m = base.CreateInstance(type);
MasterProxy proxy = new MasterProxy(type, m);
return (MarshalByRefObject)proxy.GetTransparentProxy();
}
}

/// <summary>
/// Intercepts calls to make sure the object is valid.
/// </summary>
internal class MasterProxy : RealProxy
{
private Master proxyObject;

public MasterProxy(Type type, MarshalByRefObject obj) : base(type)
{
proxyObject = (Master)obj;
}
public override IMessage Invoke(IMessage message)
{
if(message is IConstructionCallMessage)
{

IConstructionCallMessage ccm = (IConstructionCallMessage)message;

//initialize the proxy object
return base.InitializeServerObject(ccm);
}

IMethodCallMessage methodMessage = (IMethodCallMessage)message;

string methodName = methodMessage.MethodBase.Name;

return RemotingServices.ExecuteMessage(proxyObject, methodMessage);
}
}
}
 
Oddly enough, I've pretty much got it working with just using the realproxy class and a create method of my own. Only I seem to be having a problem with ArrayLists. When trying to remove the object from the arraylist, nothing happens. The only member of the arraylist is the object I am trying to remove, and when comparing the member of the arraylist to my object directly, they do equal each other.

Why would the object I am trying to remove be located in the arraylist, and yet not be removed from a simple call to arrayList.Remove(obj)? Are there any other areas of the framework that simply won't work with proxies?

Kris

P.S. Unfortunately, using the IDisposable interface won't work in my situation. I do not have control over objects inherting from my class, yet must guarantee that the object is not usable after it is no longer valid. As I cannot guarantee that implementors of derivative classes will properly implement the IDisposable scheme, I would not be able to guarantee this using that design.
 
Back
Top