Ensure SqlDependency was Stopped

2019-08-16 10:04发布

问题:

SqlDependency starts on application start and stops on app stop. There are can be cases when SqlDependency.Stop() failed (for example, problem with connect to DB). As I understand, the SqlDependency infrastructure will be removed anyway by timeout.

But I don't understand what else SqlDependency.Stop() do?

Does it make sense to call SqlDependency.Stop() before SqlDependency.Start()?

回答1:

Does it make sense to call SqlDependency.Stop() before SqlDependency.Start()?

If Start() was not called, calling Stop() is a no-op. We can check in the SqlDependency.cs reference source

   internal static bool Stop(string connectionString, string queue, bool useDefaults, bool startFailed) {
            ...
            bool result = false;

            lock (_startStopLock) {
                if (null != _processDispatcher) { // If _processDispatcher null, no Start has been called.
                    ....
                }
            }
            return result;

So it doesn't hurt, but it shouldn't be needed.