So I am using a simple repository pattern with Attributes and Filters as recommended here since I am using the Ninject.Web.WebApi-RC
package from NuGet.
This works for the first request but since I have my DbContext
in request scope, it is disposed on all subsequent requests.
Here is my attribute:
public class CommunicationKeyValidationAttribute : FilterAttribute
{
}
Here is my filter:
public class CommunicationKeyValidationFilter : AbstractActionFilter
{
public CommunicationKeyValidationFilter(IRepository repository)
{
this.repository = repository;
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
// do stuff
}
}
Here is my repository:
public class Repository : IRepository
{
public Repository(MyDbContext dbContext)
{
this.dbContext = dbContext;
}
}
Here are my Ninject bindings:
this.Kernel.Bind<MyDbContext>().ToSelf().InRequestScope();
this.Kernel.Bind<IRepository>().To<Repository>().InRequestScope();
this.Kernel.BindHttpFilter<CommunicationKeyValidationFilter>(FilterScope.Action)
.WhenActionMethodHas<CommunicationKeyValidationAttribute>()
.InRequestScope();
My controller looks like this:
public class HomeController
{
[CommunicationKeyValidation]
public ActionResult Index()
{
// do stuff
}
The issue here is that the constructor on CommunicationKeyValidationFilter
is only being called on the first request. Is there a way that I can cause ninject to construct this filter each time it attempts to resolve the filter?
DefaultFilterProvider
andNinjectFilterProvider
for Web.API... – AtomizerApiControllerActionSelector
which caches theReflectedHttpActionDescriptor
s which caches the Filters in the_filterPipeline
... – AtomizerCommunicationKeyValidationFilter
but resolve your services in theOnActionExecuting
method:public override void OnActionExecuting(HttpActionContext actionContext) { ((IRepository) actionContext.Request.GetDependencyScope().GetService(typeof(IRepository))).Do(); }
– AtomizerDefaultFilterProvider
... I thought about using a workaround like that but I was hoping there was a way to do this correctly but it looks likeApiControllerActionSelector
is pretty locked down (if that's where the filters are cached). aspnetwebstack.codeplex.com/workitem/277 – Jennajenne