Answered by:
In "Seed()" method, when trying to use .FindbyName, thread exits.

Question
-
User-1134857695 posted
Hello to all! This problem tend to happen to me sporadically. I still do not know the cause of the issue.
Details:
ASP.NET 4.6 with MVC 5 + EF 6.1 + Identity 2.2.1
I have a simple seed method for inserting an Admin account to it. As following the guides from the Internet, I end up doing the following:
var role = roleManager.FindByName(roleName); if (role == null) { role = new AppRole(roleName); var roleresult = roleManager.Create(role); } var user = userManager.FindByName(UserName); if (user == null) { //Omitted code for brevety. var result = userManager.Create(user, password); } //Keep doing some stuff.
Simple, right? The code which I've omitted is for filling in the details.
The problem is the following: Most of the times, when I run the seed method and it hits the roleManager.FindByName(roleName) the thread in which it was running exits (with code 0 (0x0)). The same goes on with userManager.FindByName. But, if I don't use those methods, everything goes smoothly.
I have checked for values in the roleManager and it is not null (it would have shouted an object not reference...)
Not that is killing me, because I only use the Seed method once... But it intrigues me to what it could be causing it.
Thanks a lot!
Wednesday, February 10, 2016 5:06 AM
Answers
-
User-271186128 posted
Hi superjose,
The problem is the following: Most of the times, when I run the seed method and it hits the roleManager.FindByName(roleName) the thread in which it was running exits (with code 0 (0x0)). The same goes on with userManager.FindByName. But, if I don't use those methods, everything goes smoothly.From your description, I suggest you could set a breakpoint to debug your code, and check whether the roleName and username is null.
Besides, since you are using Asp.net Identity, I suggest you could refer to the following code to add user and role.
var UserManager = new UserManager<MyUser>(new UserStore<MyUser>(context)); var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); string name = "Admin"; string password = "123456"; //Create Role Admin if it does not exist if (!RoleManager.RoleExists(name)) { var roleresult = RoleManager.Create(new IdentityRole(name)); } //Create User=Admin with password=123456 var user = new MyUser(); user.UserName = name; var adminresult = UserManager.Create(user, password); //Add User Admin to Role Admin if (adminresult.Succeeded) { var result = UserManager.AddToRole(user.Id, name); }
More details, please refer to this article: https://blogs.msdn.microsoft.com/webdev/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes/
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 16, 2016 7:46 AM
All replies
-
User-1134857695 posted
Hmmm. Interesting. Evaluation times out when I try to put "roleManager.FindByName(roleName)" into a Watch window. Could it be database connection problems?
Wednesday, February 10, 2016 5:22 AM -
User-1134857695 posted
Woot! It worked again! I don't know... Really... what is it. I am using Windows 10 RTM 10586.101. Insider Preview was giving me some issues when running it.
Wednesday, February 10, 2016 5:30 AM -
User-1134857695 posted
Sigh* Keeps happening. Does anyone know a solution to this.
Update: (April 29th, 2016)
Sorry for not replying. I can't reply anymore since the thread is locked. I wanted to thank you. Your code is working, and I now know why. I used to grab the UserManager from the OwinContext, which was non-existent at that time. Forcing to create a new UserManager and RoleManager allowed me to access it directly without problems.
One year later the problem was fixed! Thanks a bunch!
Friday, February 12, 2016 5:19 PM -
User-271186128 posted
Hi superjose,
The problem is the following: Most of the times, when I run the seed method and it hits the roleManager.FindByName(roleName) the thread in which it was running exits (with code 0 (0x0)). The same goes on with userManager.FindByName. But, if I don't use those methods, everything goes smoothly.From your description, I suggest you could set a breakpoint to debug your code, and check whether the roleName and username is null.
Besides, since you are using Asp.net Identity, I suggest you could refer to the following code to add user and role.
var UserManager = new UserManager<MyUser>(new UserStore<MyUser>(context)); var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); string name = "Admin"; string password = "123456"; //Create Role Admin if it does not exist if (!RoleManager.RoleExists(name)) { var roleresult = RoleManager.Create(new IdentityRole(name)); } //Create User=Admin with password=123456 var user = new MyUser(); user.UserName = name; var adminresult = UserManager.Create(user, password); //Add User Admin to Role Admin if (adminresult.Succeeded) { var result = UserManager.AddToRole(user.Id, name); }
More details, please refer to this article: https://blogs.msdn.microsoft.com/webdev/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes/
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 16, 2016 7:46 AM