美文网首页
ninject +entity framework+控制器过滤器

ninject +entity framework+控制器过滤器

作者: dantes1844 | 来源:发表于2019-06-06 16:19 被阅读0次

    问题描述

    entity framework数据层,使用ninject进行注入,生命周期设置为InRequestScope,在过滤器中调用数据库时,会出现The operation cannot be completed because the DbContext has been disposed.

    image.png

    原因分析

    在SO上找到几个相关的问题,如下:

    1. https://stackoverflow.com/a/6194159/2031686

    2. https://stackoverflow.com/questions/6193414/dependency-injection-with-ninject-and-filter-attribute-for-asp-net-mvc/27011327

    3. https://github.com/ninject/Ninject.Web.Mvc/wiki/Dependency-injection-for-filters

    4. https://stackoverflow.com/a/7834381/2031686

    Filter Providers aren't created for every request and therefore aren't allowed to get any dependency in request scope.

    其中3里面github是官方关于过滤器中使用注入的方式介绍,前面两个答案也基本是采用了这个方式。

    解决方法

    最终采用了上面1里面的,BZ的回答。

    步骤

    1. 定义一个特性类,给要使用的控制器或者action打上相关标签

    2. 定义一个过滤器,该过滤器里使用构造函数的方式获取注入的实体对象并使用。我这里就是调用数据库的DbContext

    3. 使用如下方式进行绑定

      this.BindFilter<MyAuthorizeFilter>(System.Web.Mvc.FilterScope.Controller, 0).WhenControllerHas<MyAuthorizeAttribute>();
      

    相关文章

      网友评论

          本文标题:ninject +entity framework+控制器过滤器

          本文链接:https://www.haomeiwen.com/subject/bpsuxctx.html