0

I have for the first time installed Ninject. I am using the NinjectMvc3 version which gives you a NinjectMvc3 class in the App_Start folder via Nuget. There in the RegisterServices method I'm trying to hook up the entitty connection string I have in my web.config. Basically what I have right now is this.

var sConnection = ConfigurationManager.ConnectionStrings["dEntities"].ConnectionString;
        kernel.Bind<IDataContext>().To<DataContext>().WithConstructorArgument("dEntities", new dEntities(sConnection));

This gives me the following error Exception Details: System.InvalidOperationException: This method cannot be called during the application's pre-start initialization stage.

And heres a some of the source error:

/// Initialize a new dEntities object.
     /// </summary>
     public dEntities(string connectionString) : base(connectionString, "dEntities")
     {
         this.ContextOptions.LazyLoadingEnabled = true;

What am I doing wrong?

EDIT: I may add that I am using Entity Framework and the Repository pattern. What I'm hoping to do is somehow create an instance of the entitites connection string in web.config and bind it with NinjectMvc3. Anyone have any good input on the matter?

Tim
  • 531
  • 2
  • 7
  • 25

2 Answers2

0

Do you really need to use ConfigurationManager.ConnectionStrings["dEntities"].ConnectionString;? Because afaik ConfigurationManager is pretty much just left in for backwards compatibility*

You could try to use var connection = Properties.Settings.Default.dEntities

Ron Sijm
  • 8,490
  • 2
  • 31
  • 48
  • Well Im unsure of the setup so might not need it. Although the way you suggested when Im trying this I do not get a suggestion on Properties. Settings.Default.dEntities? Are you sure that it resides in that namespace? – Tim Dec 19 '11 at 13:54
  • It should. I'm using MVC3 too. (Not with Ninject but with castle windsor, but that shouldn't be relevant) and here are my settings: http://i.imgur.com/smXq6.png, and here in code: http://i.imgur.com/Myakz.png so just the same as the ConfigurationManager – Ron Sijm Dec 19 '11 at 14:08
  • Hmm, well Im not getting any suggestions. And this I take should be done in RegisterServices method in the NinjectMvc3 class? Thanks. – Tim Dec 19 '11 at 14:26
  • I don't think ConfigurationManager is for backwards compatibility. Holding connection strings in app.config (web.config) and reading it using ConfigurationManager is very typical. – Piotr Perak Dec 20 '11 at 12:23
0

If I remember well NinjectMVC3 has WebActivator dependency. Maybe that is the problem.

Instead of doing it in NinjectMV3 provided class try to do it in global.asax App_Start event handler.

Piotr Perak
  • 10,718
  • 9
  • 49
  • 86
  • Hmm could be. Right now it crashes in the Start() method in the NinjectMvc3 class on this line: bootstrapper.Initialize(CreateKernel); But I will try and google your suggestion and see if anyone else have experienced the same. Thanks! – Tim Dec 20 '11 at 13:47
  • Found a thread regarding this here http://stackoverflow.com/questions/5910376/ninject-mvc3-bootstrapper-throwing-already-initialized-exception I'm using the Nuget package maybe that is the problem? – Tim Dec 20 '11 at 13:56