0

I have an MVC3 project that I am using Ninject to inject an Entity Framework context into. I am using the Ninject package (3.0.0.15), Ninject.MVC3 (3.0.0.6), and Ninject.Web.Common (3.0.0.7). Everything is working really great, except when I try to inject into a WebForms code behind file. I am assuming that this is because I don't have something wired in correctly, but am not sure at how to wire it in. Ninject is also not working in files that Razor instantiates.

Here is my code for my Code Behind:

[Inject]
public IDbContext DataContext { get; set; }

The Context property comes out null every time. It worked just fine until I updated to Ninject 3.0.

My start method is as follows:

public static void Start()
{
    DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
    DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
    Bootstrapper.Initialize(CreateKernel);
}

Any ideas on how to make Ninject inject the DataContext into the WebForm and into classes instantiated by Razor?

Dan VanWinkle
  • 991
  • 11
  • 20
  • Why are you having code behind files in an ASP.NET MVC application? – Darin Dimitrov Apr 17 '12 at 05:59
  • We haven't migrated the entire site over to MVC yet. This was entirely in WebForms, we still have a few WebForms pages left. – Dan VanWinkle Apr 17 '12 at 15:49
  • But if you have some legacy code why are you modifying it? Why are you trying to inject dependencies into it? Why not reuse it as-is and progressively migrate? – Darin Dimitrov Apr 17 '12 at 16:57
  • The data context isn't the biggest deal, but the UserRepository needs injected into the WebForms so I have access to the same user throughout. Why all the questions? The fact is I need to have dependencies injected into WebForms pages that are inside my MVC app. Does it really matter why? Even if you don't think there is a reason for it, that doesn't mean there isn't a reason. – Dan VanWinkle Apr 17 '12 at 19:26
  • I am asking all those questions to better understand your scenario. It is important to know as much details as possible in order to be able to provide a good answer. If you aren't in a mood to answer my questions or consider them useless feel absolutely free to ignore them. – Darin Dimitrov Apr 17 '12 at 20:21
  • Sorry, I didn't mean to be rude. You are right, you needed as much information as possible. My bad. – Dan VanWinkle Apr 17 '12 at 21:31

1 Answers1

1

For this to work you need to install the Ninject.Web NuGet (the latest version at the time of this writing is 3.0.0.5) and then have your webform derive from Ninject.Web.PageBase instead of System.Web.UI.Page:

public partial class WebForm1 : Ninject.Web.PageBase
{
    [Inject]
    public IDbContext Ctx { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

Also notice that I used Ctx as property name because there's already a property called Context on the System.Web.UI.Page class that you are hiding (you should have gotten a compile time warning).

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Perfect! This worked flawlessly. On the naming of the context part, I changed it to hide the customers name, forgot about the Context being hidden. My name for it is [CustomerName]Context. I will change that in the question :) – Dan VanWinkle Apr 17 '12 at 21:49
  • Just for anyone else who comes across this, if you already have App_Start.NinjectMVC3, you don't also need the file that the NuGet package creates for Ninject.Web. – Dan VanWinkle Apr 17 '12 at 21:51