为什么在通过c#创建新的DotNetNuke用户时会出现nullReferenceException?

时间:2012-04-13 13:43:49

标签: c# dotnetnuke

我在DotNetNuke模块中有以下代码来注册新用户:

protected static int RegisterNewDnnAccount(string firstName, string lastName, string email, string password)
    {
        // Create user
        var user = new UserInfo();
        user.FirstName = firstName;
        user.LastName = lastName;
        user.Email = email;
        user.DisplayName = string.Concat(firstName, " ", lastName);
        user.Username = email;
        user.IsSuperUser = false;

        user.Profile.FirstName = firstName;
        user.Profile.LastName = lastName;
        user.Profile.PreferredTimeZone = TimeZoneInfo.Local;
        user.Profile.PreferredLocale = "en-US";
        user.Profile.Telephone = string.Empty;
        user.Profile.Cell = string.Empty;
        user.Profile.PostalCode = string.Empty;
        user.Profile.City = string.Empty;
        user.Profile.Street = string.Empty;
        user.Profile.Region = string.Empty;

        // Set up membership
        var membership = new UserMembership(user);
        membership.Approved = true;
        membership.CreatedDate = DateTime.Now;
        membership.Password = password;
        membership.IsOnLine = false;
        user.Membership = membership;

        // Register the account
        var status = UserController.CreateUser(ref user);
        switch (status)
        {
            case UserCreateStatus.Success:
                return user.UserID;

            case UserCreateStatus.InvalidPassword:
                throw new InvalidOperationException("Failed to create user account: invalid password");

            case UserCreateStatus.InvalidEmail:
                throw new InvalidOperationException("Failed to create user account: Invalid email");

            case UserCreateStatus.DuplicateEmail:
                throw new InvalidOperationException("A user already exists with this email address");

            case UserCreateStatus.UserAlreadyRegistered:
                throw new InvalidOperationException("This user is already registered");

            default:
                throw new InvalidOperationException("user account creation failed for an unknown reason");
        }
    }

当调用UserController.CreateUser(ref user);方法时,DotNetNuke的API抛出NullReferenceException,当然零信息实际为空。

我已经确认我上面设置的每个字段都不是空的,所以很明显,我没有设置导致此问题的内容。我设置的大多数属性都来自我试图让这个错误停止,我没有做到。

有什么想法吗?

<小时/> 编辑:这是堆栈跟踪:

  

DotNetNuke.Entities.Portals.PortalController.IsMemberOfPortalGroup(Int32 portalId)      在DotNetNuke.Entities.Users.UserController.AutoAssignUsersToRoles(UserInfo user, Int32 portalId)
     在DotNetNuke.Entities.Users.UserController.CreateUser(UserInfo& user)
     在PlayerOnDemand.Common.Utils.ClientUserUtils.RegisterNewDnnAccount(String firstName, String lastName, String email, String password)的C:\ XOS \ Projects \ DNN \ Website \ DesktopModules \ XOSDigital.PlayerOnDemand \ PlayerOnDemand.Common \ Utils \ ClientUserUtils.cs:第160行

2 个答案:

答案 0 :(得分:1)

想出来。问题是我没有明确设置用户的门户网站。

答案 1 :(得分:0)

DNN中任何“未设置对象”的异常和堆栈跟踪都表明它是portalSettings或类似语言之类的通常是因为,在调用的上下文中,没有portalSettings对象加载到当前上下文中.items集合。

当人们在预定的呼叫中生成电子邮件或进行其他工作时,通常会发生这种情况,该呼叫没有加载当前的请求上下文。

由于此问题,您可能必须在虚假上下文中模拟当前的portalSettings对象,以便使用部分DNN Api。