I discovered something interesting this week. Here's the scenario. I have a windows service which houses a singleton object that has live camera statistics generated from our gate traffic camera here at the port. What this service does is
- Monitors a directory for file changes (camera images updated every minute)
- Does some fuzzy image analysis on the image to get a live traffic “count”
- Stores a copy of the image in a byte array
The singleton object is marshaled like so:
// Configure Remoting
ChannelServices.RegisterChannel(new TcpChannel(8001));
RemotingConfiguration.RegisterWellKnownServiceType(typeof(VIT.Common.Classes.GateStatusRemotable), "GateStatus", WellKnownObjectMode.Singleton);
RemotingServices.Marshal(m_GateStatus, "GateStatus");
Now, I have need to remote to this object from two places, a windows application that acts as a service controller, and a web control that actually consumes the images and data, and publishes it on the web.
Everything worked great, except that I found that the singleton object would disappear if I didn't access it! Yikes! Well, it turns out that the solution was simple. MarshalByRefObjects contain a method called InitializeLifetimeService. You can overload this to control the lifetime of your remotable objects. To make my object live forever, I overloaded this method and returned null. Works perfectly! Here's the code.
public class GateStatusRemotable : MarshalByRefObject
{
public GateStatusCollection GateStatus;
public override Object InitializeLifetimeService()
{
return null;
}
}
You can also return an ILease object if you want to get fancy with this.
Posted
10-08-2003 10:47 AM
by
Brendan Tompkins