为 IRealTimeNotifier 构建通知数据

Building Notification Data for IRealTimeNotifier

我正在使用 ASP.NET 样板并实现了 IRealTimeNotifier,以便在应用层执行某些操作时向用户发送电子邮件。

我在按照下页中的示例构建要传递给 IRealTimeNotifer 的通知数据时遇到问题:

https://aspnetboilerplate.com/Pages/Documents/Notification-System#multiple-notifiers

我的 EmailRealTimeNotifier 与您在页面上看到的完全一样:

public class EmailRealTimeNotifier : IRealTimeNotifier, ITransientDependency
{
    private readonly IEmailSender _emailSender;
    private readonly UserManager _userManager;

    public EmailRealTimeNotifier(
        IEmailSender emailSender,
        UserManager userManager)
    {
        _emailSender = emailSender;
        _userManager = userManager;
    }

    public async Task SendNotificationsAsync(UserNotification[] userNotifications)
    {
        foreach (var userNotification in userNotifications)
        {
            if (userNotification.Notification.Data is MessageNotificationData data)
            {
                var user = await _userManager.GetUserByIdAsync(userNotification.UserId);
                
                _emailSender.Send(
                    to: user.EmailAddress,
                    subject: "You have a new notification!",
                    body: data.Message,
                    isBodyHtml: true
                );
            }
        }
    }
}

然后我将其包含在我的模块 PreInitialize 中:

Configuration.Notifications.Notifiers.Add<EmailRealTimeNotifier>();

我从我的应用层调用以下内容:

await _emailNotifier.SendNotificationsAsync(userNotification.ToArray());

注入依赖后:

public class MyAppService : IMyAppService
{
    private readonly EmailRealTimeNotifier _emailNotifier;

    public MyAppService(EmailRealTimeNotifier emailNotifier)
    {
        _emailNotifier = emailNotifier;
    }
}

我尝试在将 UserNotification 对象传递给 IRealTimeNotifier 之前构建它。但是,我在应用层的try-catch中得到一个空引用异常错误。

有人可以建议我如何构建此 UserNotification 或指出访问此功能的正确方法吗?


实际调用和堆栈跟踪:

try
{
    var userNotificationList = new List<UserNotification>();
    var userNotification = new UserNotification();
    var notificationData = new NotificationData();
    var messageNotificationData = new MessageNotificationData("Test");

    userNotification.UserId = (long)AbpSession.UserId;
    userNotification.Notification.Data = messageNotificationData;

    userNotificationList.Add(userNotification);

    await _emailNotifier.SendNotificationsAsync(userNotificationList.ToArray());
}
catch (Exception e)
{
    var test = e;
    throw;
}

堆栈跟踪是:

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executing action method ESMPortal.Users.UserAppService.Update (ESMPortal.Application) with arguments (ESMPortal.Users.Dto.UserDto) - Validation state: Valid Exception thrown: 'System.NullReferenceException' in ESMPortal.Application.dll 'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App.2.4\System.Diagnostics.StackTrace.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App.2.4\System.Reflection.Metadata.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App.2.4\System.IO.MemoryMappedFiles.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

它有一条消息:

Message = "Object reference not set to an instance of an object."

我的 RealTimeNotifier class 顶部的断点从未被触发。

当我执行 userNotification.Notification.Data = messageNotificationData;.

以上操作时出现异常

IRealTimeNotifier 是指当您调用 _notificationPublisher.PublishAsync.

时由 ABP 调用

这会在数据库的 AbpNotifications table 中创建一个通知。

var notificationName = "NotificationName";
var message = "Test";
var userIds = new [] { AbpSession.ToUserIdentifier() };

await _notificationPublisher.PublishAsync(
    notificationName,
    data: new MessageNotificationData(message),
    userIds: userIds
);

对于您的情况,您可以直接调用 _emailSender

var user = await _userManager.GetUserByIdAsync(AbpSession.UserId.Value);
var message = "Test";

_emailSender.Send(
    to: user.EmailAddress,
    subject: "You have a new notification!",
    body: message,
    isBodyHtml: true
);