Reputation: 825
I want to inject a custom ViewEngine into my MVC website. This is what I do:
private static IKernel CreateKernel()
{
kernel.Bind<IViewEngine>().ToProvider(new RazorViewEngineProvider()).InRequestScope();
}
This is my provider:
public class RazorViewEngineProvider : Provider<RazorViewEngine>
{
protected override RazorViewEngine CreateInstance(IContext context)
{
return new RazorViewEngine();
}
}
Problem is: My provider is only called once when I go to the website for the first time. The next time my provider is somehow still in cache. And that's not what I want.
I want the provider to execute on every request. I thought I could do that with .InRequestScope(), but that doesn't make any difference. Does anyone know what's going on?
Upvotes: 1
Views: 217
Reputation: 507
The view engine isn't cached by Ninject in this case. It's MVC itsself that does not request it every time from the IDependencyResolver. But I think this is the correct behavior. That way it keeps the overhead to create the view engine at a minimum by reusing it.
You shouldn't have a request dependent dependency in your view engine. This kind of dependency has to be retrieved from the view model and has to be assigned by the controller.
And you should remove InRequestScope. Otherwise it will disposed by Ninject after the first request but MVC will still try to reuse it.
Upvotes: 1
Reputation: 29186
Rather then injecting the custom view engine, could you try using Application_Start()
instead:
protected void Application_Start()
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngineProvider());
RegisterRoutes(RouteTable.Routes);
}
What happens when you do the registration this way? Does it work?
Upvotes: 0