views:

40

answers:

1

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);
        }
    }
+1  A: 

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.

Simon Svensson
Indeed it was. This assumed m was the username, where it is infact a MembershipUser object! var u = Users.Single(o => o.Username == m);
George R