Create region objects

Topics: Prism v4 - WPF 4
Oct 26, 2010 at 1:01 AM

I have been building a small sample app using the code from WPF StockTraderRI from Prism v4 - Drop 10 as a guide.

I have added the bootstrapper and one module which I have referenced in my shell. bootstrapper.Run() is throwing an exception.

I believe it has something to do with AutoPopulateExportedViewsBehavior, but I do not understand enough about AddIfMissing() to determine if that is the exact problem. If I comment out the AddIfMissing() statement I get my shell without the module. I have checked and recheck to make sure that I have the appropriate attributes and that the name of the region matches the ViewExport[].

Does anyone know of what might be the issue or where I should start looking? I can post some of the source if needed or send the entire project.

An exception occurred while trying to create region objects.
    - The most likely causing exception was: 'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Practices.Prism.Regions.Behaviors.RegionCreationException: An exception occurred while creating a region with name 'MainRegion'. The exception was: Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type AutoPopulateExportedViewsBehavior, key "" ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type AutoPopulateExportedViewsBehavior, key ""
   at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key)
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType)
   at Microsoft.Practices.Prism.Regions.RegionBehaviorFactory.CreateFromKey(String key)
   at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.AttachDefaultBehaviors(IRegion region, T regionTarget)
   at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.Initialize(T regionTarget, String regionName)
   at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.Microsoft.Practices.Prism.Regions.IRegionAdapter.Initialize(Object regionTarget, String regionName)
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName).  ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type AutoPopulateExportedViewsBehavior, key "" ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type AutoPopulateExportedViewsBehavior, key ""
   at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key)
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType)
   at Microsoft.Practices.Prism.Regions.RegionBehaviorFactory.CreateFromKey(String key)
   at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.AttachDefaultBehaviors(IRegion region, T regionTarget)
   at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.Initialize(T regionTarget, String regionName)
   at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.Microsoft.Practices.Prism.Regions.IRegionAdapter.Initialize(Object regionTarget, String regionName)
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName)
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.TryCreateRegion()
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.OnUpdatingRegions(Object sender, EventArgs e)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at Microsoft.Practices.Prism.Events.WeakDelegatesManager.Raise(Object[] args)
   at Microsoft.Practices.Prism.Regions.RegionManager.UpdateRegions()'.
    But also check the InnerExceptions for more detail or call .GetRootException().

Oct 26, 2010 at 11:22 PM

I figured it out after stepping through the prism source.

DoGetInstance(Type serviceType, string key) was actually throwing the error.
"AutoPopulateExportedViewsBehavior" was not in compositionContainer.

This was because I never added the assembly to AggregateCatalog!

Oct 27, 2010 at 12:03 AM

I'm having the exact same issue. I'm following the StockTraderRI (Silverlight) Code.  Your post just saved me :)  I've been working on this for hours now. 

What's not obvious in the StockTraderRIBootstrapperCode is that in the ConfigureAggregateCatalog() Method this line of Code is called right after the Bootstrapper is loaded into the Catalog, which basically loads the entire Infrastructure Library, which is where the AutoPopulateExportedViewBehaviors Class is located.  It just hit me when you said the Assembly was never added to the AggregateCatalog. I went back to my code, and I did not have this line of code.

// This loads the Infrastructure Assembly

this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StockTraderRICommands).Assembly));

 

Thanks a BUNCH!

Devaron Ruggiero

Feb 24, 2011 at 12:31 PM

I also received the SystemActivationException.  My case was a little different as I was trying to manually wire the view to a region in my module-init using MEF instead of using the AutoPopulation behavior class provided in the stock trader example.

I was trying to put together the most basic example I could think of after reading through some of this.  My Shell has a ContentControl with a region named "Main".  I created a module DLL and registered it in my App.config file to try one of the dynamic loading capabilities of MEF.  

My Module DLL simply has a Module class and a WPF UserControl that I wanted to target the "Main" region with.  

This is my Module code:

    [ModuleExport(typeof(FirstModule))]
    public class FirstModule :  IModule
    {
        [Import]
        public IRegionManager RegionManager
        {
            get;
            set;
        }

        #region IModule Members

        public void Initialize()
        {
            this.RegionManager.RegisterViewWithRegion(RegionNames.Main, typeof(UserControl1));
        }

        #endregion
    }

 

My View was just a standard WPF user-control.  When I tried running this code I would get SystemActivationException because the MEF container couldn't resolve UserControl1.  I figured that maybe I forgot to mark the control to be Exported and sure enough that was the case.  So I changed my UserControl to look like this and it worked.

    [Export(typeof(UserControl1))]
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    }