Using Lazy<> with Prism.DryIoc.Forms gives "container is garbage collected" exception

635 views Asked by At

We're using Prism.DryIoc.Forms to create apps with Xamarin.Forms. To minimize the startup time of the app we are using the Lazy<> pattern for classes with a lot of dependencies.

This used to work fine with Prism.Unity.Forms. However, I can't get it to work with Prism.DryIoc.Forms. Any help would be appreciated.

The code is as follows. We have a page view model like this:

public class MySamplePageViewModel
{
    private readonly Lazy<ISomeClass> _lazySomeClass;

    public MySamplePageViewModel(Lazy<ISomeClass> lazySomeClass)
    {
        _lazySomeClass = lazySomeClass;
    }

    public void SomeMethod()
    {
        _lazySomeClass.Value.DoIt(); //throws exception
    }
}

However, after the page view model is being instantiated, when calling _lazySomeClass.Value we get an exception with message "Container is no longer available (has been garbage-collected).". It seems to be related to how Prism resolves the view model, because when calling the following it works fine:

var container = (Application.Current as PrismApplicationBase<IContainer>).Container;
var lazySomeClass = container.Resolve<Lazy<ISomeClass>>();
lazySomeClass.Value.DoIt(); //works fine

we're doing the registration like this:

container.Register<ISomeClass, SomeClass>(Reuse.Singleton);
container.RegisterTypeForNavigation<MySamplePage, MySamplePageViewModel>("MySamplePage");
1

There are 1 answers

2
dadhi On BEST ANSWER

The problem should be fixed in v2.10.3.

Therefore the next logical step is to ask Prism.DryIoc.Forms maintainers to update to the latest DryIoc version.