title: How to implement Multiple Notifiers with ASP.NET Boilerplate?
body: <p>I'm using ASP.NET Boilerplate (with .NET Core).
  I'm trying to implement notification system as mentioned in <a href="https://aspnetboilerplate.com/Pages/Documents/Notification-System#real-time-notifications" rel="nofollow noreferrer">official doc</a> under <a href="https://aspnetboilerplate.com/Pages/Documents/Notification-System#multiple-notifiers" rel="nofollow noreferrer">Multiple Notifiers</a>, but I encountered:</p>
  <blockquote>
  <p>System.NullReferenceException: 'Object reference not set to an instance of an object.'</p>
  </blockquote>
  <p>My question is not related to exception itself. I want to know if there are any pre-requests configuration or setting before I implement the example, or if my code misses something:</p>
  <pre><code>public class MessageAppService: AsyncCrudAppService&lt;Message, MessageDto, long, PagedAndSortedResultRequestDto, CreateMessageDto, UpdateMessageDto, ReadMessageDto&gt;, IRealTimeNotifier, ITransientDependency
  {
      private readonly IRepository&lt;Message, long&gt; _repository;
  
      private readonly IDbContextProvider&lt;MySystemDbContext&gt; _dbContextProvider;
      private MySystemDbContext db =&gt; _dbContextProvider.GetDbContext();
  
      IHubContext&lt;MyChatHub&gt; _hubContext; // This for online chat
  
      private readonly IEmailSender _emailSender;
      private readonly UserManager _userManager;
  
      private readonly INotificationSubscriptionManager _notificationSubscriptionManager;
  
      public MessageAppService(
          IDbContextProvider&lt;MySystemDbContext&gt; dbContextProvider,
          IRepository&lt;Message, long&gt; repository, 
          IHubContext&lt;MyChatHub&gt; hubContext /* This for online chat */,
          INotificationSubscriptionManager notificationSubscriptionManager,
          IEmailSender emailSender,
          UserManager userManager
      ) : base(repository)
      {
          _repository = repository;
          _dbContextProvider = dbContextProvider;
          _hubContext = hubContext; // This for online chat 
          _notificationSubscriptionManager = notificationSubscriptionManager;
          _emailSender = emailSender;
          _userManager = userManager;
      }
  
      public override async Task&lt;MessageDto&gt; CreateAsync(CreateMessageDto input)
      {
          var test = await base.CreateAsync(input);
          UserNotification userNotifications = new UserNotification();
          userNotifications.UserId = 10010;
          //
          userNotifications.Notification.TenantId = AbpSession.TenantId;
          userNotifications.Notification.NotificationName = &quot;test&quot;;
  
          UserNotification[] notificationList = new UserNotification[1];
          notificationList[0] = userNotifications;
          await SendNotificationsAsync(notificationList);
          return test;
      }
  
      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: &quot;testg@gmail.com&quot;,
                      subject: &quot;You have a new notification!&quot;,
                      body: data.Message,
                      isBodyHtml: true
                  );
              }
          }
      }
  
      void IRealTimeNotifier.SendNotifications(UserNotification[] userNotifications)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  <p>I've added below in PreInitialize() function in .Application module:</p>
  <pre><code>Configuration.Notifications.Notifiers.Add&lt;MessageAppService&gt;();
  </code></pre>
  <hr />
  <p><strong>Update</strong>:</p>
  <p>I know how to avoid this exception but I want to know if the steps that I've implemented in my code are enough to get the notification.</p>
  <hr />
  <p><strong>Update 2:</strong></p>
  <p>I've applied the code in the question mentioned by <strong>aaron</strong>.
  I got another exception:</p>
  <pre><code>WARN  2020-07-05 13:30:06,907 [9    ] fications.DefaultNotificationDistributer - System.Net.Mail.SmtpException: Failure sending mail.
   ---&gt; System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10061): No connection could be made because the target machine actively refused it. [::ffff:127.0.0.1]:25
     at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
     at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
     at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpClient.GetConnection()
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     --- End of inner exception stack trace ---
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     at Abp.Net.Mail.Smtp.SmtpEmailSender.SendEmail(MailMessage mail)
     at Abp.Net.Mail.EmailSenderBase.Send(MailMessage mail, Boolean normalize)
     at Abp.Net.Mail.EmailSenderBase.Send(String to, String subject, String body, Boolean isBodyHtml)
     at MySystem.ChatAppService.MessageAppService.SendNotificationsAsync(UserNotification[] userNotifications) in D:\WorkSpace\MySystem\aspnet-core\src\MySystem.Application\ChatAppService\MessageAppService.cs:line 172
     at Abp.Notifications.DefaultNotificationDistributer.NotifyAsync(UserNotification[] userNotifications)
  System.Net.Mail.SmtpException: Failure sending mail.
   ---&gt; System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10061): No connection could be made because the target machine actively refused it. [::ffff:127.0.0.1]:25
     at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
     at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
     at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpClient.GetConnection()
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     --- End of inner exception stack trace ---
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     at Abp.Net.Mail.Smtp.SmtpEmailSender.SendEmail(MailMessage mail)
     at Abp.Net.Mail.EmailSenderBase.Send(MailMessage mail, Boolean normalize)
     at Abp.Net.Mail.EmailSenderBase.Send(String to, String subject, String body, Boolean isBodyHtml)
     at MySystem.ChatAppService.MessageAppService.SendNotificationsAsync(UserNotification[] userNotifications) in D:\WorkSpace\MySystem\aspnet-core\src\MySystem.Application\ChatAppService\MessageAppService.cs:line 172
     at Abp.Notifications.DefaultNotificationDistributer.NotifyAsync(UserNotification[] userNotifications)
  WARN  2020-07-05 13:30:09,049 [10   ] fications.DefaultNotificationDistributer - System.Net.Mail.SmtpException: Failure sending mail.
   ---&gt; System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10061): No connection could be made because the target machine actively refused it. [::ffff:127.0.0.1]:25
     at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
     at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
     at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpClient.GetConnection()
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     --- End of inner exception stack trace ---
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     at Abp.Net.Mail.Smtp.SmtpEmailSender.SendEmail(MailMessage mail)
     at Abp.Net.Mail.EmailSenderBase.Send(MailMessage mail, Boolean normalize)
     at Abp.Net.Mail.EmailSenderBase.Send(String to, String subject, String body, Boolean isBodyHtml)
     at MySystem.ChatAppService.MessageAppService.SendNotificationsAsync(UserNotification[] userNotifications) in D:\WorkSpace\MySystem\aspnet-core\src\MySystem.Application\ChatAppService\MessageAppService.cs:line 172
     at Abp.Notifications.DefaultNotificationDistributer.NotifyAsync(UserNotification[] userNotifications)
  System.Net.Mail.SmtpException: Failure sending mail.
   ---&gt; System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10061): No connection could be made because the target machine actively refused it. [::ffff:127.0.0.1]:25
     at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
     at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
     at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpClient.GetConnection()
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     --- End of inner exception stack trace ---
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     at Abp.Net.Mail.Smtp.SmtpEmailSender.SendEmail(MailMessage mail)
     at Abp.Net.Mail.EmailSenderBase.Send(MailMessage mail, Boolean normalize)
     at Abp.Net.Mail.EmailSenderBase.Send(String to, String subject, String body, Boolean isBodyHtml)
     at MySystem.ChatAppService.MessageAppService.SendNotificationsAsync(UserNotification[] userNotifications) in D:\WorkSpace\MySystem\aspnet-core\src\MySystem.Application\ChatAppService\MessageAppService.cs:line 172
     at Abp.Notifications.DefaultNotificationDistributer.NotifyAsync(UserNotification[] userNotifications)
  ERROR 2020-07-05 13:30:28,176 [10   ] Mvc.ExceptionHandling.AbpExceptionFilter - Failure sending mail.
  System.Net.Mail.SmtpException: Failure sending mail.
   ---&gt; System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10061): No connection could be made because the target machine actively refused it. [::ffff:127.0.0.1]:25
     at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
     at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
     at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
     at System.Net.Mail.SmtpClient.GetConnection()
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     --- End of inner exception stack trace ---
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     at Abp.Net.Mail.Smtp.SmtpEmailSender.SendEmail(MailMessage mail)
     at Abp.Net.Mail.EmailSenderBase.Send(MailMessage mail, Boolean normalize)
     at Abp.Net.Mail.EmailSenderBase.Send(String to, String subject, String body, Boolean isBodyHtml)
     at MySystem.ChatAppService.MessageAppService.CreateAsync(CreateMessageDto input) in D:\WorkSpace\MySystem\aspnet-core\src\MySystem.Application\ChatAppService\MessageAppService.cs:line 153
     at Abp.Authorization.AuthorizationInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation)
     at Abp.Domain.Uow.UnitOfWorkInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation)
     at Abp.Auditing.AuditingInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation)
     at Abp.Runtime.Validation.Interception.ValidationInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation)
     at lambda_method(Closure , Object )
     at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
     at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeActionMethodAsync&gt;g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeNextActionFilterAsync&gt;g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeInnerFilterAsync&gt;g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextExceptionFilterAsync&gt;g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
  </code></pre>