Ninject3-BeginBlock()是否覆盖asp.net WebAPI中的InRequestScope



我的asp.net WebApi项目由服务、核心和数据访问的多个程序集组成。为了在项目中使用Ninject作为我的DI容器,我添加了Ninject。网状物NuGet的通用包。然后,我将IDdependencyResolver实现为:

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。网状物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.FromAssembliesMatching("WebApiTest.DataAccess.dll")
.SelectAllClasses()
.BindAllInterfaces()
.Configure(config => config.InRequestScope()));
kernel.Bind(x =>
x.FromAssembliesMatching("WebApiTest.*.dll")
.SelectAllClasses()
.BindAllInterfaces()
.Configure(config => config.InTransientScope()));
}        
}

我的问题是关于NinjectDependencyResolver->BeginScope()方法中的代码:返回新的NinjectDeprencyScope(this.cernel.BeginBlock());

我希望我的存储库(在WebApiTest.DataAccess.dll中实现)具有请求范围。我是从Nate Kohari那里看到这篇帖子的。我意识到这篇文章很旧,但关于激活块的描述让我怀疑这是否仍然是当前的实现。

Ninject2中有最后一种处理范围的方法,通过激活块。块是覆盖在绑定上声明的作用域的一种方法,而不是将>激活的实例与块本身关联。。。

那么,我的存储库的实际范围是什么

此外,在我看来,BeginBlock()的使用是可选的,但当我删除它时,对控制器的第一次调用成功,但任何后续调用都会引发异常:

Ninject组件ICache没有这样的组件在内核的组件容器中注册

为什么??

现在使用这个NinjectDependencyResolver和此NinjectDeprencyScope实现

为了在请求范围中使用依赖项,不要使用Ninject的InRequestScope()设置它们,而是使用GetDependencyScope()从请求中提取它们

我写了一篇关于这个的博客文章http://www.strathweb.com/2012/11/asp-net-web-api-and-dependencies-in-request-scope/

相关内容

  • 没有找到相关文章

最新更新