dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.54k stars 4.54k forks source link

Getting the event Notification on Sql Server Disconnection in ADO.Net #67908

Open Zqasim132 opened 2 years ago

Zqasim132 commented 2 years ago

I want to get a SQL Server disconnection event notification in my Database Service which is built in .NET 6 with ADO.net. Actually for now we are detecting the connections health or state by continuously executing a select query in a separate Thread after a specific time intervals. I want a behaviour like SignalR when server is shut down or if any network error occurs it gives an event notification on which we can perform some relevant actions. Is there any way to achieve this. Thanks!

ghost commented 2 years ago

Tagging subscribers to this area: @roji, @ajcvickers See info in area-owners.md if you want to be subscribed.

Issue Details
I want to get a SQL Server disconnection event notification in my Database Service which is built in .NET 6 with ADO.net. Actually for now we are detecting the connections health or state by continuously executing a select query in a separate Thread after a specific time intervals. I want a behaviour like SignalR when server is shut down or if any network error occurs it gives an event notification on which we can perform some relevant actions. Is there any way to achieve this. Thanks!
Author: Zqasim132
Assignees: -
Labels: `area-System.Data`, `untriaged`
Milestone: -
davidfowl commented 2 years ago

cc @ajcvickers

Wraith2 commented 2 years ago

Any ADO.NET that uses DbCommand as the base class will have the StateChange event and should (but not must) fire that event when the state changes.

However it should be noted that the only real way to know if a connection is broken is to try to use it and see if fails, whether you do that or the system does that doesn't really matter but be aware that the state change event may be out of date if no-one has tried to use the connection and may then flip when you do try to use it.