将我从父方法中抛出的方法
如果 item == typeof(User),我将通过劫持通用 Add(T item) 方法并将其抛出到私有 AddUser 方法,通过我的存储库添加一个 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);
}
}
I'm adding a User (custom class) through my Repository by hijacking the generic Add(T item) method and throwing it to a private AddUser method, if item == typeof(User). It does just that, but as soon as it hits the FlushMembership() call, it exits out too early, ie. it doesn't hit anything beyond that FlushMembership() call and thus no User gets added. It steps through everything in FlushMembership though. I don't get it.
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来就像 FlushMembership 中的某些东西抛出异常,并且它被捕获在调用堆栈的更高位置。您可以通过检查 Visual Studio 异常对话框(使用 c# 键绑定的 ctrl-alt-e)中的所有托管异常来尝试中断所有异常(而不仅仅是未处理的异常)。您还可以检查“输出”窗口是否有第一次机会出现的异常。
This sounds exactly like something in FlushMembership throws an exception, and it's getting caught higher up in the call stack. You can try breaking on all exceptions (and not just unhandled exception) by checking all managed exceptions in the Visual Studio exception dialog (ctrl-alt-e using c# keybindings). You could also check your Output window for first chance exceptions.