CreateRegion method infinite loop

Topics: Prism v2 - Silverlight 2
Feb 27, 2009 at 9:02 PM
The DelayedRegionCreationBehavior CreateRegion method gets called infinite times.

My application structure is a simples shell with

<ContentControl Regions:RegionManager.RegionName="MainRegion" />

Has anyone experienced this behaviour? Any suggestions on how I could troubleshoot this further?

Thank you,

Pavel



The call stack shows that it is gets called at least twice and fails on the second call.

> Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(System.Windows.DependencyObject targetElement = {System.Windows.Controls.ContentControl}, string regionName = "MainRegion") Line 143 + 0x79 bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.TryCreateRegion() Line 119 + 0xe bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.OnUpdatingRegions(object sender = null, System.EventArgs e = {System.EventArgs}) Line 100 + 0x8 bytes C#
  [Native to Managed Transition] 
  [Managed to Native Transition] 
  mscorlib.dll!System.Delegate.DynamicInvokeImpl(object[] args = {object[2]}) + 0xb4 bytes 
  mscorlib.dll!System.Delegate.DynamicInvoke(object[] args = {object[2]}) + 0x2a bytes 
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Events.WeakDelegatesManager.Raise(object[] args = {object[2]}) Line 49 + 0xe bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.RegionManager.UpdateRegions() Line 217 + 0x40 bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.RegionManager.RegionCollection.Add(Microsoft.Practices.Composite.Regions.IRegion region = {Microsoft.Practices.Composite.Presentation.Regions.SingleActiveRegion}) Line 306 + 0x5 bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.RegionManagerRegistrationBehavior.TryRegisterRegion() Line 127 + 0x23 bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.RegionManagerRegistrationBehavior.StartMonitoringRegionManager() Line 103 + 0x8 bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.RegionManagerRegistrationBehavior.OnAttach() Line 87 + 0x8 bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.RegionBehavior.Attach() Line 66 + 0x8 bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.RegionBehaviorCollection.Add(string key = "RegionManagerRegistration", Microsoft.Practices.Composite.Regions.IRegionBehavior regionBehavior = {Microsoft.Practices.Composite.Presentation.Regions.Behaviors.RegionManagerRegistrationBehavior}) Line 74 + 0x9 bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase<System.Windows.Controls.ContentControl>.AttachDefaultBehaviors(Microsoft.Practices.Composite.Regions.IRegion region = {Microsoft.Practices.Composite.Presentation.Regions.SingleActiveRegion}, System.Windows.Controls.ContentControl regionTarget = {System.Windows.Controls.ContentControl}) Line 112 + 0x1b bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase<System.Windows.Controls.ContentControl>.Initialize(System.Windows.Controls.ContentControl regionTarget = {System.Windows.Controls.ContentControl}, string regionName = "MainRegion") Line 66 + 0xe bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase<System.Windows.Controls.ContentControl>.Microsoft.Practices.Composite.Regions.IRegionAdapter.Initialize(object regionTarget = {System.Windows.Controls.ContentControl}, string regionName = "MainRegion") Line 82 + 0x5d bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(System.Windows.DependencyObject targetElement = {System.Windows.Controls.ContentControl}, string regionName = "MainRegion") Line 137 + 0xf bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.TryCreateRegion() Line 119 + 0xe bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.OnUpdatingRegions(object sender = null, System.EventArgs e = {System.EventArgs}) Line 100 + 0x8 bytes C#
  [Native to Managed Transition] 
  [Managed to Native Transition] 
  mscorlib.dll!System.Delegate.DynamicInvokeImpl(object[] args = {object[2]}) + 0xb4 bytes 
  mscorlib.dll!System.Delegate.DynamicInvoke(object[] args = {object[2]}) + 0x2a bytes 
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Events.WeakDelegatesManager.Raise(object[] args = {object[2]}) Line 49 + 0xe bytes C#
  Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.RegionManager.UpdateRegions() Line 217 + 0x40 bytes C#
  CoPallet.Infrastructure!CoPallet.Infrastructure.Ioc.NinjectBootstrapper.Run(bool useDefaultConfiguration = true) Line 88 + 0x5 bytes C#
  CoPallet.Infrastructure!CoPallet.Infrastructure.Ioc.NinjectBootstrapper.Run() Line 51 + 0xd bytes C#
  CoPallet.Shell!CoPallet.Shell.App.Application_Startup(object sender = {CoPallet.Shell.App}, System.Windows.StartupEventArgs e = {System.Windows.StartupEventArgs}) Line 32 + 0xa bytes C#
  System.Windows.dll!System.Windows.CoreInvokeHandler.InvokeEventHandler(int typeIndex = 167, System.Delegate handlerDelegate = {System.Windows.StartupEventHandler}, object sender = {CoPallet.Shell.App}, object args = {System.Windows.StartupEventArgs}) + 0x8c3 bytes 
  System.Windows.dll!MS.Internal.JoltHelper.FireEvent(System.IntPtr unmanagedObj = 144677232, System.IntPtr unmanagedObjArgs = 144698424, int argsTypeIndex = 167, string eventName = "M@1") + 0x336 bytes 
  [Appdomain Transition] 
  [Native to Managed Transition] 



The exceptions details.

Microsoft.Practices.Composite.Presentation.Regions.Behaviors.RegionCreationException was unhandled by user code Message="An exception occurred while creating a region with name 'MainRegion'. The exception was: System.InvalidOperationException: The Region property cannot be set after Attach method has been called.
at Microsoft.Practices.Composite.Presentation.Regions.RegionBehavior.set_Region(IRegion value)
at Microsoft.Practices.Composite.Presentation.Regions.RegionBehaviorCollection.Add(String keyIRegionBehavior regionBehavior)
at Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase`1.AttachDefaultBehaviors(IRegion regionT regionTarget)
at Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase`1.Initialize(T regionTargetString regionName)
at Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase`1.Microsoft.Practices.Composite.Regions.IRegionAdapter.Initialize(Object regionTargetString regionName)
at Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElementString regionName). " StackTrace: at Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElementString regionName) at Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.TryCreateRegion() at Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.OnUpdatingRegions(Object senderEventArgs e) InnerException: System.InvalidOperationException Message="The Region property cannot be set after Attach method has been called." StackTrace: at Microsoft.Practices.Composite.Presentation.Regions.RegionBehavior.set_Region(IRegion value) at Microsoft.Practices.Composite.Presentation.Regions.RegionBehaviorCollection.Add(String keyIRegionBehavior regionBehavior) at Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase`1.AttachDefaultBehaviors(IRegion regionT regionTarget) at Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase`1.Initialize(T regionTargetString regionName) at Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterBase`1.Microsoft.Practices.Composite.Regions.IRegionAdapter.Initialize(Object regionTargetString regionName) at Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElementString regionName) InnerException:

Mar 4, 2009 at 3:56 PM
Obviously it shouldn't do that. Can you send me a repro? erwinvd (at) microsoft dot com. I can have a look at it and see what's wrong.

Thanks,
Erwin
Jun 12, 2009 at 1:02 AM

holbil,

 

I have seen this to, actually recently, while trying to work with Prism and a custom container. I had to do my own bootstrapping, which lead me to having to register types that Prism services needed. As far as you encounter, I ran into this same issue in that the RegionManagerRegistrationBehavior calls Add on the IRegionManager. The first line in the RegionManager implementation of Add is UpdateRegions(); this cirlcles back to TryCreateRegion. My solution was to create a CustomRegionManagerRegistrationBehavior and in its constructor I inject IRegionManager. I assign this value to my private member _manager and use this value when FindRegionManager is called.

I do not know if this the exact solutions to your issue. I had a lot of things going on, but it did resolve my issue of observing orphaned regions.

Hope this helps.

 

Andres Olivares

Oct 6, 2009 at 7:59 AM

I had the same problem. I am using Autofac instead of Unity and created my own bootstrapper just like Andres did.

A solution other than yours that might be a bit ugly is to set this.regionCreated = true before calling CreateRegion. That way the next time TryCreateRegion is called it never calls CreateRegion.

It would be nice with some a proper fix for this so we who use own containers don't have to do workarounds like this.

-Linus

Mar 9, 2010 at 7:04 PM

I have to say, this is a lot more difficult than is necessary to utilize my own Container.  I am trying to create a WindsorContainer and I am have a tremendous amount of difficulty.  I know that I am not adding much to this thread, just venting...