dotnet / SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
MIT License
846 stars 282 forks source link

SqlDependency async support #2412

Open davhdavh opened 7 months ago

davhdavh commented 7 months ago

Is your feature request related to a problem? Please describe.

SqlDependency was designed before async was a thing, and it therefore quite hard to work with in a loop.

Describe the solution you'd like

while(!stoppingToken.IsCancellationRequested) {
  var command = ...;
  var dependency = new AsyncSqlDependency(command, ...);
  var whatever = await ProcessResult(command, stoppingToken);
  bool falseIfTimeout = await dependency.AwaitDependencyChange(TimeSpan.FromMinutes(1), stoppingToken);
}

Describe alternatives you've considered

It is possible to make an implementation with TaskCompletionSource, but would be so much nicer if it was part of the API.

Additional context

One of the hard things to understand when working with SqlDependency is WHEN the dependency is calculated from. The existing implementation is able to call the callback before the ProcessResult finished, so in worst case you can have an unlimited of queries ongoing, while it keeps giving you notifications about changes. This is especially a problem, if the ProcessResult update the underlying data, and thus triggers it over and over.

DavoudEshtehari commented 7 months ago

Thanks for bringing this feature to our attention. It's open to grab with contributors.