13

In a AspNet SignalR client is it the action to dispose of a HubConnection necessary?

It seems to take some time, from what I have seen...

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
cangosta
  • 1,174
  • 2
  • 13
  • 27

2 Answers2

9

It's not necessary if you are calling Stop().

See https://msdn.microsoft.com/en-us/library/dn235890(v=vs.118).aspx

otherwise, you should always Dispose of IDisposable objects when you are done using them.

If it is taking too long (i.e., blocking the current thread), just stop it on a Task, something like:

Task.Run(()=>hubConnection.Stop());
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • 2
    Thanks for your answer. I'm not calling `stop()`, once it is exactly the same thing as `Dispose()`. From the signalr source code `protected virtual void Dispose(bool disposing) { if (disposing) { Stop(); } }` – cangosta Sep 18 '15 at 11:09
  • 1
    Disposing the connection in a separate task does the trick, but is it a common practice? – cangosta Sep 18 '15 at 11:16
  • 1
    @cangosta, yes, it is common if you do not want the disposing of it to block. – d.moncada Sep 18 '15 at 16:16
  • 1
    Its a pain but: Task.Run(() => this.hubConnection.Dispose()).Wait(TimeSpan.FromSeconds(10)); – user3578181 Feb 16 '16 at 02:34
2

What do you mean by it takes too much time? Can you detail? Are you getting timeout exception?

From the book C# 5.0 in a Nutshell:

A safe rule to follow (in nearly all cases) is “if in doubt, dispose.” A disposable object —if it could talk—would say the following:

When you’ve finished with me, let me know. If simply abandoned, I might cause trouble for other object instances, the application domain, the computer, the network, or the database!

I would say dispose if it's not a dealbreaker. Also might be useful to find out what takes so long time there.

Community
  • 1
  • 1
DDan
  • 8,068
  • 5
  • 33
  • 52