How to check if Invalid Operation, Error connection is closed is fixed in System.Data.SqlClient

494 Views Asked by At

During high load, our app randomly throw this error:

System.Data.SqlClient.SqlConnection.GetOpenTdsConnection
outerType
System.AggregateException
outerMessage
A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.
innermostType
System.InvalidOperationException
innermostMessage
Invalid operation. The connection is closed

I looked into it and looks like this issue is fixed on Microsoft.Data.SQLClient

Our code comes from old world and still uses System.Data.SqlClient.Is there a way to know if same issue exists in System.Data.SqlClient and been fixed in a new version? Or we will have to use Microsoft.Data.SQLClient? (we tried Microsoft.Data.SqlClient before and there are behaviour differences)

1

There are 1 best solutions below

1
On

If you are using tasks you can do the following try that I'm not sure that you will get all the information you want but you will get more information about unhandled exception

public static void LogExceptions(this Task task)
{
   task.ContinueWith( t =>
   {
        var aggException = t.Exception.Flatten();
        foreach(var exception in aggException.InnerExceptions)
           LogException(exception);
   }, 
   TaskContinuationOptions.OnlyOnFaulted);
}

And you can use like below

Task.Factory.StartNew( () => 
{ 
      // Do your work...
}).LogExceptions();

Or you can use the TaskScheduler.UnobservedTaskException to get task exceptions where task exceptions are not observed.

For more information see the below links

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was

Looking for what caused the "A Task's exception(s) were not observed..."