in my case, messsage are come from internal events. no new request.
is there a way to create an Hub and send data to clients?
storing and resuse old Hub are crached with
'object is disposed..'
)
old frameworks has GlobalContext..
I have tried somthing like :
DataHub _hub =(DataHub) ServiceProvider.GetService(typeof(DataHub));
_hub.Clients.All.SendAsync("method");
but ServiceProvider also disposed.
CodePudding user response:
based on https://www.c-sharpcorner.com/article/signal-r-with-asp-net-core-hosted-service-net-5/
the solution is using BackgroundService with Dependency Injection of IHubContext
this instance has long live (for my undestand ,while BackgroundService does not disposed).
public class HubMessageService : BackgroundService
{
IHubContext<DataHub> _hubContext;
public HubMessageService(IHubContext<DataHub> hubContext)
{
_hubContext = hubContext ;
}
in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddHostedService<HubMessageService>();
...