async void checkLabEndTime_Elapsed(object sender, ElapsedEventArgs e) { try { if (conn.State != ConnectionState.Open) { conn.ConnectionString = ConnectionString; conn.Open(); } } catch (Exception ex) { // Log.Write(EventKind.Critical, Log.FormatExceptionInfo(ex), null); //Log exception message } SqlCommand closeLabs = new SqlCommand("Select * from Labs where ((datediff(minute, end_time, getdate())) = 0) ", conn); SqlDataReader closeLabsReader = closeLabs.ExecuteReader(); if (closeLabsReader != null) { try { while (closeLabsReader.Read()) { string labName = closeLabsReader.GetString(1); int labID = closeLabsReader.GetInt32(0); SqlCommand VMList = new SqlCommand("Select * from LabVMs where Lab_ID = " + labID, conn); SqlDataReader VMListReader = VMList.ExecuteReader(); while (VMListReader.Read()) { SqlCommand updateLabsStatus = new SqlCommand("update labs set status='Deleting' where id = " + labID, conn); updateLabsStatus.ExecuteNonQuery(); string serviceName = VMListReader.GetString(1); SqlCommand closeParticipantList = new SqlCommand("Delete from LabParticipants where LabID = " + labID, conn); SqlDataReader closeParticipantReader = closeParticipantList.ExecuteReader(); SqlCommand closeLabConfigOb = new SqlCommand("Delete from LabConfigurations where LabID = " + labID, conn); SqlDataReader closeLabConfigReader = closeLabConfigOb.ExecuteReader(); SqlCommand deleteVMPath = new SqlCommand("Delete from LabVMs where Lab_ID = " + labID, conn); deleteVMPath.ExecuteNonQuery(); SqlCommand deleteLabsStatus = new SqlCommand("Delete from Labs where id = " + labID, conn); deleteLabsStatus.ExecuteNonQuery(); VMManager vmm = new VMManager(SubscriptionID, CertThumbPrint); string status = await vmm.DeleteQCVM(serviceName).ConfigureAwait(continueOnCapturedContext: false); } } } catch (Exception exc) { //Log Exception // Log.Write(EventKind.Critical, Log.FormatExceptionInfo(exc), null); } } }
async public Task <JsonResult> DeleteQCVM(int id) { VMManager vmm = new VMManager(ConfigurationManager.AppSettings["SubcriptionID"], ConfigurationManager.AppSettings["CertificateThumbprint"]); ApplicationDbContext db = new ApplicationDbContext(); var cloudService = db.QuickCreates.Where(l => l.ID == id ).FirstOrDefault(); await vmm.DeleteQCVM(cloudService.ServiceName); db.QuickCreates.Remove(cloudService); db.SaveChanges(); return Json(new { Status = 0 }); }