문제

We are using Ninject with Caliburn.Micro to create a MVVM WCF Silverlight app. The issue I am having is with the life cycle of my view models.

I have created a simple ninject module to bind my view model and wcf client.

   public class IDCardModule : NinjectModule
   {
        public override void Load()
        {
            Bind<IIdCardManagerClient>().To<IdCardManagerClient>();
            Bind<IDCard.SL.ViewModel.IIdCardViewModel>().To<IDCard.SL.ViewModel.IdCardViewModel>();
        }
   }

In my IIdCardViewModel I have required it inherit from IDisposable, because I want to register and de-register for wcf events and some local unmanaged references.

However, Dispose is never being called.

I looked into adding a deactivation to call dispose like this:

Bind<IDCardExclude.SL.ViewModel.IIdCardExclusionViewModel>().To<IDCardExclude.SL.ViewModel.IdCardExclusionViewModel>().OnDeactivation(
                m => m.Dispose());

But that forced me to add two things, an Unload override in my IDCardModule that retrieved the object and released it:

var releaseMe = this.Kernel.Get<IIdCardViewModel>();
this.Kernel.Components.Get<Ninject.Activation.Caching.ICache>().Release(releaseMe);

and either .InThreadScope() or .InSingletonScope() to my Bind method in Load.

Is there an easier way to force a deactivation on a specific object? Or should I look into another IOC framework?

I looked into IStartable and had similar issues. As well, I read in-depth Nate's article where he has an activation block and wraps everything in a using statement. My issue there is that my view model can be long running and I don't believe his solution will work here. As well, having a special thread that sleeps and calls GC.Collect, doesn't smell right either.

도움이 되었습니까?

해결책

InTransientScoped objects lifecycle is not managed by Ninject. This means theese objects are not disposed and deactivated. If your view model is injected to another object you can use InParentScope from the named scope extension. See my blogpost about additional scopes of Ninject: http://www.planetgeek.ch/2010/12/08/how-to-use-the-additional-ninject-scopes-of-namedscope/

다른 팁

Lucas B,

I am not sure if this is gonna help you but I had disposal problems with my objects too leading in some cases to huge memory use. I found out that it was a problem of event subscription. Each time I was subscribing to an event, I was never unsubscribing to it after the event completion. As far as I understand, an object is not destroyed if it is still subscribing to an event ... So be sure to do so ( -= ).

Hope this help even if it is not totally related to you post.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top