title: Asp.Net MVC 5 Custom Action Filter With StructureMap
body: <p>i am facing issue in asp.net mvc custom acitonfilte using structuremap
  in my "LogAttribute" class i have setter dependency injection which is coming null when executing the "OnActionExecuted" Method of my customfilterclass which is "LogAttribute"</p>
  
  <p>my LogAttribute Class code is </p>
  
  <pre><code>    public class LogAttribute : ActionFilterAttribute
  {
      public ApplicationDbContext Context { get; set; }
      public string Description { get; set; }
      public LogAttribute(string description)
      {
          Description = description;
      }
  
      public override void OnActionExecuted(ActionExecutedContext filterContext)
      {
          var userId = filterContext.HttpContext.User.Identity.GetUserId();
          var user = Context.Users.Find(userId); **i am getting error here the Context is coming null here** 
          Context.Logs.Add(new Log(user, filterContext.ActionDescriptor.ActionName,
                                  filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                                  Description
                                  )
                          );
          Context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>i creat another class from wheren i am passing value to the setter dependency property </p>
  
  <pre><code>    public class StructureMapFilterProvider : FilterAttributeFilterProvider 
  {
      private readonly Func&lt;IContainer&gt; _container;
      public StructureMapFilterProvider(Func&lt;IContainer&gt; container)
      {
          _container = container;
      }
  
      public override IEnumerable&lt;Filter&gt; GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
      {
          var filters = base.GetFilters(controllerContext, actionDescriptor);
          var container = _container();
          foreach (var filter in filters)
          {
              container.BuildUp(filter.Instance);
              yield return filter;
          }
      }
  }
  </code></pre>
  
  <p>my dependency resolver class code is </p>
  
  <pre><code>public class StructureMapDependencyResolver : IDependencyResolver
  {
      private readonly Func&lt;IContainer&gt; _containerFactory;
      public StructureMapDependencyResolver(Func&lt;IContainer&gt; containerFactory)
      {
          _containerFactory = containerFactory;
      }
      public object GetService(Type serviceType)
      {
          if (serviceType == null)
          {
              return null;
          }
          var container = _containerFactory();
  
          return serviceType.IsAbstract || serviceType.IsInterface
              ? container.TryGetInstance(serviceType)
              : container.GetInstance(serviceType);
      }
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          return _containerFactory().GetAllInstances(serviceType).Cast&lt;object&gt;();
      }
  }
  </code></pre>
  
  <p>and my global.ascx code is</p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {
  
      public IContainer Container
      {
          get
          {
              return (IContainer)HttpContext.Current.Items["_Container"];
          }
          set
          {
              HttpContext.Current.Items["_Container"] = value;
          }
      }
  
      protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
          DependencyResolver.SetResolver(new StructureMapDependencyResolver(() =&gt; Container ?? ObjectFactory.Container));
  
          ObjectFactory.Configure(cfg =&gt;
             {
                 cfg.Scan(Scan =&gt;
                 {
                     Scan.TheCallingAssembly();
                     Scan.WithDefaultConventions();
                     Scan.With(new ControllerConfiguration());
                 });
                 cfg.For&lt;IFilterProvider&gt;().Use(new StructureMapFilterProvider(() =&gt; Container ?? ObjectFactory.Container));
                 cfg.For&lt;IUserStore&lt;ApplicationUser&gt;&gt;()
                    .Use&lt;UserStore&lt;ApplicationUser&gt;&gt;();
                 cfg.For&lt;DbContext&gt;()
                    .Use(() =&gt; new ApplicationDbContext());
                 cfg.SetAllProperties(x =&gt;
                  x.Matching(p =&gt;
                       p.DeclaringType.CanBeCastTo(typeof(ActionFilterAttribute)) &amp;&amp;
                       p.DeclaringType.Namespace.StartsWith("TestingSturctureMap") &amp;&amp;
                       p.PropertyType.IsPrimitive &amp;&amp;
                       p.PropertyType != typeof(string)));
  
             });
      }
  
      public void Application_BeginRequest()
      {
          Container = ObjectFactory.Container.GetNestedContainer();
      }
  
      public void Application_EndRequest()
      {
          Container.Dispose();
          Container = null;
      }
  }
  </code></pre>