.NET Web Api 2.1 中 Ninject 绑定的无参数构造函数错误 [英] Parameterless constructor error with Ninject bindings in .NET Web Api 2.1

查看:25
本文介绍了.NET Web Api 2.1 中 Ninject 绑定的无参数构造函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在编写 API 站点 (.NET Web Api 2.1)

Working currently on writing an API site (.NET Web Api 2.1)

对于我们之前的 API 站点,我们使用了 Ninject.MVC3 包并手动连接了依赖项解析器和范围,并根据建议将我们的逻辑插入到 NinjectWebCommon 中.

For our prior API sites we had used the Ninject.MVC3 package and wired up a dependency resolver and scope manually and plugged in our logic into NinjectWebCommon as per the recommendation.

这在我们的新项目中导致了无参数构造函数错误的胃灼热.过去,我们没有正确连接 Web Api 中的依赖项解析器,这是一个致命的放弃.只是这一次,我们是.它就在那里.

This was causing heartburn int our new project with the parameterless constructor error. In the past, this was a dead give away that we were not wiring up the dependency resolver in Web Api properly. Only this time, we were. It was there.

var resolver = new NinjectDependencyResolver(kernel);
GlobalConfiguration.Configuration.DependencyResolver = resolver;

所以我有点失落.上周看到 Ninject 更新了,WebAPI nuget 包复活了,所以决定尝试实现.

So I was a tad lost. In the last week, I saw that Ninject was updated, the WebAPI nuget package was revived so I decided to try and implement.

我安装了 NuGet 包 Ninject.Web.WebApi(3.2 版).这似乎没有包含应用绑定的 NinectWebCommon.cs 文件.它只添加了必要的程序集.经过一番挖掘,我偶然发现了这个实现.

I installed the NuGet package Ninject.Web.WebApi (version 3.2). This didn't seem to include a NinectWebCommon.cs file to apply bindings. It only added the necessary assemblies. Doing some digging I stumbled across this implementation.

https://gist.github.com/odytrice/5842010

(我不清楚 Nuget 包是否为我做这件事,这都是多余的代码.如果是这样,太好了,我现在只需要了解如何将我们的绑定添加到 nuget 包中.)

(I was unclear if the Nuget package was doing this for me and this is all redundant code. If so, great, I just need an understanding of how to add our bindings with the nuget package now.)

我逐行实现了这一行,除了加载模块的地方,我加载了三个单独的模块作为测试.

I implemented this line for line except where loading the modules, I loaded our three individual modules as a test.

public static INinjectModule[] Modules
{
    //get { return new INinjectModule[] { new MainModule() }; }
    get { return new INinjectModule[] { new GlobalModule(), new InventoryModule(), new StoreModule() }; }
    }

我已经确认依赖解析器是用这个新代码设置的.

I've verified that the dependency resolver is being set with this new code.

_resolver = new NinjectHttpResolver(modules);
GlobalConfiguration.Configuration.DependencyResolver = _resolver;

但问题依然存在.

异常消息:

An error occurred when trying to create a controller of type 'StoreSearchController'.
Make sure that the controller has a parameterless public constructor.

堆栈跟踪:

at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()

内部异常异常类型:System.ArgumentException

Inner Exception Exception Type: System.ArgumentException

堆栈跟踪:

at System.Linq.Expressions.Expression.New(Type type) 
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) 
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)

我不知所措.我已经看到很多关于必须设置 MVC 依赖项解析器的帖子,但我不认为这是相关的,因为在这种情况下,我们在不使用 Ninject 的家庭控制器之外没有 MVC 控制器.

I'm at a loss. I've seen lots of posts around having to set a MVC dependency resolver as well, but didn't think that was related as in this case we have no MVC controllers outside of the home controller which does not use Ninject.

其他一些可能无关紧要的花絮.我们正在使用属性路由.

One other tidbits that may or may not matter. We are using attribute routing.

感谢任何帮助.

推荐答案

您必须安装其中一个托管软件包:

You have to install one of the hosting packages:

  • Ninject.Web.WebApi.WebHost
  • Ninject.Web.WebApi.SelfHost
  • Ninject.Web.WebApi.OwinHost

这篇关于.NET Web Api 2.1 中 Ninject 绑定的无参数构造函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆