I have an ASP.NET Core 6 application that uses SQLite. I have a recurring Hangfire job that I'd like to use to back up the database using the online backup API of SQLite. Finally, I'd like to upload the backup database file to Azure blob storage. Code:
public void CreateBackup()
{
string backupPath = string.Empty;
using (var originalConnection = new SqliteConnection(this.configuration.GetConnectionString("Database")))
using (var backupConnection = new SqliteConnection(this.configuration.GetConnectionString("BackupDatabase")))
{
originalConnection.Open();
originalConnection.BackupDatabase(backupConnection);
backupPath = backupConnection.DataSource;
}
BlobClient client = new BlobClient(this.configuration.GetConnectionString("AzureBackup"), "tanusitvanytar", $"database-{DateTime.Now.ToString("yyyy-MM-dd")}");
client.Upload(backupPath);
}
But when the upload is executed I get an exception:
System.IO.IOException: 'The process cannot access the file.
The using block is supposed to close the connection to the backup database, so what is still using it? How can I fix this?
Either disable pooling by adding
Pooling=false
to the connection string
or call
SqliteConnection.ClearAllPools();
at the point where you want the connection to be closed.