[RESOLVED]ASP.NET MVC 5 Identity Sample – Program hangs during database initialization

Hello,

I installed this sample

https://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples

It used to work perfectly, then it stopped working.

I tried the following

  • Change data source type
  • Recreate database file
  • Create new project and install a clean copy of the sample (It works for sometime until it does not)
  • Run this query select cmd,* from sys.sysprocesses where blocked > 0    // It produces 0 rows

This is the code of interest

//Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
        public static void InitializeIdentityForEF(ApplicationDbContext db) {
            var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
            const string name = "admin@example.com";
            const string password = "Admin@123456";
            const string roleName = "Admin";

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);     // <----------- The program hangs here
            if (role == null) {
                role = new IdentityRole(roleName);
                var roleresult = roleManager.Create(role);
            }
            /// ....
        }
    }

Other code portions you might find interesting

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("FormConnection", throwIfV1Schema: false)
        {
        }

        //// ...
    }

Connection strings (With previously tried strings)

<connectionStrings>
    <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)v11.0;Initial Catalog=SomeProject-2-1-alpha1;Integrated Security=SSPI" providerName="System.Data.SqlClient" />-->
  <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)v11.0;AttachDbFileName=|DataDirectory|Form.mdf;Initial Catalog=Form;Integrated Security=true" providerName="System.Data.SqlClient" />-->
  <!--<add name="FormConnection"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=(LocalDB)v11.0;AttachDbFileName=|DataDirectory|Form.mdf;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />-->
  <add name="FormConnection"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=.SQLEXPRESS;AttachDbFileName=|DataDirectory|Form.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />
</connectionStrings>

I can access the database tables using server explorer as well as insert data to the database.

Thank you in advance :)

Extra info:

1) I transferred the code to 2 other computers, the problem exist in all of them

2) The output shows the following

‘iisexpress.exe’ (CLR v4.0.30319: /LM/W3SVC/308064383/ROOT-1-130591711804159526): Loaded ‘C:WINDOWSMicrosoft.NetassemblyGAC_32System.Data.OracleClientv4.0_4.0.0.0__b77a5c561934e089System.Data.OracleClient.dll’. Skipped loading symbols. Module is optimized
and the debugger option ‘Just My Code’ is enabled.
‘iisexpress.exe’ (CLR v4.0.30319: /LM/W3SVC/308064383/ROOT-1-130591711804159526): Loaded ‘C:WINDOWSassemblyGAC_MSILMicrosoft.VisualStudio.Debugger.Runtime12.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.Debugger.Runtime.dll’.

The thread 0×2420 has exited with code 259 (0×103).
The thread 0×1624 has exited with code 259 (0×103).
The thread 0×2164 has exited with code 259 (0×103).
The thread 0x1ffc has exited with code 259 (0×103).

Hi A.Amri,

Thanks for your post.

A.Amri

‘iisexpress.exe’ (CLR v4.0.30319: /LM/W3SVC/308064383/ROOT-1-130591711804159526): Loaded ‘C:WINDOWSMicrosoft.NetassemblyGAC_32System.Data.OracleClientv4.0_4.0.0.0__b77a5c561934e089System.Data.OracleClient.dll’. Skipped loading symbols. Module is optimized
and the debugger option ‘Just My Code’ is enabled.
‘iisexpress.exe’ (CLR v4.0.30319: /LM/W3SVC/308064383/ROOT-1-130591711804159526): Loaded ‘C:WINDOWSassemblyGAC_MSILMicrosoft.VisualStudio.Debugger.Runtime12.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.Debugger.Runtime.dll’.

The thread 0×2420 has exited with code 259 (0×103).
The thread 0×1624 has exited with code 259 (0×103).
The thread 0×2164 has exited with code 259 (0×103).
The thread 0x1ffc has exited with code 259 (0×103).

This error is not obviously raising from the code that you provide.

Please check the "Enable Just My Code" settings:

tools -> options -> debugging

Here is a similar thread:https://social.msdn.microsoft.com/Forums/vstudio/en-US/eee0b10b-1ef3-44a8-af6d-77c27a0d0941/application-is-not-getting-launched-from-visual-studio?forum=msbuild

Hope this can be helpful.

Best Regards,

Eileen

Hello Eileen,

Thank you for your reply.

I thought that output provided useful information on the problem.

I tried disabling ‘Just My Code’ option but it did not make any difference.

The IISExpress is still hanging whenever I reach the line of code specified above.

The same problem was reported via different people in stackoverflow, all of them are using the identity sample I am using (Or older versions)

Thank you again,

-A

I contacted the technical support, they found a problem with the sample (Something caused a deadlock)

Mert Ozturk from the technical support team found the cause of the problem.

I found a work around

//Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
        public static void InitializeIdentityForEF(ApplicationDbContext db) {
            ApplicationUserManager userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
            RoleManager<IdentityRole> roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
            //var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
            const string name = "admin@example.com";
            const string password = "Admin@123456";
            const string roleName = "Admin";

            IdentityRole adminRole = new IdentityRole(roleName);

            //Create Role Admin if it does not exist
            if(!roleManager.RoleExists(roleName))
            {
                roleManager.Create(adminRole);
                db.SaveChanges();
            }
                                    
            PasswordHasher hasher = new PasswordHasher();

            ApplicationUser adminUser = new ApplicationUser { UserName = name, Email = name, PasswordHash = hasher.HashPassword(password), LockoutEnabled = false };
            
            var adminUserResult = db.Users.FirstOrDefault<ApplicationUser>(user => user.UserName == adminUser.UserName);
            if (adminUserResult == null)
            {
                db.Users.Add(adminUser);
            }

db.SaveChanges(); //// Add user admin to Role Admin if not already added if (roleManager.RoleExists(roleName)) { SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["FormConnection"].ConnectionString); con.Open(); SqlCommand command = new SqlCommand("INSERT INTO AspNetUserRoles (UserId, RoleId) VALUES(@userId, @roleId)", con); command.Parameters.AddWithValue("@userId", adminUser.Id); command.Parameters.AddWithValue("@roleId", adminRole.Id); command.ExecuteNonQuery(); con.Close(); } }

This will work until the problem on the other side is fixed :)

Edit: Disabling lazyloading fixes the problem

Leave a Reply