From 8bb62a5158090bcb22dd867982aafd7fe43415fb Mon Sep 17 00:00:00 2001 From: colin Date: Sat, 8 Nov 2025 16:41:33 +0800 Subject: [PATCH] fix(notifications): Fix webhook notifications null object references --- .../Webhook/WebhookNotificationContext.cs | 3 +- .../WebhookNotificationPublishProvider.cs | 42 ++++++++++++------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationContext.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationContext.cs index f8d427640..0f351a71e 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationContext.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationContext.cs @@ -7,8 +7,9 @@ public class WebhookNotificationContext : IWebhookNotificationContext public NotificationInfo Notification { get; } public WebhookNotificationData Webhook { get; set; } public bool Handled { get; set; } - public WebhookNotificationContext(IServiceProvider ServiceProvider, NotificationInfo notification) + public WebhookNotificationContext(IServiceProvider serviceProvider, NotificationInfo notification) { + ServiceProvider = serviceProvider; Notification = notification; } public bool HasResolved() diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationPublishProvider.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationPublishProvider.cs index a7441d328..6aaa905b8 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationPublishProvider.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationPublishProvider.cs @@ -1,9 +1,11 @@ using LINGYUN.Abp.Webhooks; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using Volo.Abp.Features; namespace LINGYUN.Abp.Notifications.Webhook; public class WebhookNotificationPublishProvider : NotificationPublishProvider @@ -11,38 +13,48 @@ public class WebhookNotificationPublishProvider : NotificationPublishProvider public const string ProviderName = "Webhook"; public override string Name => ProviderName; - private readonly IServiceScopeFactory _serviceScopeFactory; - private readonly AbpNotificationsWebhookOptions _options; - private readonly IWebhookPublisher _webhookPublisher; + protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService(); + protected IWebhookPublisher WebhookPublisher => ServiceProvider.LazyGetRequiredService(); + protected IServiceScopeFactory ServiceScopeFactory => ServiceProvider.LazyGetRequiredService(); + protected IOptions Options => ServiceProvider.LazyGetRequiredService>(); - public WebhookNotificationPublishProvider( - IServiceScopeFactory serviceScopeFactory, - IWebhookPublisher webhookPublisher, - IOptions options) + protected override Task CanPublishAsync(NotificationInfo notification, CancellationToken cancellationToken = default) { - _serviceScopeFactory = serviceScopeFactory; - _webhookPublisher = webhookPublisher; - _options = options.Value; + if (Options.Value.Contributors.Count == 0) + { + Logger.LogWarning("The Webhook notification publishing contributor is empty, and the Webhook notification cannot be sent!"); + return Task.FromResult(false); + } + + return base.CanPublishAsync(notification, cancellationToken); } - protected override async Task PublishAsync(NotificationInfo notification, IEnumerable identifiers, CancellationToken cancellationToken = default) + protected async override Task PublishAsync(NotificationInfo notification, IEnumerable identifiers, CancellationToken cancellationToken = default) { - using var scope = _serviceScopeFactory.CreateScope(); + using var scope = ServiceScopeFactory.CreateScope(); - foreach (var contributor in _options.Contributors) + foreach (var contributor in Options.Value.Contributors) { var context = new WebhookNotificationContext(scope.ServiceProvider, notification); await contributor.ContributeAsync(context); - if (context.HasResolved()) + if (!context.HasResolved()) { - await _webhookPublisher.PublishAsync( + Logger.LogWarning("The Webhook notifies the contributor: {0} that the Webhook data for the given notification: {1} cannot be parsed. Skip it.", + contributor.Name, notification.Name); + continue; + } + else + { + await WebhookPublisher.PublishAsync( context.Webhook.WebhookName, context.Webhook.Data, notification.TenantId, context.Webhook.SendExactSameData, context.Webhook.Headers); + + Logger.LogDebug("The webhook: {webhookName} with contributor: {name} has successfully published!", context.Webhook.WebhookName, Name); } } }