1

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.

1 Answer 1

1

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>();
            ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.