ServiceBrokerListener
ServiceBrokerListener copied to clipboard
ObjectDisposedException while calling Stop()
Hi,
I'm using the following code to react to the NotificationProcessStopped
event:
// Listener contains the SqlDependencyEx instance
Listener.NotificationProcessStopped += (sender, args) =>
{
Task.Run(async () =>
{
await Task.Delay(5000);
AttemptReconnect();
});
};
private void AttemptReconnect()
{
try
{
Listener.Stop();
Listener.Start();
}
catch
{
Task.Run(async () =>
{
await Task.Delay(5000);
AttemptReconnect();
});
}
}
If I run this code and stop the SQL Server to simulate a disconnect, I am receiving an ObjectDisposedException: The CancellationTokenSource has been disposed
in the Stop()
method when the _threadSource.Token.IsCancellationRequested
property is read in line 567 on the second reconnect attempt (and every reconnect attempt thereafter).
When I change this to _threadSource.IsCancellationRequested
everything works fine.
So first time the token stops, then second time there is no token because it was disposed and now you want to use the token source to check if cancellation is requested?
From this
if ((_threadSource == null) || (_threadSource.Token.IsCancellationRequested))
to this
if ((_threadSource == null) || (_threadSource.IsCancellationRequested))
Yes that was the idea, since accessing the _threadSource.Token
immediately causes the ObjectDisposedException
.
I would suggest you submit a pull request with this change, and tag @dyatchenko . They are the only one that can make edits.