12

I'm trying Windows Azure to host an MVC4 web application. I've created a test app, using VS2012 MVC4 internet application template and added a custom Model and Controller to it.

I've published it on Azure and managed to get 'update-database' apply migrations to the Azure Database.

When i try the app locally, but using the Azure SQL database, it works fine. I can login/register and use my test controller.

When i try the app online, i can use the test controller but login or register links give the following exception:

Server Error in '/' Application.

The "WebSecurity.InitializeDatabaseConnection" method can be called only once.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The "WebSecurity.InitializeDatabaseConnection" method can be called only once.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[InvalidOperationException: The "WebSecurity.InitializeDatabaseConnection" method can be called only once.]
   WebMatrix.WebData.WebSecurity.InitializeMembershipProvider(SimpleMembershipProvider simpleMembership, DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean createTables) +123
   WebMatrix.WebData.WebSecurity.InitializeProviders(DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) +51
   WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(String connectionStringName, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) +52
   MembershipTest2.Filters.SimpleMembershipInitializer..ctor() +193

Do you have any idea where that comes from ? If i debug (the local version), this method is only called once.

Thanks.

malhadeff
  • 121
  • 1
  • 3

3 Answers3

17

You could try encapsulating the call(s) to that method to ensure it's not called more then once

                if (!WebMatrix.WebData.WebSecurity.Initialized)
                {
                    WebSecurity.InitializeDatabaseConnection(...);
                }
Gabriel P.
  • 3,400
  • 2
  • 32
  • 23
  • 1
    had to do it twice, once in the implementation of the InitializeSimpleMembershipAttribute and then again in my ef-migration configuration.cs method where I 'InitializeDatabaseConnection'ed to seed some preconfigured users/roles. Thanks for the idea! Sometimes the simple solution works best – Ingo May 12 '13 at 15:22
  • 3
    Be careful: `Initialized` and `InitializeDatabaseConnection` are not thread-safe, and you can easily create conditions where you create the potential for a race condition. See [this answer](http://stackoverflow.com/a/16512694/1945631) for details. – Andy Brown Aug 24 '13 at 10:58
1

in my case I had both

(in web.config)

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

and

(in _ViewStart.cshtml)

WebSecurity.InitializeDatabaseConnection("club", "Account", "UserID", "UserName", autoCreateTables: true);

Solution: it seems you cannot have both, so remove one

goran
  • 11
  • 1
0

Does the following SO discussion help you?

I did find the following article helped me lot to use newer MVC4 & EF together with Simple Membership Provider so if you haven't read it please take a look:

SimpleMembership, Membership Providers, Universal Providers and the new ASP.NET 4.5 Web Forms and ASP.NET MVC 4 templates

Community
  • 1
  • 1
AvkashChauhan
  • 20,495
  • 3
  • 34
  • 65