简体   繁体   English

Ninject OnePerRequestBehaviour似乎无法正常工作?

[英]Ninject OnePerRequestBehaviour doesn't seem to work correctly?

I've been using Ninject as my IOC in my web app. 我一直在我的网络应用程序中使用Ninject作为我的IOC。 It's great and I think it works really well, however I have been trying to register some interfaces / classes as OnePerRequestBehaviour but it doesn't seem to actually use the behaviour. 这很好,我认为它工作得很好,但我一直在尝试将一些接口/类注册为OnePerRequestBehaviour,但它似乎并没有真正使用这种行为。 The code runs correctly but in one of my classes it lazy loads page information from the database then once it has been loaded it doesn't need to hit the database. 代码运行正常但在我的一个类中,它延迟从数据库加载页面信息,然后一旦加载它就不需要命中数据库。

My problem is that the lazily loaded property will load in my first request, when I then request the next page the same instance of the class is used. 我的问题是,在我的第一个请求中加载了延迟加载的属性,然后我请求下一页使用该类的相同实例。 The reason I know this is because the class is not instantiated again and the lazily loaded property is already set. 我知道这个的原因是因为类没有再次实例化,并且已经设置了延迟加载的属性。

This code is within my module class : 这段代码在我的模块类中:

public class NinjectModule : StandardModule
{
    public override void Load()
    {
        Bind<IUnitOfWorkDataStore>().To<HttpContextDataStore>().Using<OnePerRequestBehavior>();


        Bind<CmsService>().ToSelf().Using<OnePerRequestBehavior>();
        Bind<CmsRepository>().ToSelf().Using<OnePerRequestBehavior>();
    }
}

Then inside my Global.asax which inherits from NinjectHttpApplication I have the following: 然后在继承自NinjectHttpApplication的Global.asax中,我有以下内容:

        protected override IKernel CreateKernel()
        {
            OnePerRequestModule module = new OnePerRequestModule();
            module.Init(this);

            KernelOptions options = new KernelOptions();
            options.InjectNonPublicMembers = true;

            IKernel kernel = new StandardKernel(options, new NinjectModule());

            return kernel;
        }

The first call made to CmsService is within the global.asax as well on authenticate_request: 对CmsService的第一次调用是在global.asax以及authenticate_request中:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Url.AbsoluteUri.Contains(".aspx") &&
                !HttpContext.Current.Request.Url.AbsoluteUri.Contains(".aspx/"))
            {
                CmsService facCMS = HttpKernelFactory.Get<CmsService>();
                ContentPage page = facCMS.GetCurrentPage();

                // DO Logic based on the page being brought back
            }
        }

The above GetCurrentPage() code: 上面的GetCurrentPage()代码:

public ContentPage GetCurrentPage()
{
    if (_currentPage != null)
        return _currentPage;

    return GetCurrentPage(_isAdmin);
}

So as you can see the _currentPage variable is only loaded if it hasn't been set before, which should be once per request, however Ninject doesn't seem to be creating the CmsService per request it seems to create it for an abritrary amount of time. 因此,你可以看到_currentPage变量只有在之前没有设置时才加载,每个请求应该是一次,但是Ninject似乎并没有按照请求创建CmsService它似乎创建了一个非常大量的时间。

Deos anyone have any idea of why this isn't working for me or any example code of where it definately does work? Deos任何人都知道为什么这对我不起作用或任何示例代码肯定会起作用?

Thanks 谢谢

The OnePerRequestModule is an HttpModule, and needs to be loaded into your ASP.NET pipeline in order to work. OnePerRequestModule是一个HttpModule,需要加载到您的ASP.NET管道才能工作。 If you add it to your web.config, it should work: 如果将其添加到web.config,它应该工作:

IIS7: IIS7:

<system.webServer> 
  <modules>
    <add name="OnePerRequestModule" type="Ninject.Core.Behavior.OnePerRequestModule, Ninject.Core"/>
  </modules>
</system.webServer>

IIS6: IIS6:

<system.web>
  <httpModules>
    <add name="OnePerRequestModule" type="Ninject.Core.Behavior.OnePerRequestModule, Ninject.Core"/>
  </httpModules>
</system.web>

The OnePerRequest behavior is greatly improved in Ninject2 (which is yet to be released). 在Ninject2(尚未发布)中,OnePerRequest行为得到了极大的改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM