Running with Code Like with scissors, only more dangerous

29Jan/080

The Remoting Mystery… solved!

I remarked yesterday that I was having difficulty accessing remoted objects – sometimes I would never break into the methods I was calling, and sometimes calling RemotingServices.Disconnect() would fail.  I was a bit miffed about all this, and to say the documentation was unclear about why precisely an object would intermittently just stop.  I was custom-marshaling the object upon startup of the host program; it wasn’t server-activated, and shouldn’t have any reason for just going away.  But the failure was intermittent; I couldn’t regularly reproduce the problem, and it wasn’t raising an exception – it was just returning false.

Well, I went to lunch today and left my remoting host program running.  When I got back I tried to debug the client application, and of course it failed.  When I tried to close the server application, though, I didn’t get my regular InvalidOperationException (the one that I was throwing) – I got a RemotingException.  And before I quickly hit enter to close the console window, I noticed it said something about how the object no longer lived on the server and therefore couldn’t be shown along the remoting channels.

It turns out that MarshalByRefObject – the class from which I’m deriving my service specification objects – this class provides a method to specify the object’s lifetime called InitializeLifetimeService().  This method returns an object that implements the System.Runtime.Remoting.Lifetime.ILease interface.  By changing the InitialLeaseTime value of this object to zero, the object can last forever while marshaled.  I overrode this method in a new base class, and then derived the services from this class:

   1:  public class RemotedServiceObject : MarshalByRefObject
   2:  {
   3:      public override object InitializeLifetimeService()
   4:      {
   5:          ILease lease = (ILease)base.InitializeLifetimeService();
   6:          if (lease.CurrentState == LeaseState.Initial)
   7:          {
   8:              lease.InitialLeaseTime = TimeSpan.Zero;
   9:          }
  10:          return lease;
  11:      }
  12:  }

So great news – it worked out perfectly.  No more returning false, and no more inability to connect to the object after a while.

Finally, remoting behaves like I tell it to!

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.

No trackbacks yet.