为IRealTimeNotifier构建通知数据 [英] Building Notification Data for IRealTimeNotifier

查看:207
本文介绍了为IRealTimeNotifier构建通知数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using ASP.NET Boilerplate and have implemented an IRealTimeNotifier in order to email users when certain actions are performed in the application layer.

按照下一页中的示例,我在构建通知数据以传递给IRealTimeNotifer时遇到了麻烦:

I'm having trouble building the notification data to pass to the IRealTimeNotifer as per the example on the following page:

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

我的EmailRealTimeNotifier"与您在页面上看到的完全相同:

My EmailRealTimeNotifier is exactly as you see on the page:

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中:

I have then included this in my module PreInitialize:

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

我从应用程序层调用以下内容:

I call the following from my application layer:

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

注入依赖项后:

public class MyAppService : IMyAppService
{
    private readonly EmailRealTimeNotifier _emailNotifier;

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

在将UserNotification对象传递给IRealTimeNotifier之前,我曾尝试构建它.但是,在我的应用程序层的try-catch中出现了空引用异常错误.

I have tried to build the UserNotification object prior to passing it to the IRealTimeNotifier. However, I get a null reference exception error in the try-catch of my application layer.

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

Could someone please suggest how I could build this UserNotification or indicate the correct way of accessing this feature?

实际调用和堆栈跟踪:

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;
}

Stacktrace是:

Stacktrace is:

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息: 执行操作方法ESMPortal.Users.UserAppService.Update (ESMPortal.Application)和参数(ESMPortal.Users.Dto.UserDto)- 验证状态:抛出有效异常: ESMPortal.Application.dll中的'System.NullReferenceException' 'dotnet.exe'(CoreCLR:clrhost):已加载'C:\ Program Files \ dotnet \ shared \ Microsoft.NETCore.App \ 2.2.4 \ System.Diagnostics.StackTrace.dll'. 跳过的加载符号.模块已优化,调试器选项 仅我的代码"已启用. 'dotnet.exe'(CoreCLR:clrhost):已加载 'C:\ Program Files \ dotnet \ shared \ Microsoft.NETCore.App \ 2.2.4 \ System.Reflection.Metadata.dll". 跳过的加载符号.模块已优化,调试器选项 仅我的代码"已启用. 'dotnet.exe'(CoreCLR:clrhost):已加载 'C:\ Program Files \ dotnet \ shared \ Microsoft.NETCore.App \ 2.2.4 \ System.IO.MemoryMappedFiles.dll". 跳过的加载符号.模块已优化,调试器选项 启用了仅我的代码".

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.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.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.2.4\System.IO.MemoryMappedFiles.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

它的信息是:

Message =对象引用未设置为对象的实例."

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

RealTimeNotifier类顶部的断点永远不会触发.

The breakpoint at the top of my RealTimeNotifier class is never triggered.

当我执行userNotification.Notification.Data = messageNotificationData;时,上面的内容就是一个例外.

The above hits the exception when I do userNotification.Notification.Data = messageNotificationData;.

推荐答案

IRealTimeNotifier是指在调用_notificationPublisher.PublishAsync时由ABP调用.

IRealTimeNotifier is meant to be called by ABP when you call _notificationPublisher.PublishAsync.

这会在数据库的 AbpNotifications 表中创建一个通知.

This creates a notification in AbpNotifications table in the database.

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

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

根据您的情况,您可以直接致电_emailSender.

In your case, you can call _emailSender directly.

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
);

这篇关于为IRealTimeNotifier构建通知数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆