ASP.NET: This method cannot be called during the application's pre-start initialization stage

asp.net Mvcasp.net Mvc-3

asp.net Mvc Problem Overview


I'm trying to get an ASP.NET MVC 3 site running on IIS 6.0.

Currently when I request a page from the server it gives the following error:

> Parser Error Message: This method cannot be called during the application's pre-start initialization stage.

on this line:

<add name="MyMembershipProvider" type="NS.MyMembershipProvider" connectionStringName="MyDatabase" applicationName="/MySite"/>

I'm completely stumped and don't have much of a clue about the ASP.NET application lifecycle, let alone the differences between 6.0 and 7.0. Reading through the MSDN pages on it hasn't seemed to help much.

Does anyone have any insight or any good links for investigation? :)

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Add this in your web.config (in the appSettings section):

<add key="enableSimpleMembership" value="false"/>
<add key="autoFormsAuthentication" value="false"/>

EDIT:

For the ones who ask why, it is a known issue described in the mvc 3 release notes More details here

Solution 2 - asp.net Mvc

After upgrading some of my applications from ASP.NET MVC3 to MVC4 I was getting this error. It was a result of the WebMatrix assemblies (WebMatrix.WebData.dll and WebMatrix.Data.dll). I removed those references and assemblies from the /bin directory and that took care of the issue.

Solution 3 - asp.net Mvc

@Ek0nomik is right. We migrated from the MembershipProvider to the new ExtendedMembershipProvider allowing us to take advantage of some of the new functionality offered in the WebMatrix namespace. By default Simple Membership is enabled for some reason so we had to disable it explicitly since we didn't want to go as far as using the SimpleMembershipProvider.

All we had to do was add this to the web.config:

<add key="enableSimpleMembership" value="false"/>

Having Simple Membership enabled caused the Provider initialisation code to execute before the Application_Start handler. Our app structure requires App_Start to be the first thing to execute. Personally I would always expect this but Simple Membership changes this behaviour. Beware.

Solution 4 - asp.net Mvc

Well, I just got this error, and it resulted from having accidentally copied a .cshtml into the root of my project. It wasn't even included in the project. Deleted that and the error went away. This was with MVC3 on IIS7. I imagine some of the people getting this problem are in the same boat.

Solution 5 - asp.net Mvc

This is caused by any of a number of Reflection calls being made too early in an Application. It just so happens the Web.Config suggestions in other answers prevented one such Reflection call from being made. In my case however:

I'm using Entity Framework, and ran update-database. I got:

> This method cannot be called during the application's pre-start initialization phase.

As it turns out we had code that used a library which was recently modified to get all code in all namespaces/projects. Specifically, it called:

System.Web.Compilation.BuildManager.GetReferencedAssemblies()

Kaboom. That caused this obscure error. EF Migrations run in a weirdo zone where the application is half running and half not, meaning the above method can never be called by any code Migrations would call on.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsqwertyView Question on Stackoverflow
Solution 1 - asp.net MvcGregoireView Answer on Stackoverflow
Solution 2 - asp.net MvcJustin HelgersonView Answer on Stackoverflow
Solution 3 - asp.net MvcAndy McCluggageView Answer on Stackoverflow
Solution 4 - asp.net MvcDavid HammondView Answer on Stackoverflow
Solution 5 - asp.net MvcChris MoschiniView Answer on Stackoverflow