title: Unity Interception (AOP) slowing down MVC Editor Templates
body: <p>I have recently added Unity Interception to my MVC app, using the following in the bootstrap code:</p>
  
  <pre><code>    private static void RegisterProfilingHooks(IUnityContainer container)
      {
          var registrations =
              container.Registrations.Where(m =&gt; m.RegisteredType != m.MappedToType).Where(
                  m =&gt; m.RegisteredType.Name != "IProfilingService").Take(150).ToList();
  
          foreach (var registration in registrations)
          {
              container.AddNewExtension&lt;Interception&gt;().Configure&lt;Interception&gt;().SetInterceptorFor(
                  registration.RegisteredType, new InterfaceInterceptor());
          }
  
          container.Configure&lt;Interception&gt;()
              .AddPolicy("ProfilerPolicy")
              .AddMatchingRule&lt;NamespaceMatchingRule&gt;(
                  new InjectionConstructor(new InjectionParameter("MyApp.*")))
              .AddCallHandler&lt;ProfilerHandler&gt;(new ContainerControlledLifetimeManager());
      }
  </code></pre>
  
  <p>However, oddly I'm seeing my Editor Templates taking about 2 seconds (before, only milliseconds) to load, per editor template. So a form with 10 elements takes 10 seconds to load.</p>
  
  <p>Basically, this code is just to intercept a profiling handler on all IoC registrations, so that we can profile any methods invoked on these instances.</p>
  
  <p>The problem is not the profiler handler, because this happens even when I comment out the ProfilerPolicy configuration.</p>
  
  <p>It's the foreach loop and the extension wiring that's causing this:</p>
  
  <pre><code>foreach (var registration in registrations)
  {
      container.AddNewExtension&lt;Interception&gt;().Configure&lt;Interception&gt;().SetInterceptorFor(
          registration.RegisteredType, new InterfaceInterceptor());
  }
  </code></pre>
  
  <p>Why would this interception code have this bizarre effect on my Editor Templates?</p>
  
  <p><strong>Additional info:</strong></p>
  
  <p>I've ruled out anything in the templates themselves causing this, as I have updated them to be basic passthroughs, and still see the issue. For example, using this as a string template:</p>
  
  <pre><code>@model System.String
  @Html.TextBoxFor(m =&gt; m)
  </code></pre>
  
  <p>I still get the same timings.</p>
  
  <p>I don't know if somehow this interception is causing our view searching to drag, or what. That's the only thing left that I can think of at this point. Our view path definitions are pretty straight-forward:</p>
  
  <pre><code>AreaPartialViewLocationFormats = new[] {
      "~/Areas/{2}/Views/Sublayouts/{1}/{0}.cshtml",
      "~/Areas/{2}/Views/Renderings/{1}/{0}.cshtml",
      "~/Areas/Global/Views/Shared/{0}.cshtml"
  };
  </code></pre>