title: Unity IoC: "Operation could destabilize the runtime"
body: <p>Is it possible to instantiate a type, configured in UnityContainer, within the constructor of another type configured in UnityContainer? With my current solution, I'm getting a  </p>
  
  <blockquote>
    <p>ResolutionFailedException:<br>
    Resolution of the dependency failed, type = "Sample.IMyProcessor", name = "(none)".<br>
    Exception occurred while: while resolving.<br>
    Exception is: VerificationException - Operation could destabilize the runtime.</p>
  </blockquote>
  
  <p>The Problem is that my second class (<em>FileLoader</em>) has an argument which should be evaluated in the first constructor: </p>
  
  <p>The Constructor of the <strong>MyProcessor</strong> class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyProcessor : IMyProcessor
  {
      private readonly IFileLoader loader;
      private readonly IRepository repository;
  
      public MyProcessor(IRepository repository, string environment, Func&lt;SysConfig, IFileLoader&gt; loaderFactory)
      {
          this.repository = repository;
          SysConfig config = repository.GetConfig(environment);
  
          loader = loaderFactory(config);
      }
  
      public void DoWork()
      {
          loader.Process();
      }
  }
  </code></pre>
  
  <p>And here the Main function with the UnityContainer config:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static void Run()
  {
      var unityContainer = new UnityContainer()
      .RegisterType&lt;IRepository, MyRepository&gt;()
      .RegisterType&lt;IFileLoader, FileLoader&gt;()
      .RegisterType&lt;IMyProcessor, MyProcessor&gt;(new InjectionConstructor(typeof(IRepository), "DEV", typeof(Func&lt;SysConfig, IFileLoader&gt;)));
  
      //Tests
      var x = unityContainer.Resolve&lt;IRepository&gt;(); //--&gt; OK
      var y = unityContainer.Resolve&lt;IFileLoader&gt;(); //--&gt; OK
  
      var processor = unityContainer.Resolve&lt;IMyProcessor&gt;();
      //--&gt; ResolutionFailedException: "Operation could destabilize the runtime."
  
      processor.DoWork();
  }
  </code></pre>
  
  <p>And the <strong>FileLoader</strong> class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class FileLoader : IFileLoader
  {
      private readonly SysConfig sysConfig;
  
      public FileLoader(SysConfig sysConfig, IRepository repository)
      {
          this.sysConfig = sysConfig;
      }
  
      public void Process()
      {
          //some sample implementation
          if (sysConfig.IsProduction)
              Console.WriteLine("Production Environement");
          else
              Console.WriteLine("Test Environment");
      }
  }
  </code></pre>
  
  <p>I assume the Problem is related to the <em>Func</em> which is passed to the MyProcessor class. Is there another way to pass the <em>loaderFactory</em> to the <em>MyProcessor</em> class?  </p>
  
  <p>Thanks!</p>