我正在通过我的Repository添加一个User(自定义类),方法是劫持泛型Add(T item)方法并将其抛给私有的AddUser方法,如果item == typeof(User)。它就是这样,但是一旦它碰到FlushMembership()调用,它就会过早退出,即。它不会触及FlushMembership()调用之外的任何内容,因此不会添加任何用户。它会逐步完成FlushMembership中的所有操作。我不明白。
private void AddUser(User u)
{
var existingUser = Membership.GetUser(u.Username);
// TODO: This could be more elegant
FlushMembership(); // Clean out this crap
var mU = existingUser;
if (mU == null)
{
Membership.CreateUser(u.Username, u.Password, u.Email);
CKDClientAreaEntities.Current.AddObject(GetSetName<User>(), u);
}
else
mU = Membership.GetUser(u.Username);
if(Roles.GetRolesForUser(u.Username).Count() <= 0)
Roles.AddUserToRole(mU.UserName, u.Role);
Membership.UpdateUser(mU);
}
private void FlushMembership()
{
var allMembers = Membership.GetAllUsers();
var allRoles = Roles.GetAllRoles();
foreach(var r in allRoles)
{
var ms = Roles.GetUsersInRole(r);
foreach(var m in ms)
{
var u = Users.Single(o => o.Username == m);
if(u == null)
Roles.RemoveUserFromRoles(m, allRoles); // If the user doesn't exist, remove them from all roles!
}
}
foreach(var m in allMembers)
{
var u = Users.Single(o => o.Username == m);
if (u == null)
Membership.DeleteUser(u.Username);
}
}
答案 0 :(得分:1)
这听起来就像FlushMembership中的某个东西抛出一个异常,并且它在调用堆栈中被抓到了更高的位置。您可以通过检查Visual Studio异常对话框中的所有托管异常(使用c#keybindings ctrl-alt-e)来尝试打破所有异常(而不仅仅是未处理的异常)。您还可以检查“输出”窗口中的第一次机会异常。