views:

75

answers:

1

I have created my custom MembershipProvider. I have used an instance of the class DBConnect within this provider to handle database functions. Please look at the code below:

public class SGIMembershipProvider : MembershipProvider
{
    #region "[ Property Variables ]"
    private int newPasswordLength = 8;
    private string connectionString;
    private string applicationName;
    private bool enablePasswordReset;
    private bool enablePasswordRetrieval;
    private bool requiresQuestionAndAnswer;
    private bool requiresUniqueEmail;
    private int maxInvalidPasswordAttempts;
    private int passwordAttemptWindow;
    private MembershipPasswordFormat passwordFormat;
    private int minRequiredNonAlphanumericCharacters;
    private int minRequiredPasswordLength;
    private string passwordStrengthRegularExpression;
    private MachineKeySection machineKey; 

    **private DBConnect dbConn;**
    #endregion

.......

    public override bool ChangePassword(string username, string oldPassword, string newPassword)
    {
        if (!ValidateUser(username, oldPassword))
            return false;

        ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, newPassword, true);

        OnValidatingPassword(args);

        if (args.Cancel)
        {
            if (args.FailureInformation != null)
            {
                throw args.FailureInformation;
            }
            else
            {
                throw new Exception("Change password canceled due to new password validation failure.");
            }
        }
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@applicationName", applicationName);
        p[1] = new SqlParameter("@username", username);
        p[2] = new SqlParameter("@password", EncodePassword(newPassword));

        bool retval = **dbConn.ExecuteSP("User_ChangePassword", p);**
        return retval;
    } //ChangePassword


    public override void Initialize(string name, NameValueCollection config)
    {
        if (config == null)
        {
            throw new ArgumentNullException("config");
        }

        ......

        ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];

        if ((ConnectionStringSettings == null) || (ConnectionStringSettings.ConnectionString.Trim() == String.Empty))
        {
            throw new ProviderException("Connection string cannot be blank.");
        }

        connectionString = ConnectionStringSettings.ConnectionString;

        **dbConn = new DBConnect(connectionString);
        dbConn.ConnectToDB();**

        ......

} //Initialize

......

} // SGIMembershipProvider 

I have instantiated dbConn object within Initialize() event.

My problem is that how could i dispose off this object when object of SGIMembershipProvider is disposed off.

I know the GC will do this all for me, but I need to explicitly dispose off that object. Even I tried to override Finalize() but there is no such overridable method. I have also tried to create destructor for SGIMembershipProvider.

Can anyone provide me solution.

+1  A: 

From what I can see, MembershipProvider is not IDisposable (nor is ProviderBase), so we're really talking about garbage collection here, not disposal. You should be able to add your own finalizer (~SGIMembershipProvider() {}), but that should only talk to unmanaged objects - the other managed objects will also be collected, after all (and should handle their own unmanaged objects, if any - which is rare).

Marc Gravell
Thanks Marc! As I said, I have also created my own finalizer ~SGIMembershipProvider() {}. Is that going to be executed at all?
IrfanRaza
@IrfanRaza - yes; when it is garbage collected. If it isn't getting called, then perhaps the engine is keeping a single provider for the duration.
Marc Gravell
Is there any other better way?
IrfanRaza