SimpleInjector.MVC NuGet package and Entity Framework

Coordinator
Jul 12, 2012 at 8:16 PM

The SimpleInjector.MVC NuGet package adds an SimpleInjectorInitializer.cs file to the App_Start folder of the MVC application. To make life for the developer as simple as possible, the registration of the PreApplicationStartMethodAttribute (first line) is used to trigger the initialization during application start-up. The initialization is executed very early in the ASP.NET pipeline, even before Application_Start event fires.

Although there is no problem configuring the container at this point, when working with Entity Framework, it's too early to create an ObjectContext instance. When the container is constructed to return ObjectContext instances, pre application start-up is too early for container.Verify() to be called, which is what the code in the SimpleInjectorInitializer.cs does. Verify simply iterates all registrations and calls GetInstance on them to see whether it can create it.

The easiest way to solve this, is to move this initialization to the Application_Start event. You can do this by removing the PreApplicationStartMethodAttribute from the SimpleInjectorInitializer.cs and call the SimpleInjectorInitializer.Initialize method from within the Application_Start event of the MvcApplication class in the global.asax file.

Marked as answer by dot_NET_Junkie on 2/26/2014 at 1:25 PM
Feb 18, 2013 at 10:29 PM
Hi!

I'm using ASP.NET 4.5 and MVC 4 with WebActivatorEx (newer version of WebActivator) and, of course, Entity Framework 5.

What I've done is that I replace the call to WebActivatorEx.PreApplicationStartMethod with WebActivatorEx.PostApplicationStartMethod (Pre with Post).

My IoC demands are just limiting the number of DbContexts that are created to a single one per request and this solution works fine.

I'm not entirely familiar with the whole ASP.NET pipeline, so do you think this is an correct fix? Or does it now actually execute to late in the pipeline?

--gligoran
Coordinator
Feb 19, 2013 at 9:42 AM
Your observation is correct. The PostApplicationStartMethod will be fired after the application's App_Start event and at this point in time, Entity Framework can safely run. For the v2 release, the NuGet package will use the PostApplicationStartMethod instead of the PreApplicationStartMethod. Please note though, that upgrading to v2 will not automatically solve this, since NuGet will not override the template file the package contains. If you're updating, you can safely change the PreApplicationStartMethod to PostApplicationStartMethod.
Feb 19, 2013 at 5:22 PM
dot_NET_junkie wrote:
Please note though, that upgrading to v2 will not automatically solve this, since NuGet will not override the template file the package contains.
Are you talking about NuGet skipping the files that have been modified, when updating/uninstalling packages?
Coordinator
Feb 19, 2013 at 6:18 PM
gligoran wrote:
Are you talking about NuGet skipping the files that have been modified, when updating/uninstalling packages?
Exactly.
Feb 21, 2013 at 6:08 PM
You could write up a PowerShell script that just changes the WebActivator to WebActivatorEx in the SimpleInjectorInitializer.cs.

Here's a sample of PowerShell scripts I use for the FancyBox NuGet package I maintain: https://gist.github.com/gligoran/5006763.
Coordinator
Feb 21, 2013 at 10:27 PM
I'm sorry, but that would would be scary shit. I'm not willing to go that path. I hope that splitting the package in two is a better solution.
Feb 22, 2013 at 9:48 AM
I completely understand, just wanted to point out there is a way. Splitting packages is a much better way, I agree.