/// <summary> /// Archive the message and its parts and optionally archives the message and part properties. /// </summary> /// <param name="expiryMinutes">The amount of time (in minutes) before the archived message expires and will be automatically deleted from the archive.</param> /// <param name="includeProperties">A flag indicating if properties should be archived along with the message.</param> /// <param name="tag">An ArchiveTag to be associated with the archived message.</param> public Task Archive(int expiryMinutes, bool includeProperties, ArchiveTag tag) { return(Task.Factory.StartNew(() => { try { if (tag.ArchiveType.Active) { using (SqlServerConnection connection = new SqlServerConnection("MessageArchive")) { connection.RefreshConfiguration(); connection.Open(); connection.BeginTransaction(); try { ArchiveMessage(expiryMinutes, connection); if (includeProperties) { ArchiveMessageProperties(connection); } ArchiveParts(connection); if (includeProperties) { ArchivePartProperties(connection); } connection.Commit(); } catch (Exception ex) { if (connection.IsOpen()) { connection.Rollback(); } throw ex; } } } } catch (Exception ex) { Logger.WriteError(string.Format("Error archiving a message to database. The message will be writen to archive directory for later archival. \r\n Details: {0}", ex.ToString()), 128); HandleFailedArchiveMessage(); } })); }