예제 #1
0
        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();
            }
        }
예제 #2
0
        public async Task PublishAsync(BaseNotification notification)
        {
            _logger.LogInformation("Publish notification: {Notification}", notification.GetType().Name);
            var wrappedNotification = notification.Wrap();

            await PublishConsoleAsync(wrappedNotification);
            await PublishSocketsAsync(wrappedNotification);
        }
예제 #3
0
        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) !);
        }