Hello am an trying to implement a global filter with injection. The filter looks like this.
public class WikiFilter : IActionFilter
{
private IWikiService service;
public WikiFilter(IWikiService service)
{
this.service = service;
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
!!!Code here!!
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
throw new NotImplementedException();
}
}
And i have attached the filter with injection the following way in my global.asax.
public class MvcApplication : System.Web.HttpApplication,
IAuthenticationApplication<User>
{
protected void Application_Start()
{
Ninject();
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
RegisterGlobalFilters(GlobalFilters.Filters);
}
private void Ninject()
{
// Create Ninject DI kernel
IKernel kernel = new StandardKernel();
kernel.Bind<DataContext>().ToSelf().InRequestScope();
kernel.Bind<IWikiRepository>().To<WikiRepository>();
kernel.Bind<IWikiService>().To<WikiService>();
// Global filters
kernel.BindFilter<WikiFilter>(FilterScope.Global, 0);
DependencyResolver.SetResolver
(new NinjectDependencyResolver(kernel));
}
}
But for some reason is the filter never fired when the application runs, have i not implemented it correctly?
I would recommend you using the
~/App_Start/NinjectMVC3.cs
file to configure the Ninject kernel:and the Global.asax stays unchanged. By the way that's the default setup when you install the Ninject.MVC3 NuGet package.