cangosta
cangosta

Reputation: 1234

SignalR dispose of HubConnection

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...

Upvotes: 13

Views: 10751

Answers (2)

d.moncada
d.moncada

Reputation: 17392

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());

Upvotes: 9

DDan
DDan

Reputation: 8276

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.

Upvotes: 2

Related Questions