RegisterViewWithRegion Silverlight v4 "key not found" error

Topics: Prism v4 - Silverlight 4
Nov 3, 2010 at 12:13 AM

Very strange and I'm not sure what is going on.  I have a colleague and I've downloaded his code from source safe.  His code runs, mine does not.

The area in question:

foreach (Region r in regionManager.Regions)

  Debug.WriteLine(r.Name);

Type myType = typeof(MWSMain);          

 regionManager.RegisterViewWithRegion("mainRegion", typeof(MWSMain));

On bolded line this is the error:

System.Collections.Generic.KeyNotFoundException crossed a native/managed boundary  Message=The given key was not present in the dictionary.  StackTrace:       at System.ThrowHelper.ThrowKeyNotFoundException()       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)  InnerException: 

The keys are there, as the two lines above execute as expected.

the part that concerns me is the native/managed boundry.

 

Any help would be wonderful

Nov 3, 2010 at 7:22 PM

Hi,

Based on my understanding, in your box when you try to register a view in the MainRegion you are receiving an KeyNotFoundException. More specifically it occurs when calling the RegisterViewWithRegion method in the RegionManager class.

I am not sure why you are receiving this exception, so could you please share the full exception message as well as the stack trace. On the other hand, if you continue experiencing this error, could you please share a repro sample? For example you could use Windows Live SkyDrive.

Fernando Antivero
http://blogs.southworks.net/fantivero

Nov 3, 2010 at 9:32 PM

The code was there, but I'll add it again.

 

public void Initialize()
 {
	foreach (Region r in regionManager.Regions)	
		Debug.WriteLine(r.Name);
	Type myType = typeof(MWSMain);
	// crashes when executing the next line.
         regionManager.RegisterViewWithRegion("mainRegion", typeof(MWSMain));
}

Exception detail:
System.Collections.Generic.KeyNotFoundException crossed a native/managed boundary
  Message=The given key was not present in the dictionary.
  StackTrace:
       at System.ThrowHelper.ThrowKeyNotFoundException()
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
  InnerException: 


Stack trace:
 	mscorlib.dll!System.ThrowHelper.ThrowKeyNotFoundException() + 0x1d bytes	
 	mscorlib.dll!System.Collections.Generic.Dictionary.this[string].get(string key) + 0x39 bytes	
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	System.Windows.dll!System.Windows.IndexerPathStep.ValidIndex(string index) + 0xdd bytes	
 	System.Windows.dll!System.Windows.IndexerPathStep.IsConnected.get() + 0x12 bytes	
 	System.Windows.dll!System.Windows.IndexerPathStep.ConnectToIndexerInSource() + 0x113 bytes	
 	System.Windows.dll!System.Windows.IndexerPathStep.ConnectToIndexer() + 0x35 bytes	
 	System.Windows.dll!System.Windows.IndexerPathStep.ReConnect(object newSource) + 0x21 bytes	
 	System.Windows.dll!System.Windows.PropertyPathListener.ReConnect(object source) + 0x15 bytes	
 	System.Windows.dll!System.Windows.Data.BindingExpression.SourceAcquired() + 0x19 bytes	
 	System.Windows.dll!System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) + 0x5b bytes	
 	System.Windows.dll!System.Windows.Data.BindingExpression.DataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) + 0xc bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.OnDataContextChanged(System.Windows.DataContextChangedEventArgs e) + 0x17 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e = {System.Windows.DataContextChangedEventArgs}) + 0x26 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e = {System.Windows.DataContextChangedEventArgs}) + 0xd0 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) + 0x32 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e = {System.Windows.DataContextChangedEventArgs}) + 0xd0 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) + 0x32 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e = {System.Windows.DataContextChangedEventArgs}) + 0xd0 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) + 0x32 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e = {System.Windows.DataContextChangedEventArgs}) + 0xd0 bytes	
 	System.Windows.dll!System.Windows.FrameworkElement.OnTreeParentUpdated(System.Windows.DependencyObject newParent, bool bIsNewParentAlive) + 0x3d bytes	
 	System.Windows.dll!System.Windows.DependencyObject.UpdateTreeParent(MS.Internal.IManagedPeer oldParent = null, MS.Internal.IManagedPeer newParent = {System.Windows.Controls.Grid}, bool bIsNewParentAlive, bool keepReferenceToParent = true) + 0x4e bytes	
 	System.Windows.dll!MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(System.IntPtr oldParentElement, System.IntPtr parentElement, System.IntPtr childElement, byte bIsParentAlive, byte bKeepReferenceToParent, byte bCanCreateParent) + 0xfe bytes	
 	[Managed to Native Transition]	
 	System.Windows.dll!MS.Internal.XcpImports.Collection_AddValue(System.Windows.PresentationFrameworkCollection collection = {System.Windows.Controls.UIElementCollection}, MS.Internal.CValue value) + 0x2d bytes	
 	System.Windows.dll!MS.Internal.XcpImports.Collection_AddDependencyObject(System.Windows.PresentationFrameworkCollection collection, System.Windows.DependencyObject value = {MWS.MWSMain}) + 0x65 bytes	
 	System.Windows.dll!System.Windows.PresentationFrameworkCollection.AddDependencyObject(System.Windows.DependencyObject value) + 0x36 bytes	
 	System.Windows.dll!System.Windows.Controls.UIElementCollection.AddInternal(System.Windows.UIElement value) + 0xb bytes	
 	System.Windows.dll!System.Windows.Controls.ItemsControl.AddVisualChild(int containerIndex, System.Windows.DependencyObject container = {MWS.MWSMain}, bool needPrepareContainer = true) + 0x5f bytes	
 	System.Windows.dll!System.Windows.Controls.ItemsControl.AddContainerForPosition(System.Windows.Controls.Primitives.GeneratorPosition position) + 0xd9 bytes	
 	System.Windows.dll!System.Windows.Controls.ItemsControl.OnItemsChangedHandler(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs args) + 0x5b bytes	
 	System.Windows.dll!System.Windows.Controls.ItemContainerGenerator.OnItemAdded(object item, int index = 0, bool suppressEvent) + 0x201 bytes	
 	System.Windows.dll!System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.ICollectionChangedListener.OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) + 0x9c bytes	
 	System.Windows.dll!System.Windows.Controls.WeakCollectionChangedListener.SourceCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x3d bytes	
 	System.Windows.dll!System.Windows.Controls.ItemCollection.NotifyCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x37 bytes	
 	System.Windows.dll!System.Windows.Controls.ItemCollection.System.Windows.Controls.ICollectionChangedListener.OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x20 bytes	
 	System.Windows.dll!System.Windows.Controls.ItemCollection.EnumerableCollectionView.System.Windows.Controls.ICollectionChangedListener.OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x4f bytes	
 	System.Windows.dll!System.Windows.Controls.WeakCollectionChangedListener.SourceCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x3d bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.ViewsCollection.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) + 0x43 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(System.Collections.IList items = Count = 1) + 0xb5 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.ViewsCollection.SourceCollectionChanged(object sender = Count = 1, System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) + 0x1e6 bytes	
 	System.Windows.dll!System.Collections.ObjectModel.ObservableCollection.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x37 bytes	
 	System.Windows.dll!System.Collections.ObjectModel.ObservableCollection.InsertItem(int index, System.__Canon item) + 0x94 bytes	
 	mscorlib.dll!System.Collections.ObjectModel.Collection.Add(Microsoft.Practices.Prism.Regions.ItemMetadata item) + 0x69 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.Region.InnerAdd(object view = {MWS.MWSMain}, string viewName = null, Microsoft.Practices.Prism.Regions.IRegionManager scopedRegionManager = {Microsoft.Practices.Prism.Regions.RegionManager}) + 0x25f bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.Region.Add(object view = {MWS.MWSMain}, string viewName = null, bool createRegionManagerScope = false) + 0x75 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.Region.Add(object view = {MWS.MWSMain}) + 0x2f bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.AddViewIntoRegion(object viewToAdd = {MWS.MWSMain}) + 0x34 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(object sender = {Microsoft.Practices.Prism.Regions.RegionViewRegistry}, Microsoft.Practices.Prism.Regions.ViewRegisteredEventArgs e = {Microsoft.Practices.Prism.Regions.ViewRegisteredEventArgs}) + 0xc3 bytes	
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	mscorlib.dll!System.Delegate.DynamicInvokeImpl(object[] args) + 0x76 bytes	
 	mscorlib.dll!System.Delegate.DynamicInvoke(object[] args) + 0xb bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Events.WeakDelegatesManager.Raise(object[] args = {object[2]}) + 0x1b9 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.RegionViewRegistry.OnContentRegistered(Microsoft.Practices.Prism.Regions.ViewRegisteredEventArgs e = {Microsoft.Practices.Prism.Regions.ViewRegisteredEventArgs}) + 0x97 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.RegionViewRegistry.RegisterViewWithRegion(string regionName = "mainRegion", System.Func<object> getContentDelegate = {System.Func<object>}) + 0x60 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.RegionViewRegistry.RegisterViewWithRegion(string regionName = "mainRegion", System.Type viewType = {System.RuntimeType}) + 0x8d bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Regions.RegionManagerExtensions.RegisterViewWithRegion(Microsoft.Practices.Prism.Regions.IRegionManager regionManager = {Microsoft.Practices.Prism.Regions.RegionManager}, string regionName = "mainRegion", System.Type viewType = {System.RuntimeType}) + 0x60 bytes	
>	MWS!MWS.MWS.Initialize() Line 30 + 0x3f bytes	C#
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.ModuleInitializer.Initialize(Microsoft.Practices.Prism.Modularity.ModuleInfo moduleInfo = {Microsoft.Practices.Prism.Modularity.ModuleInfo}) + 0x99 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.ModuleManager.InitializeModule(Microsoft.Practices.Prism.Modularity.ModuleInfo moduleInfo = {Microsoft.Practices.Prism.Modularity.ModuleInfo}) + 0x40 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.ModuleManager.LoadModulesThatAreReadyForLoad() + 0x156 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.ModuleManager.IModuleTypeLoader_LoadModuleCompleted(object sender = {Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader}, Microsoft.Practices.Prism.Modularity.LoadModuleCompletedEventArgs e = {Microsoft.Practices.Prism.Modularity.LoadModuleCompletedEventArgs}) + 0xa1 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.RaiseLoadModuleCompleted(Microsoft.Practices.Prism.Modularity.LoadModuleCompletedEventArgs e = {Microsoft.Practices.Prism.Modularity.LoadModuleCompletedEventArgs}) + 0x41 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.RaiseLoadModuleCompleted(Microsoft.Practices.Prism.Modularity.ModuleInfo moduleInfo = {Microsoft.Practices.Prism.Modularity.ModuleInfo}, System.Exception error = null) + 0x46 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.HandleModuleDownloaded(Microsoft.Practices.Prism.Modularity.DownloadCompletedEventArgs e = {Microsoft.Practices.Prism.Modularity.DownloadCompletedEventArgs}) + 0x249 bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.IFileDownloader_DownloadCompleted(object sender = {Microsoft.Practices.Prism.Modularity.FileDownloader}, Microsoft.Practices.Prism.Modularity.DownloadCompletedEventArgs e = {Microsoft.Practices.Prism.Modularity.DownloadCompletedEventArgs}) + 0x14b bytes	
 	Microsoft.Practices.Prism!Microsoft.Practices.Prism.Modularity.FileDownloader.WebClient_OpenReadCompleted(object sender = {System.Net.WebClient}, System.Net.OpenReadCompletedEventArgs e = {System.Net.OpenReadCompletedEventArgs}) + 0x4a bytes	
 	System.Net.dll!System.Net.WebClient.OnOpenReadCompleted(System.Net.OpenReadCompletedEventArgs e) + 0x17 bytes	
 	System.Net.dll!System.Net.WebClient.OpenReadOperationCompleted(object arg) + 0x31 bytes	
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	mscorlib.dll!System.Delegate.DynamicInvokeImpl(object[] args) + 0x76 bytes	
 	mscorlib.dll!System.Delegate.DynamicInvoke(object[] args) + 0xb bytes	
 	System.Windows.dll!System.Windows.Threading.DispatcherOperation.Invoke() + 0x2b bytes	
 	System.Windows.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority) + 0xcc bytes	
 	System.Windows.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context) + 0xa bytes	
 	System.Windows.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args) + 0x16 bytes	
 	System.Windows.Browser.dll!System.Windows.Hosting.ScriptingInterface.InvokeDelegate(System.Windows.Hosting.DelegateWrapper delegateWrapper, System.Windows.Hosting.NativeMethods.ScriptParam[] pParams, ref System.Windows.Hosting.NativeMethods.ScriptParam pResult = {System.Windows.Hosting.NativeMethods.ScriptParam}) + 0x28 bytes	
 	System.Windows.Browser.dll!System.Windows.Hosting.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam[] pParams, ref System.Windows.Hosting.NativeMethods.ScriptParam pResult) + 0xe3 bytes	
 	[Appdomain Transition]	
</object></object>

Nov 3, 2010 at 10:53 PM
Edited Nov 4, 2010 at 10:38 PM

Hi,

I tried to reproduce this issue using the code that you shared and found no issues. Are you using a third-party/custom UI Control exposed as a region?

This is certain strange that it only fails in your box. So, could yo please share a repro sample (application)?

Fernando Antivero
http://blogs.southworks.net/fantivero

 

Nov 4, 2010 at 2:16 PM

My guess is that the mscorlib is somehow corrupt.  I've downloaded the code to a third computer and it works, so it must be something related to my machine and its set up.

Nov 4, 2010 at 2:24 PM

Hi,

I am glad that you found a solution in your scenario. Let me know if you have any other question.

Fernando Antivero
http://blogs.southworks.net/fantivero