.netasp.net-core-signalrabp-framework

Issue with Sending Notifications from child App to Parent SignalR Hub in ABP.io


Issue with Sending Notifications from Library App to Admin SignalR Hub in ABP.io

I am working on an ABP.io project with two apps:

  1. Admin App: Hosts a SignalR NotificationHub to send notifications to users.
  2. Library App: Embedded in an iframe in the Admin app, where users can upload books. A background job (ExtractBooksCoversJob) extracts book covers after upload.

I want to send a notification from the ExtractBooksCoversJob in the Library App to the Admin SignalR Hub to notify the user once the book covers are successfully extracted.


Admin App Code

Here is the NotificationHub in the Admin app:

namespace Admin
{
    public class NotificationHub : AbpHub
    {
        public async Task SendNotification(string notificationMessage, string url, string userId)
        {
            userId = userId ?? CurrentUser.Id?.ToString();

            await Clients
                .Users(userId!)
                .SendAsync("ReceiveNotification", notificationMessage, url);
        }
    }
}

Library App Code

The background job in the Library App:

namespace Library.Services.Books
{
    public class ExtractBooksCoversJob : AsyncBackgroundJob<ExtractBooksCoversArgs>, ITransientDependency
    {
        private readonly IGdPictureAppService _gdPictureAppService;
        private readonly IConfiguration _configuration;
        private HubConnection _connection;

        public ExtractBooksCoversJob(IGdPictureAppService gdPictureAppService, IConfiguration configuration)
        {
            _gdPictureAppService = gdPictureAppService;
            _configuration = configuration;
        }

        public override async Task ExecuteAsync(ExtractBooksCoversArgs args)
        {
            Logger.LogInformation("Start extracting book covers");

            foreach (var bookId in args.BooksIds)
            {
                var coverId = await _gdPictureAppService.ExtractBookCoverAsync(bookId.ToString());
                Logger.LogInformation("Cover extracted successfully: {0}", coverId);
            }

            // SignalR connection
            _connection = new HubConnectionBuilder()
                .WithUrl(_configuration["SaasUrl"] + "/signalr-hubs/notification")
                .Build();

            await _connection.StartAsync();

            // Attempt to send notification
            await _connection.InvokeAsync(
                "SendNotification", 
                $"Covers extracted successfully: {args.BooksIds.Count}", 
                "/", 
                args.UserId.ToString()
            );

            Logger.LogInformation("All covers extracted successfully");
        }
    }
}

Issue

I receive the following error when the background job tries to invoke SendNotification:

[ERR]Failed to invoke 'SendNotification'due to an error on the server. Microsoft.AspNetCore.SignalR.HubException: Failed to invoke 'SendNotification' due to an error on the server. at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsyncCore(String methodName, ...) at Library.Services.Books.ExtractBooksCoversJob.ExecuteAsync(...)

Debugging Details

The background job logs show that it successfully extracts the book covers. However, the error occurs when invoking SendNotification.

What I Tried

Verified the URL used to connect to the SignalR hub (SaasUrl is correct). Checked that the NotificationHub is properly configured in the Admin app. Logged the UserId being passed to ensure it is not null.

What I Need Help With

How can I successfully invoke the SendNotification method from the Library app to the Admin SignalR Hub?

Any suggestions or insights would be greatly appreciated!


Solution

  • From the error message, we know the issue occurs in server side.

    In this scenario, we should enable asp.net core signalr logging feature to check the error details.

    According to my experience, it is caused by the mismatch of parameter types in SendNotification.