RegisterViewWithRegion Confusing Exception

Topics: Prism v4 - WPF 4
Jan 31, 2011 at 9:07 PM
I'm having trouble understanding what exactly this exception is telling me.  The project has two modules which both use regions.  
They both load usercontrols into a single region called NavigationRegion.  NavigationRegion is defined in the Shell view xaml file as so:
<ItemsControl x:Name="NavigationRegion" 
HorizontalContentAlignment="Stretch" />

Whenever the module initializes, I call the following line of code in my project:


I get this exception immediately:

An exception occurred while initializing module 'Module'.     - The exception message was: An exception has occurred while trying to add a view to region 'NavigationRegion'.     - The most likely causing exception was was: 'System.InvalidOperationException: Sequence contains more than one element   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)   at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key) in c:\release\WorkingDir\PrismLibraryBuild\PrismLibrary\Desktop\Prism.MefExtensions\MefServiceLocatorAdapter.cs:line 73   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)'.


In general, what is causing this exception?




Feb 1, 2011 at 3:19 AM

>>Sequence contains more than one element

Any chance you have more than one class exporting typeof(NavigationControlItemView)?

Feb 1, 2011 at 2:20 PM

I don't think I do.  I don't add that type to the catalog, and I only export it in the class itself.


Ahh!  Problem solved!  I registered the assembly twice in the MEF catalog in my shell:

                new AssemblyCatalog(typeof(SomeClass1).Assembly));
               new AssemblyCatalog(typeof(SomeClass2).Assembly));

Didn't notice the .Assembly part, so it was completely hosed when it saw two exports.