Ninject 没有解决依赖关系(无参数构造函数错误)
Ninject doesn't resolve dependency (parameterless constructor error)
我正在努力在 Web 服务应用程序中启用 Ninject 的构造函数注入。这是我采取的步骤 (Visual Studio 2013):
1) 使用 "ASP.NET Web Application" 模板创建了一个新项目。
2) 在项目创建向导的第二步中选择了一个 "Empty" 应用程序模板。
3) 在第三步调用"Add folders and core references for"中选择了"Web API"
4) 项目创建完成,现在我添加了一个类型为"Web API 2 Controller - Empty"的控制器。添加了以下代码:
using System.Web.Http;
namespace WebApplication1.Controllers
{
public interface IDummy
{
}
public class Dummy : IDummy
{
}
public class DefaultController : ApiController
{
public DefaultController(IDummy dummy)
{
}
[HttpGet]
[Route("")]
public string Index()
{
return "Hello World!";
}
}
}
5) 安装的 NuGet 包:Ninject.Web.WebApi(Ninject WebApi2 集成),现在我在其他线程中发现要使其与 IIS 一起工作,我需要安装 Ninject.Web.Common.WebHost 包裹。之后,NinjectWebCommon.cs 文件出现在 AppStart 中。我对此文件所做的唯一编辑是在 RegisterServices 方法中:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IDummy>().To<Dummy>();
}
现在,当我尝试 运行 应用程序时(在本地,只需在 Visual Studio 中按 F5)我希望在浏览器中获得 Hello World 字符串,但我却得到了臭名昭著的无参数构造函数错误:
我猜这意味着 Ninject 出于某种原因没有完成它的工作。不幸的是,到目前为止,谷歌搜索解决方案毫无结果。任何人都可以暗示我应该怎么做才能完成这项工作?提前致谢。
我能够通过使用 this solution 非常简单地完成这项工作。你只需要安装Ninject.Web.WebApi
然后添加下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
using Ninject;
using Ninject.Activation;
using Ninject.Parameters;
using Ninject.Syntax;
internal class NinjectDependencyResolver : NinjectScope, IDependencyResolver
{
private readonly IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
if (kernel == null)
throw new ArgumentNullException("kernel");
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectScope(this.kernel.BeginBlock());
}
}
internal 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()
{
IDisposable disposable = (IDisposable)resolutionRoot;
if (disposable != null) disposable.Dispose();
resolutionRoot = null;
}
}
用法
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
IKernel kernel = new StandardKernel();
kernel.Bind<IDummy>().To<Dummy>();
config.DependencyResolver = new NinjectDependencyResolver(kernel);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
我正在努力在 Web 服务应用程序中启用 Ninject 的构造函数注入。这是我采取的步骤 (Visual Studio 2013):
1) 使用 "ASP.NET Web Application" 模板创建了一个新项目。
2) 在项目创建向导的第二步中选择了一个 "Empty" 应用程序模板。
3) 在第三步调用"Add folders and core references for"中选择了"Web API"
4) 项目创建完成,现在我添加了一个类型为"Web API 2 Controller - Empty"的控制器。添加了以下代码:
using System.Web.Http;
namespace WebApplication1.Controllers
{
public interface IDummy
{
}
public class Dummy : IDummy
{
}
public class DefaultController : ApiController
{
public DefaultController(IDummy dummy)
{
}
[HttpGet]
[Route("")]
public string Index()
{
return "Hello World!";
}
}
}
5) 安装的 NuGet 包:Ninject.Web.WebApi(Ninject WebApi2 集成),现在我在其他线程中发现要使其与 IIS 一起工作,我需要安装 Ninject.Web.Common.WebHost 包裹。之后,NinjectWebCommon.cs 文件出现在 AppStart 中。我对此文件所做的唯一编辑是在 RegisterServices 方法中:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IDummy>().To<Dummy>();
}
现在,当我尝试 运行 应用程序时(在本地,只需在 Visual Studio 中按 F5)我希望在浏览器中获得 Hello World 字符串,但我却得到了臭名昭著的无参数构造函数错误:
我能够通过使用 this solution 非常简单地完成这项工作。你只需要安装Ninject.Web.WebApi
然后添加下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
using Ninject;
using Ninject.Activation;
using Ninject.Parameters;
using Ninject.Syntax;
internal class NinjectDependencyResolver : NinjectScope, IDependencyResolver
{
private readonly IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
if (kernel == null)
throw new ArgumentNullException("kernel");
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectScope(this.kernel.BeginBlock());
}
}
internal 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()
{
IDisposable disposable = (IDisposable)resolutionRoot;
if (disposable != null) disposable.Dispose();
resolutionRoot = null;
}
}
用法
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
IKernel kernel = new StandardKernel();
kernel.Bind<IDummy>().To<Dummy>();
config.DependencyResolver = new NinjectDependencyResolver(kernel);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}