/// <summary> /// Register ui module /// </summary> /// <param name="services"></param> /// <returns></returns> public static INotificationServiceCollection AddNotificationRazorUIModule(this INotificationServiceCollection services) { services.Services.ConfigureOptions(typeof(NotificationRazorFileConfiguration)); MenuEvents.Menu.OnMenuSeed += (sender, args) => { GearApplication.BackgroundTaskQueue.PushBackgroundWorkItemInQueue(async x => { await x.InjectService <IMenuService>() .AppendMenuItemsAsync(new NotificationsMenuInitializer()); }); }; return(services); }
/// <summary> /// Register notificator notification sender /// </summary> /// <param name="services"></param> /// <returns></returns> public static INotificationServiceCollection AddNotificationModuleEvents(this INotificationServiceCollection services) { SystemEvents.Application.OnEvent += (obj, args) => { if (!GearApplication.Configured) { return; } GearApplication.BackgroundTaskQueue.PushBackgroundWorkItemInQueue(async x => { try { if (string.IsNullOrEmpty(args.EventName)) { return; } var service = IoC.Resolve <INotificationSubscriptionService>(); var notifier = IoC.Resolve <INotify <GearRole> >(); var subscribedRoles = await service.GetRolesSubscribedToEventAsync(args.EventName); if (!subscribedRoles.IsSuccess) { return; } var template = await service.GetEventTemplateAsync(args.EventName); if (!template.IsSuccess) { return; } var templateWithParams = template.Result.Value?.Inject(args.EventArgs); //var engine = new RazorLightEngineBuilder() // .UseMemoryCachingProvider() // .Build(); //var templateWithParams = await engine.CompileRenderAsync($"template_{ev.EventName}", template.Result.Value, ev.EventArgs); var notification = new Notification { Subject = template.Result.Subject, Content = templateWithParams, NotificationTypeId = NotificationType.Info }; await notifier.SendNotificationAsync(subscribedRoles.Result, notification, null); } catch (Exception e) { Console.WriteLine(e); } }); }; SystemEvents.Database.OnSeed += async(obj, args) => { if (!(args.DbContext is INotificationSubscriptionsDbContext)) { return; } try { var service = IoC.Resolve <INotificationSubscriptionService>(); await service.SeedEventsAsync(); } catch (Exception e) { Console.WriteLine(e); } }; return(services); }
/// <summary> /// Register module storage /// </summary> /// <typeparam name="TContext"></typeparam> /// <param name="services"></param> /// <param name="options"></param> /// <returns></returns> public static INotificationServiceCollection AddNotificationSubscriptionModuleStorage <TContext>(this INotificationServiceCollection services, Action <DbContextOptionsBuilder> options) where TContext : DbContext, INotificationSubscriptionsDbContext { Arg.NotNull(services, nameof(AddNotificationSubscriptionModuleStorage)); services.Services.AddDbContext <TContext>(options); services.Services.AddScopedContextFactory <INotificationSubscriptionsDbContext, TContext>(); services.Services.RegisterAuditFor <INotificationSubscriptionsDbContext>($"{nameof(Notification)} module"); SystemEvents.Database.OnAllMigrate += (sender, args) => { GearApplication.GetHost <IWebHost>().MigrateDbContext <TContext>(); }; return(services); }
/// <summary> /// Add SignalR /// </summary> /// <param name="services"></param> /// <param name="path"></param> /// <returns></returns> public static INotificationServiceCollection RegisterNotificationsHubModule <TCommunicationHub>(this INotificationServiceCollection services, string path = "/rtn") where TCommunicationHub : class, ICommunicationHub { Arg.NotNull(services, nameof(services)); services.Services.AddGearSingleton <ICommunicationHub, TCommunicationHub>(); services.Services.AddSignalR(options => { options.EnableDetailedErrors = true; }); services.Services.AddHealthChecks() .AddSignalRHub(GearApplication.SystemConfig.EntryUri + path.Substring(1), "signalr-hub"); return(services); }