WebAPI2项目中的Ninject问题

时间:2014-03-08 21:16:19

标签: asp.net-mvc ninject asp.net-mvc-5

我是Ninject的新手,目前正在尝试在WebAPI2项目中使用它。它一直告诉我,当我运行程序时它找不到某些类文件(特别是ResolutionExtensions.cs似乎很多)。

这是堆栈跟踪。它抱怨没有无参数的构造函数,但我试图查看其他类似的问题,似乎没有。

<StackTrace>
 at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method) at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType) at Ninject.Injection.DynamicMethodInjectorFactory.Create(ConstructorInfo constructor) in c:\Projects\Ninject\ninject\src\Ninject\Injection\DynamicMethodInjectorFactory.cs:line 48 at Ninject.Planning.Strategies.ConstructorReflectionStrategy.Execute(IPlan plan) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Strategies\ConstructorReflectionStrategy.cs:line 67 at Ninject.Planning.Planner.<>c__DisplayClass1.<CreateNewPlan>b__0(IPlanningStrategy s) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Planner.cs:line 109 at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable`1 series, Action`1 action) in c:\Projects\Ninject\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:line 32 at Ninject.Planning.Planner.CreateNewPlan(Type type) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Planner.cs:line 109 at Ninject.Planning.Planner.GetPlan(Type type) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Planner.cs:line 71 at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 77 at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:line 157 at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\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 c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:line 197 at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:line 165 at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 114 at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\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 c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 96 at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:line 157 at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 386 at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source) at Ninject.ResolutionExtensions.TryGet[T](IEnumerable`1 iterator) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 270 at Ninject.ResolutionExtensions.TryGet(IResolutionRoot root, Type service, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 189 at WebApiContrib.IoC.Ninject.NinjectDependencyScope.GetService(Type serviceType) 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)
</StackTrace>

我不确定它是否正在尝试引用dll,或者是否存在它应该链接到的实际类文件,但是here's an example of the file path it seems to be looking for (even though it does not exist on my local)

我的控制器看起来像这样

 public class TeamsController : ApiController
{ 
    private IGridironRepository<Team> _repo;

    public TeamsController(IGridironRepository<Team> repo)
    {
        _repo = repo;
    }

    // GET api/teams
    public IEnumerable<Team> Get()
    {
        var query = _repo.GetAll()
                    .OrderBy(x => x.Name)
                    .ToList();
        return query;
    }

    // GET api/teams/5
    public Team Get(int id)
    {
        var query = _repo.FindBy(x => x.Id == id)
                    .SingleOrDefault();

        return query;
    }
 }

我的app_start / ninjectwebcommon有这个

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        //Support WebAPI (Webapicontrib.ioc.ninject)
        GlobalConfiguration.Configuration.DependencyResolver =
            new NinjectResolver(kernel);

        RegisterServices(kernel);
        return kernel;
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind(typeof(IGridironRepository<>)).To(typeof(GridironRepository<>));
        kernel.Bind<GridironContext>().To<GridironContext>();
    }        

我的global.asax看起来像这样

public class WebApiApplication : System.Web.HttpApplication
{
     protected void Application_Start()
     {
         AreaRegistration.RegisterAllAreas();
         GlobalConfiguration.Configure(WebApiConfig.Register);
         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
         RouteConfig.RegisterRoutes(RouteTable.Routes);
         BundleConfig.RegisterBundles(BundleTable.Bundles);
     }
}

欢迎任何线索或想法,谢谢。

1 个答案:

答案 0 :(得分:0)

尝试明确注册Register Services():

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind(IGridironRepository<Team>).To(GridironRepository<Team>);
    kernel.Bind<GridironContext>().To<GridironContext>();
}