diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj
index 5b0d584d61..69d6fe966e 100644
--- a/src/Core/Core.csproj
+++ b/src/Core/Core.csproj
@@ -21,6 +21,7 @@
+
@@ -44,10 +45,6 @@
-
-
-
-
diff --git a/src/Core/Services/Implementations/MultiServicePushNotificationService.cs b/src/Core/Services/Implementations/MultiServicePushNotificationService.cs
index ececb16581..c718f55e02 100644
--- a/src/Core/Services/Implementations/MultiServicePushNotificationService.cs
+++ b/src/Core/Services/Implementations/MultiServicePushNotificationService.cs
@@ -30,9 +30,7 @@ namespace Bit.Core.Services
}
else
{
-#if NET471
_services.Add(new NotificationHubPushNotificationService(globalSettings, httpContextAccessor));
-#endif
// _services.Add(new AzureQueuePushNotificationService(globalSettings, httpContextAccessor));
}
}
diff --git a/src/Core/Services/Implementations/NotificationHubPushNotificationService.cs b/src/Core/Services/Implementations/NotificationHubPushNotificationService.cs
index 628082470c..c0a7e8c35e 100644
--- a/src/Core/Services/Implementations/NotificationHubPushNotificationService.cs
+++ b/src/Core/Services/Implementations/NotificationHubPushNotificationService.cs
@@ -1,5 +1,4 @@
-#if NET471
-using System;
+using System;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
using Microsoft.Azure.NotificationHubs;
@@ -13,16 +12,17 @@ namespace Bit.Core.Services
{
public class NotificationHubPushNotificationService : IPushNotificationService
{
- private readonly NotificationHubClient _client;
+ private readonly GlobalSettings _globalSettings;
private readonly IHttpContextAccessor _httpContextAccessor;
+ private NotificationHubClient _client = null;
+ private DateTime? _clientExpires = null;
+
public NotificationHubPushNotificationService(
GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor)
{
- _client = NotificationHubClient.CreateClientFromConnectionString(globalSettings.NotificationHub.ConnectionString,
- globalSettings.NotificationHub.HubName);
-
+ _globalSettings = globalSettings;
_httpContextAccessor = httpContextAccessor;
}
@@ -170,13 +170,25 @@ namespace Bit.Core.Services
private async Task SendPayloadAsync(string tag, PushType type, object payload)
{
- await _client.SendTemplateNotificationAsync(
+ await RenewClientAndExecuteAsync(async client => await client.SendTemplateNotificationAsync(
new Dictionary
{
{ "type", ((byte)type).ToString() },
{ "payload", JsonConvert.SerializeObject(payload) }
- }, tag);
+ }, tag));
+ }
+
+ private async Task RenewClientAndExecuteAsync(Func task)
+ {
+ var now = DateTime.UtcNow;
+ if(_client == null || !_clientExpires.HasValue || _clientExpires.Value < now)
+ {
+ _clientExpires = now.Add(TimeSpan.FromMinutes(30));
+ _client = NotificationHubClient.CreateClientFromConnectionString(
+ _globalSettings.NotificationHub.ConnectionString,
+ _globalSettings.NotificationHub.HubName);
+ }
+ await task(_client);
}
}
}
-#endif
diff --git a/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs b/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs
index b339ae4c44..f132207642 100644
--- a/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs
+++ b/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs
@@ -1,5 +1,4 @@
-#if NET471
-using System.Collections.Generic;
+using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.NotificationHubs;
using Bit.Core.Enums;
@@ -10,13 +9,15 @@ namespace Bit.Core.Services
{
public class NotificationHubPushRegistrationService : IPushRegistrationService
{
- private readonly NotificationHubClient _client;
+ private readonly GlobalSettings _globalSettings;
+
+ private NotificationHubClient _client = null;
+ private DateTime? _clientExpires = null;
public NotificationHubPushRegistrationService(
GlobalSettings globalSettings)
{
- _client = NotificationHubClient.CreateClientFromConnectionString(globalSettings.NotificationHub.ConnectionString,
- globalSettings.NotificationHub.HubName);
+ _globalSettings = globalSettings;
}
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
@@ -76,9 +77,11 @@ namespace Bit.Core.Services
BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier);
BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier);
- BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate, userId, identifier);
+ BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate,
+ userId, identifier);
- await _client.CreateOrUpdateInstallationAsync(installation);
+ await RenewClientAndExecuteAsync(async client =>
+ await client.CreateOrUpdateInstallationAsync(installation));
}
private void BuildInstallationTemplate(Installation installation, string templateId, string templateBody,
@@ -113,7 +116,7 @@ namespace Bit.Core.Services
{
try
{
- await _client.DeleteInstallationAsync(deviceId);
+ await RenewClientAndExecuteAsync(async client => await client.DeleteInstallationAsync(deviceId));
}
catch(Exception e)
{
@@ -135,7 +138,8 @@ namespace Bit.Core.Services
$"organizationId:{organizationId}");
}
- private async Task PatchTagsForUserDevicesAsync(IEnumerable deviceIds, UpdateOperationType op, string tag)
+ private async Task PatchTagsForUserDevicesAsync(IEnumerable deviceIds, UpdateOperationType op,
+ string tag)
{
if(!deviceIds.Any())
{
@@ -161,7 +165,8 @@ namespace Bit.Core.Services
{
try
{
- await _client.PatchInstallationAsync(id, new List { operation });
+ await RenewClientAndExecuteAsync(async client =>
+ await client.PatchInstallationAsync(id, new List { operation }));
}
catch(Exception e)
{
@@ -172,6 +177,18 @@ namespace Bit.Core.Services
}
}
}
+
+ private async Task RenewClientAndExecuteAsync(Func task)
+ {
+ var now = DateTime.UtcNow;
+ if(_client == null || !_clientExpires.HasValue || _clientExpires.Value < now)
+ {
+ _clientExpires = now.Add(TimeSpan.FromMinutes(30));
+ _client = NotificationHubClient.CreateClientFromConnectionString(
+ _globalSettings.NotificationHub.ConnectionString,
+ _globalSettings.NotificationHub.HubName);
+ }
+ await task(_client);
+ }
}
}
-#endif
diff --git a/src/Core/Utilities/ServiceCollectionExtensions.cs b/src/Core/Utilities/ServiceCollectionExtensions.cs
index 09b04e4d54..4e3f54c3d4 100644
--- a/src/Core/Utilities/ServiceCollectionExtensions.cs
+++ b/src/Core/Utilities/ServiceCollectionExtensions.cs
@@ -91,12 +91,10 @@ namespace Bit.Core.Utilities
{
services.AddSingleton();
}
-#if NET471
else if(!globalSettings.SelfHosted)
{
services.AddSingleton();
}
-#endif
else
{
services.AddSingleton();