137

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? :)

Nkosi
  • 235,767
  • 35
  • 427
  • 472
sqwerty
  • 1,773
  • 2
  • 14
  • 14
  • 1
    I have the same issue with MVC 2 on the VS localhost webserver (Cassini). – Morten Christiansen Jan 11 '11 at 15:08
  • 1
    This same problem (and resolution) appears to be applicable to MVC 3 on IIS 7.0 as well. – CrazyPyro Apr 19 '12 at 20:38
  • 2
    Also have this problem with MVC4 & IIS6, with a new project. Fixed by removing webmatrix.*, see below answer. – Andrew Nov 08 '12 at 10:24
  • I had the same error, but not in the context of a membership provider, so the accepted answer was no help. Turns out I'd made a stupid mistake, not specifying the assembly in the configuration section. I changed the section name to "fully.qualified.name.of.type, assembly.name" to clear the error. – FishesCycle Oct 21 '13 at 22:52

5 Answers5

254

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

Gregoire
  • 24,219
  • 6
  • 46
  • 73
40

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.

Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
  • 2
    @rboarman - This has fixed it for me repeatedly. Are you sure the assemblies are not still sitting in the /bin directory? – Justin Helgerson Oct 31 '12 at 13:33
  • Adding the key as described above fixed it. The presence of WebMatrix.WebData.dll and WebMatrix.Data.dll didn't make a difference either way. – rboarman Oct 31 '12 at 17:06
  • This fixed it for me when I upgraded from MVC4RC to RTM. I started out by making a fresh project and copying over all the related bits. I made no code changes, and the web.config had no significant changes either. Strange. – Andrew Nov 08 '12 at 10:24
  • 1
    Fixed it for me. I was in a situation where I installed a few things for Azure, built the project, decided that wasn't what I wanted and did an undo through TFS, so all my code was IDENTICAL to what it was when it was working. However, my bin folder was a bit heavier. Clearing the WebMatrix assemblies out manually (as opposed to calling Clean on the project) worked. Thanks Ek0nomik. – Yetti Dec 06 '12 at 19:50
  • This fixed it for me also. These references are added by the MVC4 NuGet package but it looks like they are not necessary when publishing. – Isaac Llopis Jan 07 '13 at 12:49
  • I'm still having this problem (stuck on an old MVC) -- where are the assemblies coming from? – drzaus Oct 17 '18 at 13:31
8

@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.

Andy McCluggage
  • 37,618
  • 18
  • 59
  • 69
6

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.

David Hammond
  • 3,286
  • 1
  • 24
  • 18
2

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.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Chris Moschini
  • 36,764
  • 19
  • 160
  • 190