It seems that when I installed the Simple Injector MVC3 Integration package, SimpleInjectorInitializer was placed in the App_Start folder. However, when the Verify() method is called within Initialize(), it produces this error when attempting to get the Entity Framework Object Context (I have also provided the inner exceptions):

The supplied connection string is not valid, because it contains insufficient mapping or metadata information.
    InnerException: Unable to determine application context. The ASP.NET application path could not be resolved.
        InnerException: Exception has been thrown by the target of an invocation.
            InnerException: (Shown in browser) This method cannot be called during the application's pre-start initialization phase.

It also seemed that I had to move the registration calls from the Global.asax.cs Application_Start() to the SimpleInjectorInitializer's InitializeContainer() method.

I am not entirely sure what to do at this point, but this issue seems somewhat related to this question: Connection string exception after upgrading to Ninject 3.0.0-rc3

It seems that the application is trying to load the context before Entity Framework is even aware of its own and is unable to properly parse the connection string?

有帮助吗?

解决方案

It seems that the application is trying to load the context before Entity Framework is even aware of its own and is unable to properly parse the connection string?

You are absolutely right. The SimpleInjectorInitializer.Initialize method is called automatically during the pre application start phase. Although there is no problem configuring the container at this point, in your situation it's too early for Verify() to be called. Verify simply iterates all registrations and calls GetInstance on them to see whether it can create it. At that point in the application's lifetime however, an Entity Framework object context can't be created. I must say I think it is weird that EF framework knows anything about the ASP.NET framework, but unfortunately that's the way it is.

To solve this problem, you can either move the Verify() call, or the complete initialization to a later moment in the application startup. For instance, you can do the following:

  1. Remove the [assembly: WebActivator.PreApplicationStartMethod] (top line) of the SimpleInjectorInitializer.cs class.
  2. Add a call to the SimpleInjectorInitializer.Initialize() method in the Application_Start() event of the global asax.

After doing this, initializing (and especially, verifying) of the object graph is done after the pre-init state.

Another option is to remove the container.Verify(); call from the SimpleInjectorInitializer.Initialize method, since it is the early verification process that is killing you. However, please read this Verify the container’s configuration first, to see alternatives, before doing so.

The SimpleInjector.MVC NuGet package uses the WebActivator.PreApplicationStartMethod for convenience. It allows the package to 'just work', without a need for a developer to do any manual coding (except of course wiring the container) to get this working. But since this doesn't work when working with Entity Framework, we will have to reconsider this strategy.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top