Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ninject to Bind WebApi Filter Attributes

I'm currently using ninject.mvc3 in my asp.net webapi project, and it works fine. I am now trying to bind filter attributes, but BindFilter and BindHttpFilter are missing from IKernel interface when using the NinjectWebCommon static class method. If I add the Ninject.Web.WebApi package, its there but the project does not run (problem here, solution said that Ninject.Web.WebApi is deprecated) so I removed it back.

I also followed this github wiki exactly and still this.BindFilter was not found. I have the latest nuget packages; Ninject (3.0.1.10), Ninject.Web.Common (3.0.0.7), Ninject.MVC3 (3.0.0.6)

Where Is BindFilter or BindHttpFilter? Did something change? How do I bind filter attributes in asp.net web api? There are so many libraries and projects and the docs don't tell you what is the current working one and what is deprecated....

like image 824
Shawn Mclean Avatar asked Oct 06 '12 06:10

Shawn Mclean


2 Answers

The packages you are using are correct by they don't include BindHttpFilter yet. BindHttpFilter extension method is from https://github.com/remogloor/Ninject.Web.WebApi which is currently not working for the release version of WebApi as it was written against the beta WebApi version. In order to inject into a WebApi filter you need BindHttpFilter, you can't pass a WebApi filter into the BindFilter method.

This is https://github.com/ninject/Ninject.Web.WebApi/pull/1 seems to be the latest info I could find on the status of an update.

You could also try another NuGet package http://nuget.org/packages/Ninject.Web.WebApi-rc but I had some errors with it, don't remember now exactly what errors. Anyway after reading the discussion on the above mentioned pull request I gave up on this package.

In my case I am trying to implement a logging filter so I am just going to create a static method that I can call from the filter till injection support will be available.

like image 121
IMLiviu Avatar answered Nov 03 '22 01:11

IMLiviu


I Have same problem. 1) I install latest (but unstable) Ninject for Web.Api from PM console using command

PM> Install-Package Ninject.Web.WebApi -Pre

2) Then i add this to "using" section of NinjectWebCommon.cs

using Ninject.Web.WebApi.FilterBindingSyntax;

After these steps my IKernel has method BindHttpFilter. And all works well

like image 21
ulmer-morozov Avatar answered Nov 03 '22 00:11

ulmer-morozov