我需要向Ninject.Web.MVC 3.0注册Ninject.OnePerRequestModule吗? [英] Do I need to register Ninject.OnePerRequestModule with Ninject.Web.MVC 3.0?

查看:83
本文介绍了我需要向Ninject.Web.MVC 3.0注册Ninject.OnePerRequestModule吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ninject for DI和MVC4应用程序中的Ninject.MVC3扩展,特别是版本3.0.0.6.

I'm using Ninject for DI and the Ninject.MVC3 extension within an MVC4 app, specifically version 3.0.0.6.

在Ninject的Wiki上阅读文档,看来用InRequestScope创建的对象的生存期实际上是由ASP.NET控制的,要正确清理对象的生存期,应注册OnePerRequest HttpModule.

Reading the documentation on Ninject's wiki, it appears that the lifetime of objects created with InRequestScope are actually controlled by ASP.NET and to properly clean up one should register the OnePerRequest HttpModule.

但是,由于找不到type="Ninject.OnePerRequestModule"/>,因此看起来好像已经过时了.

However it looks like this doco is out of date as type="Ninject.OnePerRequestModule"/> cannot be found.

另一方面,我最近读了一些,这似乎表明NinjectHttpApplication已经交易与OnePerRequest.

On the other hand I read some bits recently that seemed to suggest that NinjectHttpApplication already deals with OnePerRequest.

我的问题是Ninject.Web.MVC 3.0是否已经解决了此问题,或者我是否仍然必须手动将HttpModule添加到我的"web.config"中.

My question is whether Ninject.Web.MVC 3.0 already deals with this issue or if I still have to manually add the HttpModule to my 'web.config'.

我已将以下内容添加到我的配置中,并且似乎一切正常,尽管我根本不知道如何测试对象何时被破坏:

I have added the below to my config and everything appears to be working, though I have no idea at all how to test for when objects are destroyed:

  <system.web>
    <httpModules>
      <add name="OnePerRequestModule" type="Ninject.Web.Common.OnePerRequestHttpModule"/>
    </httpModules>
    ...

我需要手动添加OnePerRequest HttpModule还是Ninject.Web.MVC为我处理此问题?

Do I need to manually add the OnePerRequest HttpModule or does Ninject.Web.MVC handle this for me?

推荐答案

执行摘要:不,您不需要在web.config中执行任何操作.

Exec summary: No, you don't need to do anything in the web.config.

您可以通过以下方式手动进行健全性检查:

You could manually sanity check by:

  • 绑定某些内容IDisposable .InRequestScope()
  • 将其添加到控制器的控制器中
  • 附加调试器,在void Dispose()
  • 中放置一个断点
  • 手动触发HttpRequest
  • Binding something IDisposable .InRequestScope()
  • adding it to the ctor of a Controller
  • attaching a debugger, put a breakpoint in void Dispose()
  • manually triggering a HttpRequest

如果您按下Dispose,它会Ninject射出那只熊(然后您将有一个调用堆栈来证明它).

If you hit the Dispose, it woz Ninject that shot the bear (and you'll have a call stack to prove it).

它是什么电线? Ninject.Web.Common NuGet软件包添加:

What wires it it? The Ninject.Web.Common NuGet package adds:

public static class NinjectWebCommon
{
    public static void Start()
    {
        ...
        DynamicModuleUtility.RegisterModule( typeof( OnePerRequestHttpModule ) );

DynamicModuleUtility来自Microsoft.Web.Infrastructure(对Start方法的调用由以下驱动:

DynamicModuleUtility is from Microsoft.Web.Infrastructure (and the call to the Start method is driven by:

[assembly: WebActivator.PreApplicationStartMethod( typeof( App_Start.NinjectWebCommon ), "Start" )]

(来自WebActivator NuGet软件包)

(Which comes from the WebActivator NuGet package)

从学问上讲,文档 说:

要获得更多确定性行为,您可以执行以下任一操作:

To get more deterministic behavior, you can do any of the following:

  1. ...

  1. ...

使用为您注册OnePerRequestModule的Ninject.Web.Common扩展.

Use the Ninject.Web.Common extensions that register the OnePerRequestModule for you.

...

可以注册

(我的重点).我将使其更加明确...

(my emphasis). I'll make it more explicit...

顺便说一句,在开始回答这个问题的过程中,我没有特别的知识,只是Ninject Ninject.Web.Common的来源和我的一个应用程序.

BTW at the start of the exercise of answering this question, I had no special knowledge, just the source to Ninject Ninject.Web.Common and one of my apps.

这篇关于我需要向Ninject.Web.MVC 3.0注册Ninject.OnePerRequestModule吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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