将我从父方法中抛出的方法

发布于 2024-09-13 02:38:48 字数 1610 浏览 7 评论 0原文

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-09-20 02:38:48

这听起来就像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文