我的 asp.net WebApi项目由服务、核心和数据访问的多个程序集组成。为了尝试在项目中使用 Ninject 作为我的 DI 容器,我添加了来自 NuGet 的 Ninject.Web.Common 包。然后,我将 IDependencyResolver 实现为:
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
readonly IKernel kernel;
public NinjectDependencyResolver(IKernel kernel) : base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(this.kernel.BeginBlock());
}
}
public class NinjectDependencyScope : IDependencyScope
{
IResolutionRoot resolver;
public NinjectDependencyScope(IResolutionRoot resolver)
{
this.resolver = resolver;
}
public object GetService(System.Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
var resolved = this.resolver.Get(serviceType);
return resolved;
}
public System.Collections.Generic.IEnumerable<object> GetServices(System.Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
return this.resolver.GetAll(serviceType);
}
public void Dispose()
{
IDisposable disposable = resolver as IDisposable;
if (disposable != null)
disposable.Dispose();
resolver = null;
}
}
这是我的Ninject.Web.Common.cs。
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(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(x =>
x.FromAssembliesInPath(AppDomain.CurrentDomain.RelativeSearchPath)
.SelectAllIncludingAbstractClasses()
.BindDefaultInterface()
.Configure(config => config.InSingletonScope()));
//kernel.Bind(x =>
// {
// x.FromAssembliesMatching("*")
// .SelectAllClasses()
// .BindDefaultInterface()
// .Configure(b => b.InTransientScope());
// });
//kernel.Load()
//kernel.Bind<ISecurityService>().To<SecurityServiceImplementation>();
//kernel.Bind(x => x
// .FromAssembliesMatching("*")
// .SelectAllClasses()
// .BindDefaultInterface());
//.Configure(b => b.InTransientScope()));
//kernel.Load("*.dll");
}
}
例外情况是
[ActivationException: Error activating IHostBufferPolicySelector
No matching bindings are available, and the type is not self-bindable.
Activation path:
1) Request for IHostBufferPolicySelector
我已经使用了各种注册(注释掉),但没有一个有效。NinjectWebCommon.cs -> CreateKernel() 方法中的断点被命中,GetService(System.Type serviceType) 方法中的断点也会命中。AppDomain.CurrentDomain.RelativeSearchPath 解析为应用程序的 bin 目录,它包含所有 dll,包括 System.Web.Http.dll其中包含 IHostBufferPolicySelector 类型。
如何正确使用 Ninject.Extensions.Conventions 来设置内核以进行类型解析?
从 Remo 的答案中的提示和 Filip 的评论以及大量的调试时间来看,我发现在 GetService() 实现中使用 this.resolver.Get(serviceType)
而不是 this.resolver.TryGet(serviceType)
是我这种情况的罪魁祸首。
我计划写一篇关于这个的详细博客文章,但简而言之,一旦我们将 NinjectDependencyResolver 插入 MVC 使用以下行: GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
并且我们没有定义框架级依赖绑定(例如 IHostBufferPolicySelector 等),当某些框架级依赖关系未通过 Ninject 解析时,Get()
方法会引发异常。使用 TryGet()
不会引发异常,并且框架会回退到未解析(又名空)依赖项(如 IHostBufferPolicySelector)的默认依赖项。因此,选项是
- 使用 TryGet() 方法解析依赖关系。
- 包装进入尝试/捕获并丢弃异常。
试试这篇文章。 不是捕获 Ninject 异常,而是捕获所有 WebAPI 调用的异常。http://blog.greatrexpectations.com/2013/05/15/exception-handling-for-web-api-controller-constructors/在堆栈跟踪中,发生异常的构造函数将可见。
没有类HostBufferPolicySelector
因此没有IHostBufferPolicySelector
作为默认接口的类。您可以尝试BindAllInterfaces
或BindDefaultInterfaces
。