Question

Trying to create a user account in a test. But getting a Object reference is not set to an instanve of an object error when running it.

Here's my MemberShip provider class, it's in a class library MyCompany.MyApp.Domain.dll:

using System;
using System.Collections.Generic;
using System.Web.Security;

namespace MyCompany.MyApp.Domain
{
    public class MyMembershipProvider : SqlMembershipProvider
    {
        const int defaultPasswordLength = 8;
        private int resetPasswordLength;

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            resetPasswordLength = defaultPasswordLength;
            string resetPasswordLengthConfig = config["resetPasswordLength"];
            if (!String.IsNullOrEmpty(resetPasswordLengthConfig))
            {
                config.Remove("resetPasswordLength");

                if (!int.TryParse(resetPasswordLengthConfig, out resetPasswordLength))
                {
                    resetPasswordLength = defaultPasswordLength;
                }
            }
            base.Initialize(name, config);
        }

        public override string GeneratePassword()
        {
            return Utils.PasswordGenerator.GeneratePasswordAsWord(resetPasswordLength);
        }
    }
}

Here's my App.Config for my seperate Test Class Library MyCompany.MyApp.Doman.Test.dll that references my business domain library above:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="SqlServer" connectionString="data source=mycomp\SQL2008;Integrated Security=SSPI;Initial Catalog=myDatabase" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
        <membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="15">
            <providers>
                <clear/>
                <add name="MyMembershipProvider"
                     type="MyCompany.MyApp.Domain.MyMembershipProvider,MyCompany.MyApp.Domain"
                     connectionStringName="SqlServer"
                     applicationName="MyApp"
                     minRequiredNonalphanumericCharacters="0"
                     enablePasswordRetrieval="false"
                     enablePasswordReset="true"
                     requiresQuestionAndAnswer="false"
                     requiresUniqueEmail="true"
                     passwordFormat="Hashed"/>
            </providers>
        </membership>
    </system.web>
</configuration>

Here's my method that throws "Object reference is not set to an instanve of an object"

public class MemberTest
    {
        public static void CreateAdminMemberIfNotExists()
        {
            MembershipCreateStatus status;
            status = MembershipCreateStatus.ProviderError;

            MyMembershipProvider provider = new MyMembershipProvider();

            provider.CreateUser("Admin", "password", "someone@somewhere.co.uk", "Question", "Answer", true, Guid.NewGuid(), out status);
        }
    }

it throws on the provider.CreateUser line

Was it helpful?

Solution

I'm quite sure you should call provider.Initialize(...) in your test code before calling CreateUser.

OTHER TIPS

Well not quite anyone had the answer. csgero was on the right track though initialize was the problem. But just calling that directly was not a solution. This works:

public class MemberTest
    {
        public static void CreateAdminMemberIfNotExists()
        {
            MembershipCreateStatus status;
            MembershipUser member = Membership.CreateUser("Admin", "password", "email@somewhere.co.uk", "Question", "Answer", true, out status);
        }
    }

I believe instantiating my membership provider directly requires setting the config properties typically stored in the app.config or web.config and then calling initialize. Calling the static CreateUser method on the Mebership class however causes the config to be read, then the type specified in the config is parsed, loaded and initialised.

Is the error directly on provider.CreateUser or somewhere down the stack inside it - perhaps you could check for null before calling.

Perhaps a dependancy is missing - have you got the relevant DB dll's on the path?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top