1

Couple of my API controllers need to have access to Hub Context and I know that the GlobalHost.ConnectionManager.GetHubContext call is expensive. Is there an efficient way to do that?

1 Answer 1

3

How do you know the GlobalHost.ConnectionManager.GetHubContext call is expensive?

I just tried calling GetHubContext 10,000 times in a loop and that took 174 milliseconds on my machine. That means each call averaged 0.0000174 seconds. I don't think that is particularly expensive in the context of most Web API calls or web request in general.

If the very first call to GetHubContext is made before any SignalR connections are established, then that the initial call will take longer. On my machine, this first call took 96 milliseconds. This time was spent initializing some SignalR singletons that would normally be initialized when the first SignalR connection is established. Subsequent calls averaged the .0174 milliseconds referenced above.

If that's too expensive, you can always store the returned IHubContext in a singleton. It's thread-safe.

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.