Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class proxy (apicontroller) and constructor args #38

Open
grofit opened this issue Aug 29, 2017 · 1 comment
Open

class proxy (apicontroller) and constructor args #38

grofit opened this issue Aug 29, 2017 · 1 comment

Comments

@grofit
Copy link

grofit commented Aug 29, 2017

I have read in historical articles/tickets/posts that if you want to do proxying on classes you need to provide a parameterless constructor. I was just wondering is this still applicable, and if so is it a problem within the ninject layer or castle dynamic proxy layer?

As if I have a class like:

public class SomeClassToProxy
{
   public SomeClasstoProxy(ISomething something) { ... }

   [SomeInterceptAttribute]
   public virtual DoSomething() { ... }
   
}

And lets assume somewhere I have Bind<ISomething>().To<Something>();, so the DI framework knows about the ISomething and how to resolve it, so when it says it needs a parameterless constructor I am unsure why it causes a problem when I am binding against the class opposed to the interface, as even if I proxy against an interface (i.e. public class SomeClassToProxy : ISomeInterfaceToProxy) and have the binding at the interface layer it still needs to resolve the SomeClassToProxy at some point.

Anyway some info around this issue would be great as atm the moment we try to put any intercept attributes on an ApiController DynamicProxy blows up because it cannot find a parameterless constructor.

@spam1923
Copy link

For this I used the Ninject.Web.WebApi or if you do MVC its in the Ninject.Web.MVC package. There is a syntax I have used to put ActionFilters on Controllers when an Attribute exists. To do this I put these lines in with my Bindings that execute on start.

using Ninject.Web.WebApi.FilterBindingSyntax;
using Ninject.Web.Mvc.FilterBindingSyntax;

//MVC Example
            this.Kernel.BindFilter<YourActionFilterClass>(System.Web.Mvc.FilterScope.Action, 3).WhenActionMethodHas<YourAttributeUsedOnControllerToTriggerFilter>();
            this.Kernel.BindFilter<YourActionFilterClass>(System.Web.Mvc.FilterScope.Controller, 4).WhenControllerHas<YourAttributeUsedOnControllerToTriggerFilter>();

//WebApi Example
            this.Kernel.BindHttpFilter<YourActionFilterClass>(System.Web.Http.Filters.FilterScope.Action).WhenActionMethodHas<YourAttributeUsedOnControllerToTriggerFilter>();
            this.Kernel.BindHttpFilter<YourActionFilterClass>(System.Web.Http.Filters.FilterScope.Controller).WhenControllerHas<YourAttributeUsedOnControllerToTriggerFilter>();

In my case my Filters Inherited from one of these System.Web.Mvc.IAuthorizationFilter, System.Web.Http.Filters.IAuthorizationFilter depending on whether the filter is for WebAPI or MVC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants