CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

Object Persistence and Remoting...

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

  1. Monitors a directory for file changes (camera images updated every minute)
  2. Does some fuzzy image analysis on the image to get a live traffic “count”
  3. 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.



About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News

MVP
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.