public async Task SendNotification(NotificationScope scope, BaseNotification notification) { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } if (notification == null) { throw new ArgumentNullException(nameof(notification)); } var users = await GetUsers(scope); using (var db = _contextFactory.CreateContext()) { foreach (var uid in users) { var obj = JsonConvert.SerializeObject(notification); var data = new NotificationData { Id = Guid.NewGuid().ToString(), Created = DateTimeOffset.UtcNow, ApplicationUserId = uid, NotificationType = GetNotificationTypeString(notification.GetType()), Blob = ZipUtils.Zip(obj), Seen = false }; db.Notifications.Add(data); } await db.SaveChangesAsync(); } }
public async Task PublishAsync(BaseNotification notification) { _logger.LogInformation("Publish notification: {Notification}", notification.GetType().Name); var wrappedNotification = notification.Wrap(); await PublishConsoleAsync(wrappedNotification); await PublishSocketsAsync(wrappedNotification); }
public static string ToJson(this BaseNotification baseNotification) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(baseNotification.GetType()); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { serializer.WriteObject(ms, baseNotification); return(Encoding.UTF8.GetString(ms.ToArray())); } }
public static INotification Wrap(this BaseNotification notification) { var genericType = typeof(WrapperNotification <>).MakeGenericType(notification.GetType()); return((Activator.CreateInstance(genericType, notification) as INotification) !); }