Using WebMatrix, SimpleMembershipProvider in WCF service and the “This method cannot be called during the application’s pre-start initialization stage” error.

Having made the transition to using MVC architecture for all my new applications I recently came across a situation where I needed to use the WebMatrix membership provider within my WCF services. It wasn’t a trivial process so I thought I would document it here.

So how do you get access to the SimpleMembershipProvider in WCF?

First add the references to the WebMatrix dlls


Second add the Web.Config reference in system.web as such

<membership defaultProvider="SimpleMembershipProvider">
      type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"
      passwordFormat="Hashed" />

Making sure you select your defaultProvider as SimpleMembershipProvider.

Lastly make sure the required assemblies are referenced in the web.config system.web.compilation.assemblies section as such.

<compilation debug="true" targetFramework="4.5">
    <add assembly="WebMatrix.Data, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <add assembly="WebMatrix.WebData, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

and at that point you should be able to do this.

SimpleMembershipProvider provider = (SimpleMembershipProvider)System.Web.Security.Membership.Provider;

and use the provider now as you as you normally would i.e. ValidateUser(), ChangePassword() etc…

However at this point I ran into an issue where the following error appeared. “Parser Error Message: This method cannot be called during the application’s pre-start initialization stage.”. The solution to this problem was the following StackOverflow post and adding the these into my appSettings.

  <add key="autoFormsAuthentication" value="true"/>

Which successfully fixed the problem and everything worked at that point.