Ninject 3 - BeginBlock()是否覆盖了asp.net WebAPI中的InRequestScope?

时间:2012-11-14 16:43:26

标签: asp.net-web-api ninject ninject.web

我的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.FromAssembliesMatching("WebApiTest.DataAccess.dll")
            .SelectAllClasses()
            .BindAllInterfaces()
            .Configure(config => config.InRequestScope()));

        kernel.Bind(x =>
            x.FromAssembliesMatching("WebApiTest.*.dll")
            .SelectAllClasses()
            .BindAllInterfaces()
            .Configure(config => config.InTransientScope()));
    }        
}

我的问题是关于NinjectDependencyResolver中的代码 - &gt; BeginScope()方法:返回新的NinjectDependencyScope(this.kernel.BeginBlock());

我想将我的存储库(在WebApiTest.DataAccess.dll中实现)作为请求作用域。我从Nate Kohari那里看到了post。我意识到帖子已经过时但是关于激活块的描述让我想知道这是否仍然是当前的实现。

  

通过激活块,有一种在Ninject2中处理范围的最终方法。块是一种覆盖绑定上声明的范围的方法,而是将&gt;激活的实例与块本身相关联。 ......

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

另外,听起来我使用BeginBlock()是可选的,但当我删除它时,对Controller的第一次调用成功,但任何后续调用都会抛出异常:

  

Ninject组件ICache没有在内核的组件容器中注册此类组件

WHY ??

2 个答案:

答案 0 :(得分:13)

现在使用这个 NinjectDependencyResolver和此NinjectDependencyScope实施

答案 1 :(得分:1)

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

将它们从请求中删除

我整理了一篇关于http://www.strathweb.com/2012/11/asp-net-web-api-and-dependencies-in-request-scope/

的博客文章