Blog'A'Little

Just another C# guy

CrossAppDomainSingleton - Update

I forgot a couple of very essential things in my last post about a singleton that lives across appdomains.

When accessing a remote object using remoting, like I did with the singleton implementation, the remoting system obtains a lease for the object. The lease period is implemented in the MarshalByRefObject class through a method called InitializeLifeTimeService(). A singleton like the one we wanted to achieve needs to live forever, therefor you need to override the InitializeLifeTimeService() method and always return null. Returning null means that it should live forever (more details can be at http://msdn.microsoft.com/msdnmag/issues/03/12/LeaseManager/default.aspx)

1
2
3
4
5
6
7
8
9
10
11
12
13
    /// <summary>
    /// Override of lifetime service initialization.
    ///
    /// The reason for overriding is to have this singleton live forever
    /// </summary>
    /// <returns>object for the lease period - in our case always null</returns>
    public override object InitializeLifetimeService()
    {
      // We want this singleton to live forever
      // In order to have the lease across appdomains live forever,
      // we return null.
      return null;
    }

In addition I've made it all threadsafe. So look at the attachment for this post instead..

On a second note; if you for instance expose an event in your singleton and any subscribers to that event exists in another AppDomain, you might want to keep in mind that you should probably inherit from MarshalByRefObject and override the IntializeLifeTimeService() method and return null there as well. Otherwize you might end up having a broken lease in the delegate added to the singleton.

kick it on DotNetKicks.com kick it on GameDevKicks.com

Comments

Jesse Chisholm said:

I'm surprised this hasn't had any comments in over a year.

Thanks for the improvements.

What is the best way to kill this singleton as part of an explicit clean application shutdown?

-Jesse

kick it on DotNetKicks.com kick it on GameDevKicks.com
# september 26, 2008 2:24

baimos said:

Hi, I have one question. What about private constructor? If I don't make a mistake the code line appDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName) will throw exception in such case.

kick it on DotNetKicks.com kick it on GameDevKicks.com
# oktober 15, 2008 4:21

Egor said:

Thanks for this, it gave me a great head start. I just wanted to say that your update is broken - it misses the singletony part of it, a new instance will be created for every domain. You need to include this part from the original:

Type type = typeof(T);

    T instance = (T)appDomain.GetData(type.FullName);

    if (null == instance)

    {

     instance = (T)appDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);

     appDomain.SetData(type.FullName, instance);

    }

kick it on DotNetKicks.com kick it on GameDevKicks.com
# februar 20, 2009 1:59

Duarte Nunes said:

Hi,

I think the synchronization is faulty, as it doesn't prevent the creation of multiple app domains (the uniqueness of the friendly name is not enforced) and the respective T instances. You need to protect the critical section with a named native NT object, which is inherently shared among app domains (for example, var mutex = new Mutex(false, AppDomainName)).

Best regards,

Duarte

kick it on DotNetKicks.com kick it on GameDevKicks.com
# august 24, 2009 11:50

Jason Jakob said:

Correction Egor instead of creating a new variable to hold the instance you must use the member variable _instance. like so:

Type type = typeof(T);

                       _instance = (T)appDomain.GetData(type.FullName);

                       if (null == _instance) {

                           _instance = (T)appDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);

                           appDomain.SetData(type.FullName, _instance);

                       }

kick it on DotNetKicks.com kick it on GameDevKicks.com
# oktober 13, 2009 10:09

http://yhvoeq.com/ said:

kaUhqrU

kick it on DotNetKicks.com kick it on GameDevKicks.com
# juli 26, 2010 11:55

http://wylhgsbdoyog.com/ said:

OX2Fjl  <a href="omoteyyrqrsb.com/.../a>, [url=http://jjuxvufcyoyn.com/]jjuxvufcyoyn[/url], [link=http://qaybdydgbuyl.com/]qaybdydgbuyl[/link], http://flveaxkamhbl.com/

kick it on DotNetKicks.com kick it on GameDevKicks.com
# oktober 3, 2010 8:25

Pherionerib said:

Great news, everyone! Today all of us acute needs quest of some watering-hole of piece and harmony.

Completele the only is thecutestsiteintheworld.com - this site.

kick it on DotNetKicks.com kick it on GameDevKicks.com
# oktober 12, 2010 11:23

Paixekeh said:

Maybe I`ll be Captain Obvious, but... it's only few days to New Year last, so let's be happy!

Hoho3ho!)

kick it on DotNetKicks.com kick it on GameDevKicks.com
# desember 19, 2010 9:45

Accerrive said:

Happy New Yearhttp://nfksghjskf.com/ - , everyone! :)

kick it on DotNetKicks.com kick it on GameDevKicks.com
# desember 25, 2010 10:59

Loalmleta said:

Happy Fresh Year<a href=sdjfh.in/.../a> harry! :)

kick it on DotNetKicks.com kick it on GameDevKicks.com
# januar 5, 2011 2:16

http://mxvgeo.com/ said:

xzWwaDw

kick it on DotNetKicks.com kick it on GameDevKicks.com
# januar 31, 2011 12:17

Jailen said:

Aritcels like this make life so much simpler.

kick it on DotNetKicks.com kick it on GameDevKicks.com
# desember 10, 2011 7:40
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Design downloaded from Free Templates - your source for free web templates