IStorageMechanism throws exception when used in ViewModel constructor

Jan 23, 2012 at 4:45 AM

Is it not allowed to pass IStorageMechanism into my ViewModel constructor like so:

  public MyClassINavigationService navigationService, IStorageMechanism storage)

 

 ?  If so, why am I experiencing the following exception?

System.InvalidOperationException was unhandled
  Message=Sequence contains more than one element
  StackTrace:
       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
       at Caliburn.Micro.SimpleContainer.GetInstance(Type service, String key)
       at Caliburn.Micro.SimpleContainer.<DetermineConstructorArgs>b__1a(ParameterInfo info)
       at System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()
       at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
       at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
       at Caliburn.Micro.SimpleContainer.DetermineConstructorArgs(Type implementation)
       at Caliburn.Micro.SimpleContainer.BuildInstance(Type type)
       at Caliburn.Micro.SimpleContainer.<>c__DisplayClass8.<RegisterPerRequest>b__7(SimpleContainer container)
       at Caliburn.Micro.SimpleContainer.GetInstance(Type service, String key)
       at TrackUTransit.Client.AppBootstrapper.GetInstance(Type service, String key)
       at Caliburn.Micro.ViewModelLocator.<.cctor>b__5(Type viewType)
       at Caliburn.Micro.ViewModelLocator.<.cctor>b__6(Object view)
       at Caliburn.Micro.FrameAdapter.OnNavigated(Object sender, NavigationEventArgs e)
       at System.Windows.Navigation.NavigationService.RaiseNavigated(Object content, Uri uri, NavigationMode mode, Boolean isNavigationInitiator, PhoneApplicationPage existingContentPage, PhoneApplicationPage newContentPage)
       at System.Windows.Navigation.NavigationService.CompleteNavigation(DependencyObject content, NavigationMode mode)
       at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)
       at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
       at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at System.Delegate.DynamicInvokeOne(Object[] args)
       at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
       at System.Delegate.DynamicInvoke(Object[] args)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
       at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
       at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
       at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
       at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

If I remove IStorageMechanism from my constructor, all is well.

Jan 23, 2012 at 7:28 AM

Given the stacktrace, I would guess that more than one IStoreMechanism entry has been registered in the container.

Jan 23, 2012 at 1:07 PM
Edited Jan 23, 2012 at 2:25 PM

I am not explicitly registering an instance of IStorageMechanism anywhere in the app. 

Exception is thrown because PhoneContainer has two implementations of IStorageMechanism registered (AppSettings and PhoneStateStorage).  Just need to figure out how to resolve one of them now.

Coordinator
Jan 23, 2012 at 6:14 PM

The phone container registers *all" implementations of IStorageMechanism with the container at startup.