title: UnityConfig.Resolve component failed when unity interception is used
body: <p>I have problem with resolving component with Unity in my .NET MVC 5 app.
  In my App_Start folder I have generated UnityConfig.cs and UnityMvcActivator.cs, which looks like this:</p>
  
  <pre><code>public static class UnityConfig
  {
      #region Unity Container
      private static Lazy&lt;IUnityContainer&gt; container =
        new Lazy&lt;IUnityContainer&gt;(() =&gt;
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            return container;
        });
      public static IUnityContainer Container =&gt; container.Value;
      #endregion
  
      public static void RegisterTypes(IUnityContainer container)
      {
          // NOTE: To load from web.config uncomment the line below.
          // Make sure to add a Unity.Configuration to the using statements.
          // container.LoadConfiguration();
  
          container.AddNewExtension&lt;Interception&gt;();
  
          container.RegisterType&lt;ILogger, EntLibFileLogger&gt;(new ContainerControlledLifetimeManager(),
              new InjectionConstructor(ConfigurationManager.AppSettings["logTrace"],
                  int.Parse(ConfigurationManager.AppSettings["logSize"])));
          container.RegisterType&lt;IUserRepository, UserRepository&gt;(new Interceptor&lt;InterfaceInterceptor&gt;(), new InterceptionBehavior&lt;LoggingInterceptionBehavior&gt;());
          container.RegisterType&lt;IUserProvider, UserProvider&gt;(new Interceptor&lt;InterfaceInterceptor&gt;(), new InterceptionBehavior&lt;LoggingInterceptionBehavior&gt;());
          ...
      }
  }
  
  public static class UnityMvcActivator
  {
      /// &lt;summary&gt;
      /// Integrates Unity when the application starts.
      /// &lt;/summary&gt;
      public static void Start() 
      {
          FilterProviders.Providers.Remove(FilterProviders.Providers.OfType&lt;FilterAttributeFilterProvider&gt;().First());
          FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(UnityConfig.Container));
  
          DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfig.Container));
  
          // TODO: Uncomment if you want to use PerRequestLifetimeManager
          // Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
      }
  
      /// &lt;summary&gt;
      /// Disposes the Unity container when the application is shut down.
      /// &lt;/summary&gt;
      public static void Shutdown()
      {
          UnityConfig.Container.Dispose();
      }
  }
  </code></pre>
  
  <p>Interception look like this:</p>
  
  <pre><code>class LoggingInterceptionBehavior : IInterceptionBehavior
  {
      private readonly ILogger _logger;
  
      public LoggingInterceptionBehavior(ILogger logger)
      {
          _logger = logger;
      }
  
      public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
      {
          var methodDesc = $"{input.MethodBase.DeclaringType.Name}.{input.MethodBase.Name}";
          var args = input.Arguments == null || input.Arguments.Count == 0
              ? "null"
              : JsonConvert.SerializeObject(input.Arguments);
          // Before invoking the method on the original target.
          _logger.Log($"{methodDesc} start. Arguments: {args}", LogLevel.Info);
  
          // Invoke the next behavior in the chain.
          var result = getNext()(input, getNext);
  
          // After invoking the method on the original target.
          if (result.Exception != null)
          {
              _logger.Log($"{methodDesc} threw exception. {result.Exception.GetBaseException()}", LogLevel.Error);
          }
          else
          {
              _logger.Log($"{methodDesc} end", LogLevel.Info);
          }
  
          return result;
      }
  
      public IEnumerable&lt;Type&gt; GetRequiredInterfaces()
      {
          return Type.EmptyTypes;
      }
  
      public bool WillExecute
      {
          get { return true; }
      }
  }
  </code></pre>
  
  <p>I have also customized authorization filter, where is following code: </p>
  
  <pre><code>public class SayDoAuthorizeAttribute : AuthorizeAttribute
  {
      private readonly string[] allowedRoles;
  
      public SayDoAuthorizeAttribute() : base()
      {
      }
  
      public SayDoAuthorizeAttribute(params string[] roles)
      {
          allowedRoles = roles;
      }
  
      protected override bool AuthorizeCore(HttpContextBase httpContext)
      {
          var userRoles = SayDoAuthorizeHelper.GetRolesAndCacheThem(httpContext);
          bool authorize = false;
          foreach (var role in allowedRoles)
          {
              if (userRoles.Contains(role))
              {
                  authorize = true;
                  break;
              }
          }
          return authorize;
      }
  
      ...
  }
  </code></pre>
  
  <p>The problem is that UnityConfig.Resolve fails on resolving IUserProvider component in following SayDoAuthorizeHelper.GetRolesAndCacheThem method. (I want to have this class static, because IsUserInRole and IsUserInRoles mehthods are also used in razor views.</p>
  
  <pre><code>public static class SayDoAuthorizeHelper
  {
      public static IEnumerable&lt;string&gt; GetRolesAndCacheThem(HttpContextBase httpContext)
      {
          var nameFromSession = httpContext.Session[Constants.SessionKeyUserPreselectSession];
          var userName = (nameFromSession != null) ? (string)nameFromSession : httpContext.User.Identity.Name;
  
          var rolesString = (string)httpContext.Cache.Get(userName);
          if (string.IsNullOrEmpty(rolesString))
          {
              var userProvider = UnityConfig.Container.Resolve&lt;IUserProvider&gt;();
              var roles = userProvider.GetUserRolesNames(userName);
              rolesString = string.Join(",", roles);
              //set absolute cache
              var minutesToCacheRoles = double.Parse(ConfigurationManager.AppSettings[Constants.KeyCacheUserRolesInMinutes]);
              httpContext.Cache.Insert(userName, rolesString, null, DateTime.Now.AddMinutes(minutesToCacheRoles), Cache.NoSlidingExpiration);
              return roles;
          }
          return rolesString.Split(',').ToList();
      }
  
      public static bool IsUserInRole(System.Security.Principal.IPrincipal userPrincipal, string roleName)
      {
          var userRoles = GetRolesAndCacheThem(new HttpContextWrapper(HttpContext.Current));
          return userRoles.Contains(roleName);
      }
  
      public static bool IsUserInRoles(System.Security.Principal.IPrincipal userPrincipal, string[] roleNames)
      {
          var userRoles = GetRolesAndCacheThem(new HttpContextWrapper(HttpContext.Current));
          foreach (var roleName in roleNames)
          {
              if (userRoles.Contains(roleName))
              {
                  return true;
              }
          }
          return false;
      }
  }
  </code></pre>
  
  <p>When I have breakpoint set on line 
  var userProvider = UnityConfig.Container.Resolve();
  then no exception is thrown, and code still continue, but I see in local variables that userProvider threw an exception of type 'System.IO.FileNotFoundException'   Abb.Czopc.SayDo.BusinessLogic.Interface.IUserProvider {System.IO.FileNotFoundException}
  with message "Cannot load assembly '15bd37ce9f8a41d9b9916a1ebadc536a'.". But in output window I see that assembly was loaded.
  'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131699797337577440): Loaded '15bd37ce9f8a41d9b9916a1ebadc536a'.</p>
  
  <p>When I have registration in UnityConfig without Interception, everything works good.</p>
  
  <pre><code>    container.RegisterType&lt;IUserRepository, UserRepository&gt;();
      container.RegisterType&lt;IUserProvider, UserProvider&gt;();
  </code></pre>
  
  <p>I also tried replace InterfaceInterceptor with VirtualMethodInterceptor, but with same result.
  Can anyone help? I dont know what is wrong. Thanks for help.
  Solution is written in VS 2015, list of nuget packages is here:</p>
  
  <pre><code>  &lt;package id="Abb.Czopc.Common.Log" version="1.0.1" targetFramework="net451" /&gt;
    &lt;package id="Abb.Czopc.Common.Log.EntLibLogger" version="1.0.1" targetFramework="net451" /&gt;
    &lt;package id="Antlr" version="3.5.0.2" targetFramework="net451" /&gt;
    &lt;package id="AutoMapper" version="6.2.2" targetFramework="net451" /&gt;
    &lt;package id="EnterpriseLibrary.Common" version="6.0.1304.0" targetFramework="net451" /&gt;
    &lt;package id="EnterpriseLibrary.Logging" version="6.0.1304.0" targetFramework="net451" /&gt;
    &lt;package id="jQuery" version="3.3.1" targetFramework="net451" /&gt;
    &lt;package id="jQuery.Validation" version="1.17.0" targetFramework="net451" /&gt;
    &lt;package id="Microsoft.AspNet.Mvc" version="5.2.4" targetFramework="net451" /&gt;
    &lt;package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net451" /&gt;
    &lt;package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" /&gt;
    &lt;package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net451" /&gt;
    &lt;package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.8" targetFramework="net451" /&gt;
    &lt;package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.5" targetFramework="net451" /&gt;
    &lt;package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.9" targetFramework="net451" /&gt;
    &lt;package id="Microsoft.Net.Compilers" version="2.7.0" targetFramework="net451" developmentDependency="true" /&gt;
    &lt;package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" /&gt;
    &lt;package id="Modernizr" version="2.8.3" targetFramework="net451" /&gt;
    &lt;package id="Newtonsoft.Json" version="11.0.2" targetFramework="net451" /&gt;
    &lt;package id="Respond" version="1.4.2" targetFramework="net451" /&gt;
    &lt;package id="Unity.Abstractions" version="3.3.0" targetFramework="net451" /&gt;
    &lt;package id="Unity.Container" version="5.8.5" targetFramework="net451" /&gt;
    &lt;package id="Unity.Interception" version="5.5.1" targetFramework="net451" /&gt;
    &lt;package id="Unity.Mvc" version="5.0.13" targetFramework="net451" /&gt;
    &lt;package id="WebActivatorEx" version="2.2.0" targetFramework="net451" /&gt;
    &lt;package id="WebGrease" version="1.6.0" targetFramework="net451" /&gt;
  </code></pre>