ActionFilterAttribute ninject注射 - 的DbContext已被释放 [英] ActionFilterAttribute ninject injection - DbContext has been disposed

查看:733
本文介绍了ActionFilterAttribute ninject注射 - 的DbContext已被释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的项目,该项目使用的服务和工作单位(所有Ninject注入从NinjectModule依赖性)通常Repository模式,但我想从 ActionFilterAttribute <访问服务/ code>注入的页面布局的一些信息(从DB)我秀,所以我并不需要乱用所有的每个控制器上的动作。

I have my project which uses the usual Repository pattern with Services and Unit of Work (all with Ninject injecting the dependencies from a NinjectModule), but I'm trying to access a service from an ActionFilterAttribute to inject some information (from DB) in the layout of the pages I show so I don't need to mess with all the actions on each controller.

问题是当我保存到DB一个屏幕上,并移动到下一个,然后再回到previous(与标准 @ Url.Action ):在 ActionFilterAttribute 首页动作被触发,但在调用服务和相应的库(在属性中)因为的DbContext已被处置抛出一个异常

The problem comes when I save to DB on one screen and move to the next and then come back to the previous (with a standard @Url.Action): The ActionFilterAttribute for the Index action is triggered but the call to the service and corresponding repository (within the attribute) throw an exception because the DbContext has been disposed.

是否与访问服务,因此,在的DbContext ActionFilterAttribute ,同时注入该服务的问题通过物业注入?我要让我使用属性注射服务的属性,因为构造函数接受两个参数是任意取决于操作方法的签名,所以我唯一的选择是通过属性注入一个音符。

Is there any problem with accessing a service and, consequently, the DbContext from an ActionFilterAttribute while injecting the service via Property Injection? I want to make a note that I use property injection for the service in the attribute because the constructor receives 2 parameters that are arbitrary depending on the signature of the Action methods, so my only option was to inject via property.

让我知道,如果你需要一些code,我会更新的问题。

Let me know if you need some code and I'll update the question.

推荐答案

我找到了解决我的问题如下问题:
<一href=\"http://stackoverflow.com/questions/7192543/injecting-dependencies-into-asp-net-mvc-3-action-filters-whats-wrong-with-this\">Injecting依赖到ASP.NET MVC 3行动的过滤器。有什么不对这种做法?

I found the solution to my problem in the following question: Injecting dependencies into ASP.NET MVC 3 action filters. What's wrong with this approach?

组合<一个href=\"http://stackoverflow.com/questions/7192543/injecting-dependencies-into-asp-net-mvc-3-action-filters-whats-wrong-with-this/7194467#7194467\">Mark西曼的回答与<一个href=\"http://stackoverflow.com/questions/7192543/injecting-dependencies-into-asp-net-mvc-3-action-filters-whats-wrong-with-this/7194467#comment8645786_7194467\">striplingwarrior's评论是解决它。

Combining Mark Seeman's answer with striplingwarrior's comment was the solution to it.

基本上我分裂我的ActionFilterAttribute成仅仅是装饰我的行为,并保持我需要为以后的参数的属性,并且还融入了检查行动的自定义属性,如果我的属性存在,那么它注入我从想要的数据的ActionFilter数据库进入ViewBag。一切都是后来从Ninject的BindFilter扩展,因此仅适用于它所需要的方法绑定。

Basically I splitted my ActionFilterAttribute into an Attribute that merely decorated my Actions and keeps the parameters I need for later, and also into an ActionFilter that checked the Action's custom attributes and if my attribute exists, then it injects the data I wanted from the DB into the ViewBag. Everything is later binded with the BindFilter extension from Ninject so it applies only to the methods it needs.

这篇关于ActionFilterAttribute ninject注射 - 的DbContext已被释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆