5

我的 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 -> BeginScope() 方法中的代码: return new NinjectDependencyScope(this.kernel.BeginBlock());

我想让我的存储库(在 WebApiTest.DataAccess.dll 中实现)成为请求范围。我从 Nate Kohari 那里看到了这篇文章。我意识到这篇文章很旧,但关于激活块的描述让我想知道这是否仍然是当前的实现。

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

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

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

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

为什么??

4

2 回答 2

13

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

于 2012-11-15T15:48:48.047 回答
1

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

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

于 2012-11-15T16:07:24.817 回答