加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-web-api – Ninject 3 – BeginBlock()是否覆盖了asp.ne

发布时间:2020-12-16 03:42:01 所属栏目:asp.Net 来源:网络整理
导读:我的asp.net WebApi项目包含多个服务,核心和数据访问程序集.为了在项目中使用Ninject作为我的DI容器,我从NuGet添加了Ninject.Web.Common包.然后,我实现了IDependencyResolver: public class NinjectDependencyResolver : NinjectDependencyScope,IDependenc
我的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()方法:返回新的NinjectDependencyScope(this.kernel.BeginBlock());

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

There’s one final way of handling scope in Ninject2,through activation blocks. Blocks are >a way to override the scope that was declared on a binding,and instead associate the >activated instances with the block itself. …

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

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

Ninject component ICache No such component has been registered in the kernel’s component container

为什么??

解决方法

现在用这个
NinjectDependencyResolver和 NinjectDependencyScope实施

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读