WebSecurity.InitializeDatabaseConnection method can be called only once
Asked Answered
S

3

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.

Staircase answered 25/10, 2012 at 16:38 Comment(2)
how is this off topic? I am having this same problem.Sweeten
where is the code that you call WebSecurity.InitializeDatabaseConnection ?Orthopterous
S
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(...);
                }
Spotty answered 4/1, 2013 at 20:30 Comment(2)
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 bestAdrastus
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 for details.Bowdlerize
B
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

Brittne answered 21/3, 2013 at 1:18 Comment(0)
P
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

Paracelsus answered 20/12, 2012 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.