如预期辛格尔顿范围绑定不工作 [英] Singleton Scope binding not working as intended

查看:108
本文介绍了如预期辛格尔顿范围绑定不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ninject MVC3插件与我的Web API应用。我有一个结合,看起来像:

I am using the ninject mvc3 plugin with my web api application. I have a binding that looks like:

kernel.Bind<的IFoo>()到<富方式>()InSingletonScope();

这是我的跨pretation的籽粒将创建中只有一个实例,并适当地重新使用它。通过将一个断点的构造函数,我可以清楚地看到,它是越来越每个请求调用一次,我无法解释为什么。

It is my interpretation that the kernal will create exactly one instance of Foo and reuse it appropriately. By putting a breakpoint in Foo's constructor, I can clearly see that it is getting called once per request, and I cannot explain why.

我唯一的猜测是,不知何故被越来越每个请求创建一个新的内核,但不会出现这样的情况,因为 CreateKernel 方法,设置全局依赖解析器仅在应用程序生命周期越来越运行一次。

My only guess is that somehow a new kernel is getting created per request, but that doesn't appear to be the case, as the CreateKernel method which sets the global dependency resolver is only getting run once in the application lifetime.

我使用的的这个帖子使ninject玩因为框架的变化MVC 4不错,我不得不做出我分配到 GlobalConfiguration.Configuration.DependencyResolver

I am using some code taken from this post to make ninject play nice with mvc 4. Because of framework changes, I had to make an additional wrapper that I assign to GlobalConfiguration.Configuration.DependencyResolver:

public class NinjectResolver : NinjectScope, IDependencyResolver
{
    private readonly IKernel _kernel;
    public NinjectResolver(IKernel kernel)
        : base(kernel)
    {
        _kernel = kernel;
    }
    public IDependencyScope BeginScope()
    {
        return new NinjectScope(_kernel.BeginBlock());
    }
}

我在做什么错了?

What am I doing wrong?

推荐答案

我从来没有得到它的正常工作,我不知道为什么。我的猜测是它是与MVC4整合是目前有点不成熟。

I never could get it to work properly, and I am not sure why. My guess is it has something to do with MVC4 integration being a bit immature at the moment.

由于我使用的是另一种:

As an alternative I am using:

kernel.Bind<的IFoo方式>()ToConstant(新富());

这似乎是工作,但我不太高兴。

This seems to work, but I am not too happy with it.

这篇关于如预期辛格尔顿范围绑定不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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