使用 Ninject2 InRequestScope 时可以回复 Dispose 进行清理吗?
在我正在开发的应用程序中,我创建了一个轻量级记录器,它有两个目的。使用枚举“Domain”记录的错误将记录到数据库中,而使用枚举“Application”记录的错误可以从服务中提取以显示给用户。
我使用 ninject InRequestScope 来允许在请求范围内维护错误集合。
我不确定的一个问题是我应该何时调用该函数将错误记录到数据库中。该函数使用事务将所有错误推送到数据库中。我的一个想法是让错误记录器使用 IDisposable,然后从 Dispose 方法调用 LogErrorsToDatabase() 方法。
这样做有什么问题吗?
ninject 会自动调用 dispose,还是需要配置它才能这样做?
我首先尝试将该服务注入 Global.asax,但这遇到了不可预见的问题。
编辑:我找到了一种更优雅的方法,因此为了将来的参考,我将在下面详细说明。
我首先创建了一个自定义操作过滤器,并使用属性注入来注入 IErrorLog 服务:
public class LogErrorsAttribute : ActionFilterAttribute
{
[Inject]
public IErrorQueue ErrorQueue { private get; set; }
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
ErrorQueue.CommitDatabaseLog();
base.OnResultExecuted(filterContext);
}
}
接下来我使用 Ninject 的 BindFilter 方法将此函数绑定到每个调用的操作:
BindFilter<LogErrorsAttribute>(FilterScope.Last, 0);
现在一切都完美且干净地运行。
最终编辑:我注意到最近有人对此表示赞同,所以他们可能使用了我找到的解决方案。我确实在上次编辑中发现了解决方案的错误。看来我的导航控制器正在触发此功能,在导航触发时,没有发生错误,这会写入过滤器上下文,并在链中稍后的操作调用时停止其运行。为了解决这个问题,我使用了以下绑定代码:
kernel.BindFilter<LogErrorsAttribute>(FilterScope.Last, 0).When(
(context, ad) => !string.IsNullOrEmpty(ad.ActionName) && ad.ControllerDescriptor.ControllerName.ToLower() != "navigation");
这将阻止任何导航代码触发过滤器(从控制器渲染部分视图。)
In an application I'm working on I've created a lightweight logger which has two purposes. Errors logged with the enum "Domain" will get logged to a database, whilst errors logged with the enum "Application" can be pulled from a service to be displayed to a user.
I'm using ninject InRequestScope to allow for the error collection to be maintained over the scope of the request.
One issue I'm uncertain about is when should I call the function to log errors to the database. This function pushes all errors into the DB using a Transaction. An idea I had was to make the error logger use IDisposable and then call the LogErrorsToDatabase() method from the Dispose method.
Are there any issues with doing this?
Will ninject automatically call dispose, or do I need to configure it to do so?
I first tried to inject the service into Global.asax, but this had unforseen issues.
EDIT: I found a way to do this more elegantly, so for future reference, I'll detail it below.
I first created a custom action filter and used property injection to inject the IErrorLog service:
public class LogErrorsAttribute : ActionFilterAttribute
{
[Inject]
public IErrorQueue ErrorQueue { private get; set; }
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
ErrorQueue.CommitDatabaseLog();
base.OnResultExecuted(filterContext);
}
}
I next used Ninject's BindFilter method to bind this function to every called action:
BindFilter<LogErrorsAttribute>(FilterScope.Last, 0);
Everything works perfectly and cleanly now.
Final Edit: I noticed someone has upvoted this recently, so they might have used the solution I found. I did discover a bug with the solution in the last edit. It seems that my navigation controller was firing this function, at the point that the navigation fired, no errors had occured, this wrote to the filtercontext and stopped it functioning when called by the action later down the chain. To resolve this, I used the following binding code:
kernel.BindFilter<LogErrorsAttribute>(FilterScope.Last, 0).When(
(context, ad) => !string.IsNullOrEmpty(ad.ActionName) && ad.ControllerDescriptor.ControllerName.ToLower() != "navigation");
This stops the filter being fired by any navigation code (partial views being rendered from the controller.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,Ninject 将处理请求范围内的对象。
Yes Ninject will dispose objects in request scope.