I have used signalR in my project. There is a hub that users can send notifications to each other. In login part of the project, I want to send notification to the admin when a user is signed in. I need to send this notification from controller, not client side. I tried:
//after dependancy injection
_hubContext.Clients.User(***).SendAsync("LoginIn", "text1", "text2", "text3");
The following error is shown:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.SignalR.IHubContext' while attempting to activate 'CustomizedIdentity.Controllers.HomeController'.
I tried to register IHubContext in Services:
builder.Services.AddScoped<IHubContext<MessageHub>>();
But it does not work.
Update:
private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signInManager;
private readonly IUserRepository _userRepository;
private readonly INotificationRepository _notificationRepository;
private readonly IHubContext _hubContext;
public HomeController(
UserManager<AppUser> userManager,
SignInManager<AppUser> signInManager,
IUserRepository userRepository,
INotificationRepository notificationRepository,
IHubContext hubContext
)
{
_userManager = userManager;
_signInManager = signInManager;
_userRepository = userRepository;
_notificationRepository = notificationRepository;
_hubContext = hubContext;
}