I am working on an ABP.io project with two apps:
NotificationHub
to send notifications to users.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.
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);
}
}
}
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");
}
}
}
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.
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.
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!
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
.