I am writing my own ErrorHandler attribute for my MVC project. I would like to add an implementation of EventViewerLogger to this attribute.
I use Ninject 2.2, and it works great for other functions, such as repository repositories and aggregated services through controller constructors.
I understand that I cannot embed an implementation of any class in an attribute through a constructor, so I have to enter it in the attribute property.
The interface is below:
namespace Foo.WebUI.Infrastructure { public interface ILogger { void Log(Exception e); } }
Implementing Event Viewer Log
namespace Foo.WebUI.Infrastructure {
Below is the code for the error handler:
namespace Foo.WebUI.Handlers {
In global.asax I register the handler and Ninject.
protected void Application_Start() { IKernel kernel = new StandardKernel(new NinjectInfrastructureModule()); }
Finally, I have a custom filter provider
namespace Foo.WebUI.Infrastructure { public class NinjectFilterProvider: FilterAttributeFilterProvider { private readonly IKernel kernel; public NinjectFilterProvider(IKernel kernel) { this.kernel = kernel; } public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) { var filters = base.GetFilters(controllerContext, actionDescriptor);
When I run the application, I get the following exception:
Activation path: 2) Injection of the ILogger dependency into the Logger property of type CustomHandleErrorAttribute 1) Request for CustomHandleErrorAttribute
Suggestions: 1) Make sure that the implementation type has an open constructor. 2) If you have implemented the Singleton template, use the binding with InSingletonScope ().
Source Error: Line 27: foreach (var filter in filters) Line 28: { Line 29: kernel.Inject(filter.Instance); Line 30: }
Spent a day on this, learned a lot about dependecy injections, which is great, but what am I doing wrong here?