我将项目升级到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>
答案 0 :(得分:4)
在Web.API 2.0中,HttpControllerDescriptor
没有GetCustomAttributes
方法的两个重载,因此.GetMethod("GetCustomAttributes")
会抛出此异常。
所以这是Ninject中的一个错误,但这已在2013-10-25修复:Fixed FilterInjection for WebApi2。
所以你有三一个选项:
WhenControllerHas
调用逻辑的When
帮助器GetCustomAttributes
。答案 1 :(得分:1)
kernel.BindHttpFilter<ApiValidationFilter>(FilterScope.Action)
.When((controllerContext, actionDescriptor) =>
actionDescriptor.ControllerDescriptor.GetCustomAttributes<ApiValidationAttribute>().Any());