一个控制器有时与Ninject绑定两次 [英] One Controller is Sometimes Bound Twice with Ninject

查看:51
本文介绍了一个控制器有时与Ninject绑定两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下NinjectModule,我们在其中绑定我们的存储库和业务对象:

I have the following NinjectModule, where we bind our repositories and business objects:

/// <summary>
/// Used by Ninject to bind interface contracts to concrete types.
/// </summary>
public class ServiceModule : NinjectModule
{
    /// <summary>
    /// Loads this instance.
    /// </summary>
    public override void Load()
    {
        //bindings here.
        //Bind<IMyInterface>().To<MyImplementation>();
        Bind<IUserRepository>().To<SqlUserRepository>();
        Bind<IHomeRepository>().To<SqlHomeRepository>();
        Bind<IPhotoRepository>().To<SqlPhotoRepository>();
        //and so on

        //business objects
        Bind<IUser>().To<Data.User>();
        Bind<IHome>().To<Data.Home>();
        Bind<IPhoto>().To<Data.Photo>();
        //and so on
    }
}

这是我们Global.asax的相关替代,我们继承自NinjectHttpApplication以便将其与Asp.Net Mvc集成(该模块位于名为Thing.Web.Configuration的单独的dll中):

And here are the relevant overrides from our Global.asax, where we inherit from NinjectHttpApplication in order to integrate it with Asp.Net Mvc (The module lies in a separate dll called Thing.Web.Configuration):

    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();

        //routes and areas
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);

        //Initializes a singleton that must reference this HttpApplication class,
        //in order to provide the Ninject Kernel to the rest of Thing.Web. This
        //is necessary because there are a few instances (currently Membership)
        //that require manual dependency injection.
        NinjectKernel.Instance = new NinjectKernel(this);

        //view model factory.
        NinjectKernel.Instance.Kernel.Bind<IModelFactory>().To<MasterModelFactory>();
    }

    protected override NinjectControllerFactory CreateControllerFactory()
    {
        return base.CreateControllerFactory();
    }

    protected override Ninject.IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load("Thing.Web.Configuration.dll");
        return kernel;
    }

现在,一切正常,但有一个例外:由于某种原因,有时Ninject会绑定光电控制器两次..这会导致ActivationException,因为Ninject无法辨别我想要哪个PhotoController.这会导致网站上所有对缩略图和其他用户图像的请求均失败.

Now, everything works great, with one exception: For some reason, sometimes Ninject will bind the PhotoController twice. This leads to an ActivationException, because Ninject can't discern which PhotoController I want. This causes all requests for thumbnails and other user images on the site to fail.

这是整个PhotoController:

Here is the PhotoController in it's entirety:

public class PhotoController : Controller
{
    public PhotoController()
    {

    }

    public ActionResult Index(string id)
    {
        var dir = Server.MapPath("~/" + ConfigurationManager.AppSettings["UserPhotos"]);
        var path = Path.Combine(dir, id);

        return base.File(path, "image/jpeg");
    }
}

每个控制器的工作方式都完全相同,但是由于某种原因,PhotoController受到了双重限制.即使这样,它也只会偶尔发生(在重新构建解决方案时,或者在应用程序池启动时在暂存/生产中).一旦发生这种情况,它会一直发生,直到我重新部署而不更改任何内容为止.

Every controller works in exactly the same way, but for some reason the PhotoController gets double-bound. Even then, it only happens occasionally (either when re-building the solution, or on staging/production when the app pool kicks in). Once this happens, it continues to happen until I redeploy without changing anything.

那么...这是怎么回事?

So...what's up with that?

推荐答案

您对以下问题的回答中所述另一个类似的问题,这是Ninject 2.0中的一个竞赛条件错误,该错误已在2.2版中修复.我找不到Ninject的任何发行说明,但它为我解决了这个确切的问题.

As noted in the comments of your answer to another similar question, this was a race condition bug in Ninject 2.0, which was fixed in version 2.2. I can't find any release notes for Ninject, but it solved this exact problem for me.

这篇关于一个控制器有时与Ninject绑定两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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