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

BindHttpFilter called twice, not only when IFilterBindingInNamedWithOrOnSyntax condition is true #21

Open
brenovieira opened this issue Oct 23, 2015 · 0 comments

Comments

@brenovieira
Copy link

I have these bindings in NinjectModule:

this.Bind<IBar>().ToMethod(ctx => new Bar()).WhenTargetHas<BarAttribute>();
this.Bind<IBar>().ToMethod(ctx => new Bar2());

When I inject IBar, Ninject correctly injects new Bar() when there's BarAttribute and
injects new Bar2() without BarAttribute.

For instance,

public class BarController
{
   public BarController([Bar]IBar bar) {} // injects new Bar()
}
public class Bar2Controller
{
   public Bar2Controller(IBar bar) {} // injects new Bar2()
}

But using BindHttpFilter, this "chain" behavior is different..

this.BindHttpFilter<FooAttribute>(FilterScope.Action)
   .WhenActionMethodHas<AllowAnonymousAttribute>()
   .WithConstructorArgument("foo", (object)null);
this.BindHttpFilter<FooAttribute>(FilterScope.Action);

public class FooController
{
   [AllowAnonymous]
   public Method() {}
}

public class FooAttribute : AuthorizeAttribute
{
   public FooAttribute(IDependency dependency) {}
}

When I request /foo/method, Ninject calls FooAttribute twice and injects null (using first binding) as expected, then instantiates FooAttribute again with IDependency binding.

What I expected was Ninject.Webapi chain the conditions and solve at the first match, as it does with normal Bind<>.

To solve my need (inject some paremeter when action is decorated with [AllowAnonymous], and another parameter when action isn't decorated with [AllowAnonymous]), I need to create mutual exclusive conditions which is a bad to maintain these two behaviors with Ninject Bind<> and Ninject.Web.WebApi BindHttpFilter<>:

this.BindHttpFilter<FooAttribute>(FilterScope.Action)
   .WhenActionMethodHas<AllowAnonymousAttribute>()
   .WithConstructorArgument("foo", (object)null);
this.BindHttpFilter<FooAttribute>(FilterScope.Action)
   .When((httpConfig, actionDescriptor) => !actionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any())

What do you think ?

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

1 participant