From 8d37f1c9466bfca60a8ab1635efd9cbbfaea717f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 28 Apr 2017 16:10:27 -0400 Subject: [PATCH] adjust service lifetimes --- src/Core/ServiceCollectionExtensions.cs | 14 +++++++------- .../Implementations/PushSharpPushService.cs | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Core/ServiceCollectionExtensions.cs b/src/Core/ServiceCollectionExtensions.cs index d351551e68..0af68ce060 100644 --- a/src/Core/ServiceCollectionExtensions.cs +++ b/src/Core/ServiceCollectionExtensions.cs @@ -25,23 +25,23 @@ namespace Bit.Core { services.AddSingleton(); services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); } public static void AddDefaultServices(this IServiceCollection services) { services.AddSingleton(); - services.AddScoped(); - services.AddScoped(); + services.AddSingleton(); + services.AddSingleton(); } public static void AddNoopServices(this IServiceCollection services) { services.AddSingleton(); - services.AddScoped(); - services.AddScoped(); + services.AddSingleton(); + services.AddSingleton(); } } } diff --git a/src/Core/Services/Implementations/PushSharpPushService.cs b/src/Core/Services/Implementations/PushSharpPushService.cs index 652c98079a..7c8af781ed 100644 --- a/src/Core/Services/Implementations/PushSharpPushService.cs +++ b/src/Core/Services/Implementations/PushSharpPushService.cs @@ -14,6 +14,7 @@ using Newtonsoft.Json; using Microsoft.Extensions.Logging; using System.Diagnostics; using Bit.Core.Utilities; +using Microsoft.AspNetCore.Http; namespace Bit.Core.Services { @@ -21,20 +22,20 @@ namespace Bit.Core.Services { private readonly IDeviceRepository _deviceRepository; private readonly ILogger _logger; - private readonly CurrentContext _currentContext; + private readonly IHttpContextAccessor _httpContextAccessor; private GcmServiceBroker _gcmBroker; private ApnsServiceBroker _apnsBroker; public PushSharpPushService( IDeviceRepository deviceRepository, + IHttpContextAccessor httpContextAccessor, ILogger logger, - CurrentContext currentContext, IHostingEnvironment hostingEnvironment, GlobalSettings globalSettings) { _deviceRepository = deviceRepository; + _httpContextAccessor = httpContextAccessor; _logger = logger; - _currentContext = currentContext; InitGcmBroker(globalSettings); InitApnsBroker(globalSettings, hostingEnvironment); @@ -96,9 +97,11 @@ namespace Bit.Core.Services }; var excludedTokens = new List(); - if(!string.IsNullOrWhiteSpace(_currentContext.DeviceIdentifier)) + var currentContext = _httpContextAccessor?.HttpContext?. + RequestServices.GetService(typeof(CurrentContext)) as CurrentContext; + if(!string.IsNullOrWhiteSpace(currentContext?.DeviceIdentifier)) { - excludedTokens.Add(_currentContext.DeviceIdentifier); + excludedTokens.Add(currentContext.DeviceIdentifier); } await PushToAllUserDevicesAsync(cipher.UserId.Value, JObject.FromObject(message), excludedTokens); @@ -116,9 +119,11 @@ namespace Bit.Core.Services }; var excludedTokens = new List(); - if(!string.IsNullOrWhiteSpace(_currentContext.DeviceIdentifier)) + var currentContext = _httpContextAccessor?.HttpContext?. + RequestServices.GetService(typeof(CurrentContext)) as CurrentContext; + if(!string.IsNullOrWhiteSpace(currentContext?.DeviceIdentifier)) { - excludedTokens.Add(_currentContext.DeviceIdentifier); + excludedTokens.Add(currentContext.DeviceIdentifier); } await PushToAllUserDevicesAsync(folder.UserId, JObject.FromObject(message), excludedTokens);