Quantcast
Channel: Building Notification Data for IRealTimeNotifier - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Building Notification Data for IRealTimeNotifier

$
0
0

im using aspNetboilerplate and have implemented a IRealTimeNotifier in order to email users when certain actions are performed in the application layer.

Im 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][1]

My emailRealTimeNotifer is exactly as you see on the site

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

I have then included this in my module pre-initialize:

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

I call the following from my application layer:

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

after injecting the dependancy:

    private readonly EmailRealTimeNotifier _emailNotifier;
EmailRealTimeNotifier emailNotifier
_emailNotifier = emailNotifier;

I have tried to build the UserNotification object prior to passing it to the IRealTimeNotifier however i get an null reference exception error in the try catch of my applciation layer.

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

** Updated as per comments to include actual call and stack trace **

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

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.

it has a message of:

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

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


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images