我为 Cruise Control .net 创建了以下自定义任务,但我找不到有关如何将自定义任务添加到项目配置的单一参考。有没有人有一个有用的例子来说明如何在 cc.net 中添加对自定义任务的引用?
(以下是我的新任务)
public class RestoreDb : TaskBase
{
#region Parameters
[Required]
public string ServerName { get; set; }
[Required]
public string DbName { get; set; }
public string BackupFileName { get; set; }
#endregion
protected override bool Execute(ThoughtWorks.CruiseControl.Core.IIntegrationResult result)
{
bool returnResult = false;
try
{
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
connectionStringBuilder.IntegratedSecurity = true;
connectionStringBuilder.DataSource = ServerName;
SqlConnection connection = new SqlConnection(connectionStringBuilder.ToString());
connection.Open();
Server server = new Server(new ServerConnection(connection));
if (server.Databases[DbName] != null)
{
Log.Info("Dropping existing " + DbName + " on " + ServerName);
server.Databases[DbName].Drop();
}
else
{
Log.Info(DbName + " on " + ServerName + " doesn't exist.");
}
Log.Info("Restoring " + DbName + " on " + ServerName);
Database newDb = new Database(server, DbName);
Restore rs = new Restore();
rs.NoRecovery = false;
FileInfo fi = new FileInfo(server.Settings.BackupDirectory + "\\" + BackupFileName);
rs.Devices.AddDevice(fi.FullName, DeviceType.File);
rs.Database = DbName;
rs.Action = RestoreActionType.Database;
rs.ReplaceDatabase = true;
DataTable fileContents = rs.ReadFileList(server);
string originalDbName = fileContents.Rows[0][0].ToString();
string originalLogFileName = fileContents.Rows[1][0].ToString();
rs.RelocateFiles.Add(new RelocateFile(originalDbName,
string.Format(@"C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\{0}.mdf", DbName)));
rs.RelocateFiles.Add(new RelocateFile(originalLogFileName,
string.Format(@"C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\{0}_1.ldf", DbName)));
rs.SqlRestore(server);
Log.Info("Restoring done.");
returnResult = true; // success!
}
catch (Exception ex)
{
Log.Error(ex);
returnResult = false;
}
return returnResult;
}
}