开发者

One Controller is Sometimes Bound Twice with Ninject

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
    }
}

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;
    }

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.

Here is the PhotoController in it's entirety:

public class PhotoController : Controller
{
    public PhotoController()
    {

    }

    public ActionResult Index(string i开发者_开发知识库d)
    {
        var dir = Server.MapPath("~/" + ConfigurationManager.AppSettings["UserPhotos"]);
        var path = Path.Combine(dir, id);

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

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜