1
votes

I have a self hosted webApi project in a desktop C# application. This uses Microsoft.Owin.Hosting.WebApp.

I have a Hub derived class that has a send and a receive from an angular client.

The hub is instanced by the WebApp.start. I find that it is short lived. When the client sends a message, the hub is instanced and then disposed. I need to send message to the client, but I don't have an instance of the hub to send a message in the other direction.

At one point, I was holding a reference to the instance (as in memory leak) and I could get a message to the client.

The client shows as it is always connected. No disconnected messages.

What am I missing?

1

1 Answers

0
votes

You should not hold this instance by yourself and you shold never create an instance by yourself.

Read the details about the hub onject life time here: https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#hub-object-lifetime

In the case you need to send a message to the connected clients you need to use GetHubContext.

Example:

var context = GlobalHost.ConnectionManager.GetHubContext<yourHub>();
context.Clients.All.Send("Something");

(In the case you are using core signalr read: Call SignalR Core Hub method from Controller)