3

我将我的项目升级到 MVC5 和 Web API 2,现在我的 ASP.NET Web API 的 Ninject 过滤器绑定收到以下错误。我安装了 Ninject.Web.WebApi-RC 包。有关如何解决此问题的任何解决方案?

kernel.BindHttpFilter<ApiValidationFilter>(FilterScope.Action)
    .WhenControllerHas<ApiValidationAttribute>();

kernel.BindHttpFilter<HttpHeaderFilter>(FilterScope.Controller)
    .WhenControllerHas<HttpHeaderAttribute>()
    .WithConstructorArgumentFromControllerAttribute<HttpHeaderAttribute>("name", q => q.Name)
    .WithConstructorArgumentFromControllerAttribute<HttpHeaderAttribute>("value", q => q.Value);

源代码错误:

public static IEnumerable<object> GetCustomAttributes(this HttpControllerDescriptor actionDescriptor, Type type)
{
    return ((IEnumerable)typeof(HttpControllerDescriptor)
        .GetMethod("GetCustomAttributes").MakeGenericMethod(type)
        .Invoke(actionDescriptor, new object[0])).Cast<object>();
}

错误

<Response>
<Message>An error has occurred.</Message>
<ExceptionMessage>Ambiguous match found.</ExceptionMessage>
<ExceptionType>System.Reflection.AmbiguousMatchException</ExceptionType>
<StackTrace>
at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers) at System.Type.GetMethod(String name) at Ninject.Web.WebApi.FilterBindingSyntax.HttpActionDescriptorExtensionMethods.GetCustomAttributes(HttpControllerDescriptor actionDescriptor, Type type) in d:\Projects\Ninject.Web.WebApi\src\Ninject.Web.WebApi\FilterBindingSyntax\HttpActionDescriptorExtensionMethods.cs:line 54 at Ninject.Web.WebApi.FilterBindingSyntax.FilterFilterBindingBuilder`1.<>c__DisplayClass9.<WhenControllerHas>b__8(HttpConfiguration controllerContext, HttpActionDescriptor actionDescriptor) in d:\Projects\Ninject.Web.WebApi\src\Ninject.Web.WebApi\FilterBindingSyntax\FilterFilterBindingBuilder.cs:line 166 at Ninject.Web.WebApi.FilterBindingSyntax.FilterFilterBindingBuilder`1.<>c__DisplayClass2.<When>b__0(IRequest ctx) in d:\Projects\Ninject.Web.WebApi\src\Ninject.Web.WebApi\FilterBindingSyntax\FilterFilterBindingBuilder.cs:line 126 at Ninject.Planning.Bindings.BindingConfiguration.Matches(IRequest request) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Bindings\BindingConfiguration.cs:line 129 at Ninject.Planning.Bindings.Binding.Matches(IRequest request) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Bindings\Binding.cs:line 237 at Ninject.KernelBase.<>c__DisplayClass1c.<SatifiesRequest>b__1b(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 455 at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate) at Ninject.KernelBase.CanResolve(IRequest request) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 314 at Ninject.KernelBase.Resolve(IRequest request) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 345 at Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 263 at Ninject.ResolutionExtensions.GetAll[T](IResolutionRoot root, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 113 at Ninject.Web.WebApi.Filter.NinjectFilterProvider.GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor) in d:\Projects\Ninject.Web.WebApi\src\Ninject.Web.WebApi\Filter\NinjectFilterProvider.cs:line 57 at System.Web.Http.Controllers.HttpActionDescriptor.<InitializeFilterPipeline>b__0(IFilterProvider fp) at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.Enumerable.<ReverseIterator>d__a0`1.MoveNext() at System.Web.Http.Controllers.HttpActionDescriptor.<RemoveDuplicates>d__3.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.Enumerable.<ReverseIterator>d__a0`1.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at System.Web.Http.Controllers.HttpActionDescriptor.InitializeFilterPipeline() at System.Lazy`1.CreateValue() at System.Lazy`1.LazyInitValue() at System.Lazy`1.get_Value() at System.Web.Http.Controllers.HttpActionDescriptor.GetFilterPipeline() at System.Web.Http.Controllers.HttpActionDescriptor.GetFilterGrouping() at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
</StackTrace>
</Response>
4

2 回答 2

4

在 Web.API 2.0 中HttpControllerDescriptor,该方法没有两个重载,GetCustomAttributes因此会.GetMethod("GetCustomAttributes")引发此异常。

所以这是 Ninject 中的一个错误,但已在 2013-10-25: Fixed FilterInjection for WebApi2 修复

所以你有三个一的选择:

  • 等待 更新到最新版本的 Ninject.Web.WebApi-RC 包,其中包含此错误,当前为:Ninject.Web.WebApi 3.0.2-unstable-9018
  • 您从 GitHub 获取源代码并为自己构建它。
  • 而不是使用固定调用逻辑的WhenControllerHas助手使用。WhenGetCustomAttributes
于 2013-11-03T12:26:22.640 回答
1

nemsev 的回答为我指明了正确的方向,但我最终不得不使用以下内容:

kernel.BindHttpFilter<ApiValidationFilter>(FilterScope.Action)
    .When((controllerContext, actionDescriptor) => 
            actionDescriptor.ControllerDescriptor.GetCustomAttributes<ApiValidationAttribute>().Any());
于 2013-11-13T16:06:33.020 回答