diff --git a/src/App/Services/MobileStorageService.cs b/src/App/Services/MobileStorageService.cs index f5b2914a8..158a31848 100644 --- a/src/App/Services/MobileStorageService.cs +++ b/src/App/Services/MobileStorageService.cs @@ -1,11 +1,12 @@ using Bit.Core; using Bit.Core.Abstractions; +using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Bit.App.Services { - public class MobileStorageService : IStorageService + public class MobileStorageService : IStorageService, IDisposable { private readonly IStorageService _preferencesStorageService; private readonly IStorageService _liteDbStorageService; @@ -88,5 +89,17 @@ namespace Bit.App.Services } return _liteDbStorageService.RemoveAsync(key); } + + public void Dispose() + { + if (_liteDbStorageService is IDisposable disposableLiteDbService) + { + disposableLiteDbService.Dispose(); + } + if (_preferencesStorageService is IDisposable disposablePrefService) + { + disposablePrefService.Dispose(); + } + } } } diff --git a/src/Core/Services/LiteDbStorageService.cs b/src/Core/Services/LiteDbStorageService.cs index 6990e06fd..7ba9617f5 100644 --- a/src/Core/Services/LiteDbStorageService.cs +++ b/src/Core/Services/LiteDbStorageService.cs @@ -2,12 +2,13 @@ using LiteDB; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; +using System; using System.Linq; using System.Threading.Tasks; namespace Bit.Core.Services { - public class LiteDbStorageService : IStorageService + public class LiteDbStorageService : IStorageService, IDisposable { private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings { @@ -16,6 +17,7 @@ namespace Bit.Core.Services private readonly string _dbPath; private ILiteCollection _collection; private Task _initTask; + private LiteDatabase _db; public LiteDbStorageService(string dbPath) { @@ -36,8 +38,8 @@ namespace Bit.Core.Services { try { - var db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;"); - _collection = db.GetCollection("json_items"); + _db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;"); + _collection = _db.GetCollection("json_items"); } finally { @@ -71,6 +73,11 @@ namespace Bit.Core.Services _collection.DeleteMany(i => i.Id == key); } + public void Dispose() + { + _db?.Dispose(); + } + private class JsonItem { public JsonItem() { } diff --git a/src/Core/Utilities/ServiceContainer.cs b/src/Core/Utilities/ServiceContainer.cs index c6a8938d7..8a64c065a 100644 --- a/src/Core/Utilities/ServiceContainer.cs +++ b/src/Core/Utilities/ServiceContainer.cs @@ -113,6 +113,13 @@ namespace Bit.Core.Utilities public static void Reset() { + foreach (var service in RegisteredServices) + { + if (service.Value != null && service.Value is IDisposable disposableService) + { + disposableService.Dispose(); + } + } Inited = false; RegisteredServices.Clear(); RegisteredServices = new Dictionary();