IndexOutOfRangeException in Bootstrapper

Topics: Bugs, Getting Started
Feb 1, 2012 at 7:39 PM
Edited Feb 1, 2012 at 7:59 PM

I'm getting an IndexOutOfRangeException from the PhoneContainer upon starting my App, when container.GetInstance() is called. The service that's requested (MainPageViewModel) is registered with the container, I've checked using the debugger.

This started happening after a fairly extensive rewrite without testing in between (I know, bad...) with no commits to any sort of version control in between (I knooow, even worse), so I'm not sure I haven't broken something, but I can't find any hints on where to look for an error (i.e. no compiler warnings or anything), just the one exception. I'm marking this as "Bugs" for now, but it might well turn out to be an error of mine.

I'm pretty desperate at this point, mostly because this is part of a project with a presentation due tomorrow... any help?

 

EDIT: I've narrowed it down to happening only when I add UserDataContext (a LINQ to SQL DataContext) to the container (no matter if as a Singleton or PerRequest). Even when the program doesn't get far enough to reference UserDataContext, if breaks on GetInstance(MainPageViewModel, null)

Coordinator
Feb 1, 2012 at 8:07 PM

My guess is that your MailPageViewModel has a ctor dependency that is not registered in the container. Therefore, it cannot create the MainPageViewModel. Just a guess...

Feb 1, 2012 at 8:18 PM

I believe the ctor dependencies are fine... I've also put break points into all the constructors, but none of them are called at all...

Feb 1, 2012 at 9:22 PM
Edited Feb 1, 2012 at 9:23 PM

It might help if you posted some of your code: the ctor of MainPageViewModel and the registration calls to the IoC container.

Feb 1, 2012 at 9:53 PM

I've found a solution that works - moving all the methods and non-database properties from the DataContext into a seperate class, and registering that in the bootstrapper. For completeness, here's my MainViewModel constructor and registration calls:

public MainPageViewModel(INavigationService navService, ContactsViewModel cvm)
        {
            this.navService = navService;

            if (firstConstructor)
            {
                myPopup = new Popup() { IsOpen = false, Child = new StartupSplashScreen() };
            }
            else
            {
                myPopup = new Popup() { IsOpen = false, Child = new SplashScreen() };
            }
            this.cvm = cvm;
            rvm = new RecentsViewModel();
            Items.Add(cvm);
            Items.Add(rvm);
            ActivateItem(cvm);
        }
protected override void Configure()
        {
            container = new PhoneContainer(RootFrame);
            container.RegisterPhoneServices();
            container.PerRequest<MainPageViewModel>();
            container.Singleton<ContactsViewModel>();
            container.PerRequest<RecentsViewModel>();
            container.PerRequest<SettingsPageViewModel>();
            container.PerRequest<ContactEditPageViewModel>();
            container.PerRequest<ContactDetailsPageViewModel>();
            container.PerRequest<GroupDialogViewModel, GroupDialogViewModel>();
            container.Singleton<SettingsModel>();
            container.Singleton<UserDataContext>();
            AddCustomConventions();
        }

Feb 1, 2012 at 10:02 PM

Why do you register the RecentsViewModel with the IoC container, but new it up explicitly inside the constructor of the MainPageViewModel? 

Feb 1, 2012 at 10:07 PM

Umm... RecentsViewModel is actually not really implemented yet, so I didn't pay much attention to it, but I did try taking it out of the register calls to no avail.

Feb 1, 2012 at 10:23 PM

But it works now, right?

Feb 1, 2012 at 10:33 PM

The example posted above shows what it looked like when it didn't work. Commenting out the registration for UserDataContext (not MainPageViewModel) made the error go away.

If UserDataContext is constructed with new, everything works (nothing wrong in the ctor). If it's registered to the PhoneContainer as either Singleton or PerRequest, it breaks, even before anything using UserDataContext is used.

A plain DataContext with nothing but Tables does not cause the error.

The class I split off of UserDataContext like I said above works fine if registered as Singleton or PerRequest.

I'm still confused... each of the parts works without a hitch, but together they cause that cryptic error...