目前正在编写API站点(.NET Web Api 2.1)
对于我们以前的API站点,我们使用了Ninject.MVC3包并手动连接了依赖项解析器和范围,并根据建议将我们的逻辑插入到NinjectWebCommon中.
这导致我们的新项目出现胃灼热,无参数构造函数错误.在过去,这是一个死的赠品,我们没有正确地连接Web Api中的依赖性解析器.只有这一次,我们是.它就在那里.
var resolver = new NinjectDependencyResolver(kernel);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
Run Code Online (Sandbox Code Playgroud)
所以我有点失落.在上周,我看到Ninject已更新,WebAPI nuget包已复活,因此我决定尝试实施.
我安装了NuGet包Ninject.Web.WebApi(版本3.2).这似乎不包括应用绑定的NinectWebCommon.cs文件.它只添加了必要的组件.做一些挖掘我偶然发现了这个实现.
https://gist.github.com/odytrice/5842010
(我不清楚Nuget包是否正在为我做这个,这都是冗余代码.如果是这样,那很好,我只需要了解如何使用nuget包添加我们的绑定.)
除了加载模块之外,我在线上实现了这一行,我将三个单独的模块加载为测试.
public static INinjectModule[] Modules
{
//get { return new INinjectModule[] { new MainModule() }; }
get { return new INinjectModule[] { new GlobalModule(), new InventoryModule(), new StoreModule() }; }
}
Run Code Online (Sandbox Code Playgroud)
我已经验证了使用这个新代码设置了依赖项解析器.
_resolver = new NinjectHttpResolver(modules);
GlobalConfiguration.Configuration.DependencyResolver = _resolver;
Run Code Online (Sandbox Code Playgroud)
但问题仍然存在.
ExceptionMessage:
An error occurred when trying to create a controller of type 'StoreSearchController'.
Make sure that the controller has a …Run Code Online (Sandbox Code Playgroud) 我正在使用Ninject(MVC5 + WEBAPI)构建应用程序.并且有一些问题可以找出许多答案中的哪些答案解释了集成Ninject和WebApi的最新解决方案.所以我安装了以下软件包:
有人建议我的初始问题(Ninject不解析API控制器实例化)应该通过以下任一方法解决:
所以它归结为,所有这些包都是什么?另外,我需要使用Owin托管吗?我从nuget控制台得到一个'无法找到包Ninject.Web.WebApi'消息,所以我假设这个消息不存在了吗?
谢谢.
我正在使用Ninject.MVC3和WebAPI.最初,我正在使用这里概述的NinjectResolver和NinjectScope的实现,即使用,我注意到每次调用Controller时都会调用BeginBlock().在对控制器进行负载测试(超过几百次调用)时,我注意到w3wp的内存消耗显着增加(高负载时高达1.4 gigs)并且GC永远不会回收任何内存._kernel.BeginBlock()
根据这篇SO帖子,不应该处理内核并且不应该使用BeginBlock().之后我更新了Resolver和Scope,如下所示:
public class NinjectScope : IDependencyScope
{
protected IResolutionRoot resolutionRoot;
public NinjectScope(IResolutionRoot kernel)
{
resolutionRoot = kernel;
}
public object GetService(Type serviceType)
{
IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
return resolutionRoot.Resolve(request).SingleOrDefault();
}
public IEnumerable<object> GetServices(Type serviceType)
{
IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
return resolutionRoot.Resolve(request).ToList();
}
public void Dispose()
{
//Don't dispose the kernel
//IDisposable disposable = (IDisposable)resolutionRoot;
//if …Run Code Online (Sandbox Code Playgroud)