0

尝试使用 ninject 将我的接口绑定到一个具体的类,但我必须遗漏一些东西并且无法让它工作。

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation
  Source=mscorlib
  ParamName=instantiation
  StackTrace:
       at System.RuntimeType.MakeGenericType(Type[] instantiation)
       at Ninject.Activation.Providers.StandardProvider.GetImplementationType(Type service) in StandardProvider.cs:line 126
       at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in StandardProvider.cs:line 77
       at Ninject.Activation.Context.Resolve() inContext.cs:line 157
       at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in KernelBase.cs:line 386
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
       at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in Target.cs:line 197
       at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) inTarget.cs:line 165
       at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in StandardProvider.cs:line 114
       at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in StandardProvider.cs:line 96
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in StandardProvider.cs:line 96
       at Ninject.Activation.Context.Resolve() inContext.cs:line 157
       at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in KernelBase.cs:line 386
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
       at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) inTarget.cs:line 197
       at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) inTarget.cs:line 165
       at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in StandardProvider.cs:line 114
       at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in StandardProvider.cs:line 96
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in StandardProvider.cs:line 96
       at Ninject.Activation.Context.Resolve() inContext.cs:line 157
       at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in KernelBase.cs:line 386
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
       at PriceCheck.Api.Models.Ninject.NinjectScope.GetService(Type serviceType) in NinjectScope.cs:line 24
       at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
       at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
  InnerException: 

在这里崩溃

   public object GetService(Type serviceType)
        {
            IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
            return resolutionRoot.Resolve(request).SingleOrDefault();
        }



  public interface IResponseResult
    {
       void AddError(string key, string errorMessage);
       bool IsValid();
       Dictionary<string, string> Errors { get; }

       ResponseBase Status { get; set; }
    }


 public class ResponseResult<T> : IResponseResult
    {
        public ResponseResult()
        {
            Errors = new Dictionary<string, string>();
            Status = new ResponseBase();
        }

        public void AddError(string key, string errorMessage)
        {
            if (!Errors.ContainsKey(key))
            {
                Errors.Add(key, errorMessage);
            }        
        }

        public bool IsValid()
        {
            if (Errors.Count > 0)
            {
                return false;
            }

            return true;
        }

        public Dictionary<string, string> Errors { get; private set; }
        public T Response { get; set; }

        public ResponseBase  Status { get; set; }
    }


Bind(typeof(IResponseResult)).To(typeof(ResponseResult<>));
4

1 回答 1

2

您并没有真正为服务提供“构造的通用类型”。
泛型 - 开放和封闭构造类型
.NET 中的“开放泛型类型”到底是什么?

Ninject inside 实际上试图制造一个 - 但期望generic arguments你没有。

Type.MakeGenericType(service.GetGenericArguments())  

我不确定您的想法是什么 - 以及如何解决此类问题。

您可以尝试ToProvider或设置提供程序 - 但您仍然缺少一些东西。

于 2013-04-21T19:53:38.793 回答