From 313879e52b7e80490860470885f2b2da973bbf8d Mon Sep 17 00:00:00 2001 From: Nabil Sedoud Date: Mon, 31 Aug 2020 09:42:34 +0100 Subject: [PATCH] fix disposing of direct instantiated objects in calalog service #1392 (#1395) --- .../Services/IntegrationEventLogService.cs | 23 ++++++++++++++++++- .../CatalogIntegrationEventService.cs | 22 +++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs index 800b99a38..22c5e1fba 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs @@ -15,11 +15,12 @@ using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services { - public class IntegrationEventLogService : IIntegrationEventLogService + public class IntegrationEventLogService : IIntegrationEventLogService,IDisposable { private readonly IntegrationEventLogContext _integrationEventLogContext; private readonly DbConnection _dbConnection; private readonly List _eventTypes; + private volatile bool disposedValue; public IntegrationEventLogService(DbConnection dbConnection) { @@ -89,5 +90,25 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi return _integrationEventLogContext.SaveChangesAsync(); } + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + _integrationEventLogContext?.Dispose(); + } + + + disposedValue = true; + } + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } } diff --git a/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs index bb3a23d40..3e7ba9868 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs @@ -14,13 +14,14 @@ using System.Threading.Tasks; namespace Catalog.API.IntegrationEvents { - public class CatalogIntegrationEventService : ICatalogIntegrationEventService + public class CatalogIntegrationEventService : ICatalogIntegrationEventService,IDisposable { private readonly Func _integrationEventLogServiceFactory; private readonly IEventBus _eventBus; private readonly CatalogContext _catalogContext; private readonly IIntegrationEventLogService _eventLogService; private readonly ILogger _logger; + private volatile bool disposedValue; public CatalogIntegrationEventService( ILogger logger, @@ -65,5 +66,24 @@ namespace Catalog.API.IntegrationEvents await _eventLogService.SaveEventAsync(evt, _catalogContext.Database.CurrentTransaction); }); } + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + (_eventLogService as IDisposable)?.Dispose(); + } + + disposedValue = true; + } + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } }