GetInstance exception

Jun 2, 2011 at 8:56 PM

After upgrading from v1.0 to v1.1 during start of app i'm getting "Sequence contains more than one element" in

public object GetInstance(Type service, string key) {
            var entry = GetEntry(service, key);
            if (entry != null) {
                return entry.Single()();  - exception

service and key are nulls!.

Jun 2, 2011 at 9:08 PM
Edited Jun 2, 2011 at 9:08 PM

It was related with missing custom rules in ViewLocator.NameTransformer.

Jun 2, 2011 at 9:09 PM

Is it a WP7 app? If so, we no longer locate page VMs by key but by type. They need to be registered by type.

Jun 12, 2011 at 6:47 AM

I'm getting the same error after upgrading to v1.1 for WP7.

I registered with the new way.



Jun 12, 2011 at 10:58 AM

If you can create a simple solution that reproduces the problem, then please create an issue, attach the solutions, and I'll have a look at it.

Jun 13, 2011 at 4:10 AM
Edited Jun 13, 2011 at 4:12 AM

I moved from v1.0 to v1.1 and I just changed the configuration part alone and the code is not much difference. I pasted both the old and new code.

Old Code

  protected override void Configure()
            container = new PhoneContainer(RootFrame);
            dataservices = new DataService();
            TiltEffect.SetIsTiltEnabled(RootFrame, true);
            container.RegisterSingleton(typeof(AddStockViewModel), "AddStockViewModel"typeof(AddStockViewModel));
            if (!ConstantUtil.IsFreeVersion)
                container.RegisterSingleton(typeof(SettingsViewModel), "SettingsViewModel"typeof(SettingsViewModel));
            container.RegisterSingleton(typeof(StocksViewModel), "StocksViewModel"typeof(StocksViewModel));
            container.RegisterSingleton(typeof(PortfolioScreenViewModel), "PortfolioScreenViewModel"typeof(PortfolioScreenViewModel));
            container.RegisterPerRequest(typeof(StockInfoViewModel), "StockInfoViewModel"typeof(StockInfoViewModel));
            container.RegisterPerRequest(typeof(ChartViewModel), "ChartViewModel"typeof(ChartViewModel));
            container.RegisterSingleton(typeof(InformationViewModel), "InformationViewModel"typeof(InformationViewModel));
            container.RegisterSingleton(typeof(DetailPageViewModel), "DetailPageViewModel"typeof(DetailPageViewModel));
            container.RegisterInstance(typeof(INavigationService), nullnew FrameAdapter(RootFrame));
            container.RegisterInstance(typeof(IDataService), null, dataservices);
            container.RegisterInstance(typeof(IEventAggregator), nullnew EventAggregator());

             Helper.CachedDataService = dataservices;

New Code

  protected override void Configure()
            container = new PhoneContainer(RootFrame);
            dataservices = new DataService();
            TiltEffect.SetIsTiltEnabled(RootFrame, true);
            if (!ConstantUtil.IsFreeVersion)
            container.Instance<INavigationService>(new FrameAdapter(RootFrame));
            container.Instance<IEventAggregator>(new EventAggregator());
            Helper.CachedDataService = dataservices;


Jun 13, 2011 at 1:01 PM

Definitely there's a problem with the config. The call to RegisterPhoneServices does a bunch of stuff for you, so you don't need to register INavigationService or IEventAggregator.

Jun 13, 2011 at 10:17 PM

I still get the same error even after removing the register INavigationService or IEventAggregator.

Jun 14, 2011 at 12:19 AM

Well, I can't see anything else wrong with your configuration. There's something else going on somewhere, but I can't tell from this sample. If you want to send me some code in an email, I can take a quick look. robertheisenberg at hotmail dot com

Jun 14, 2011 at 5:07 AM

Robert, Thanks for your reply. I emailed you an sample project.

Jun 14, 2011 at 12:37 PM

Your problem is that you don't follow the naming conventions for View => ViewModel resolution. You have AddSymbolViewModel.cs and then you have AddSymbol. But, it should be AddSymbolView, as that is the supported convention. If you want to support having just AddSymbol, you can add a custom rule to the NameTransformer of the ViewModelLocator. You should also add the corresponding, reverse rule, to the ViewLocator. You can read about that here The IoC error is confusing, I agree. I'll see if I can improve that in the next version.

Jun 14, 2011 at 1:01 PM

Thanks for your reply and I will update my view name. But why it works only for MainPage.xaml

Jun 14, 2011 at 2:05 PM

Because we recognize "Page" as a special naming convention. That's basically to make it work with the out-of-the-box WP7 template that names the default view MainPage.