title: Caliburn.Micro Navigation causing StackOverflowException
body: <p>I'm trying to set up navigation in an MVVM app using Caliburn.Micro and its <code>SimpleContainer</code> class. I need to inject my <code>ShellViewModel</code> into the <code>Navigator</code> for it to access the <code>ActivateItem()</code> method. When the shell view first loads it needs to activate the first child view (<code>CourseListViewModel</code>). To perform this initial navigation I'm trying to inject the <code>Navigator</code> into my <code>ShellViewModel</code>. The problem is this causes a <code>StackOverflowException</code> because the <code>ShellViewModel</code> constructor requires a <code>Navigator</code> which requires a <code>ShellViewModel</code> and so on.</p>
  <p>When I configured the <code>SimpleContainer</code> I registered the <code>ShellViewModel</code> using the <code>Singleton()</code> method. My understanding was that the container would then create an instance of the <code>ShellViewModel</code> the first time it's requested, and then return the same instance for any subsequent requests despite the fact that my <code>ShellViewModel</code> doesn't actually implement the Singleton pattern. Because of this, I only expected the <code>ShellViewModel</code> constructor to be called once, but this clearly isn't what's happening.</p>
  <p><strong>Bootstrapper</strong></p>
  <pre><code>class Bootstrapper : BootstrapperBase
  {
      public Bootstrapper()
      {
          Initialize();
      }
  
      protected override void OnStartup(object sender, StartupEventArgs e)
      {
          DisplayRootViewFor&lt;IShell&gt;();
      }
  
      private readonly SimpleContainer _container = new SimpleContainer();
  
      protected override void Configure()
      {
          _container.Instance(_container);
          
          _container.Singleton&lt;IEventAggregator, EventAggregator&gt;()
                      .Singleton&lt;IWindowManager, WindowManager&gt;()
                      .Singleton&lt;INavigator, Navigator&gt;() // &lt;&lt; Navigator is registered as a Singleton
                      .Singleton&lt;IShell, ShellViewModel&gt;() // &lt;&lt; ShellViewModel is registered as a Singleton
                      .Singleton&lt;Models.GoogleClassroom&gt;();
  
          // Register all ViewModels
          GetType().Assembly.GetTypes()
              .Where(type =&gt; type.IsClass)
              .Where(type =&gt; type.Name.EndsWith(&quot;ViewModel&quot;))
              .Where(type =&gt; type.Name != &quot;ShellViewModel&quot;) // Avoid registering ShellViewModel as a PerRequest instance
              .ToList()
              .ForEach(type =&gt; _container.RegisterPerRequest(type, null, type));
  
      }
  
      protected override object GetInstance(Type service, string key)
      {
          return _container.GetInstance(service, key);
      }
  
      protected override IEnumerable&lt;object&gt; GetAllInstances(Type service)
      {
          return _container.GetAllInstances(service);
      }
  
      protected override void BuildUp(object instance)
      {
          _container.BuildUp(instance);
      }
  }
  </code></pre>
  <p><strong>Navigator</strong></p>
  <pre><code>public class Navigator : INavigator
  {
      private readonly IShell _shell;
      private readonly IWindowManager _windowManager;
  
      public Navigator(IShell shell, IWindowManager windowManager)
      {
          _shell = shell;
          _windowManager = windowManager;
      }
  
      // I can provide the rest of the methods if necessary but the exception occurs in the
  
  }
  </code></pre>
  <p><strong>ShellViewModel</strong></p>
  <pre><code>public interface IShell : IConductActiveItem { }
  
  public class ShellViewModel : Conductor&lt;object&gt;.Collection.OneActive, IHandle&lt;object&gt;, IShell
  {
  
      private readonly IEventAggregator _eventAggregator;
      private readonly IWindowManager _windowManager;
      private readonly INavigator _navigator;
  
      public ShellViewModel(IWindowManager windowManager, IEventAggregator eventAggregator, INavigator navigator)
      {
          _windowManager = windowManager;
          _eventAggregator = eventAggregator;
          _eventAggregator.Subscribe(this);
          _navigator = navigator;
  
          // Activate the initial child view
          _navigator.To&lt;CourseListViewModel&gt;().Go();
  
      }
  }
  </code></pre>
  <p>I stepped through the application and found that the <code>StackOverflowException</code> occurred when the <code>protected override object GetInstance()</code> method tried to get an instance of type <code>IShell</code>, but because of the way I believed the <code>_container.Singleton()</code> registration worked it took me a little while to figure out what was happening. While trying to debug it I removed the <code>Navigator</code> from the <code>ShellViewModel</code> constructor and injected the conductor itself instead. Then I tried to activate the initial child view as below:</p>
  <pre><code>public ShellViewModel(IWindowManager windowManager, IEventAggregator eventAggregator, SimpleContainer container) // INavigator navigator)
  {
      // Code shortened, but same as above
      
      // GetInstance&lt;IShell&gt;() calls this constructor recursively
      container.GetInstance&lt;IShell&gt;().ActivateItem(container.GetInstance&lt;CourseListViewModel&gt;());
  
  }
  </code></pre>
  <p>As you can see in the comments, this is where I spotted the problem. The <code>ShellViewModel</code> constructor is being called recursively instead of the conductor injecting an existing object (Singleton) as I expected.</p>
  <p>My question is have I misunderstood what <code>SimpleContainer.Singleton()</code> does or have I used it incorrectly? and what is the correct way to implement this to resolve my problem with the initial navigation and still inject the dependencies to it?</p>