我已創建自定義MembershipProvider。我在這個提供程序中使用了DBConnect類的一個實例來處理數據庫函數。請看下面的代碼:如何處理自定義會員供應商內的自定義對象
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
我已經實例化Initialize()
事件中dbConn
對象。
我的問題是,當SGIMembershipProvider
的對象被關閉時,我該如何處理掉這個對象。
我知道GC會爲我做這一切,但我需要明確處置掉這個對象。即使我試圖覆蓋Finalize()
,但沒有這種可重寫的方法。我也嘗試爲SGIMembershipProvider
創建析構函數。
任何人都可以提供我的解決方案。
謝謝馬克!正如我所說,我也創建了自己的終結器〜SGIMembershipProvider(){}。這是否會被執行? – IrfanRaza 2010-04-16 10:44:03
@IrfanRaza - 是的;當它被垃圾收集時。如果它沒有被調用,那麼引擎在這段時間內可能會保持一個提供者。 – 2010-04-16 10:50:32
還有其他更好的方法嗎? – IrfanRaza 2010-04-16 11:23:30