.NET 6 When using Dapper to call a stored procedure, I receive the error The I/O operation has been aborted

61 views Asked by At

I have a .NET 6 Windows Service that runs stored procedures. I am using Dapper 2.0.151, and System.Data.SqlClient 4.8.6

Let's say that I have a stored procedure usp_GetFeed that returns 70,000 rows.

  • If I run the stored procedure from within a SQL Server job, it runs fine and takes about 2 minutes

  • If I call the stored procedure from the service, using Dapper, around every 2nd or 3rd time I call it, I get the following error:

Error during SearchPlugin.PullDataFromFeeds: Feed usp_GetFeed failed: [115520ms] ExecuteStoredProcedureAsync:
System.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The I/O operation has been aborted because of either a thread exit or an application request.)

System.ComponentModel.Win32Exception (995): The I/O operation has been aborted because of either a thread exit or an application request.

at System.Data.SqlClient.SqlCommand.EndExecuteNonQuery(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location ---
at Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, Object param) in /_/Dapper/SqlMapper.Async.cs:line 647
at Search.Sync.SearchPlugin.Data.DataLayer.ExecuteStoredProcedureAsync(String sql) in C:_git\GlobalClearance\Search.Sync.Service\Search.Sync.SearchPlugin\Data\DataLayer.cs:line 245 ClientConnectionId:8cefc850-0dbd-4837-bbf5-6ef1e736ba86

Here is the code used to call Dapper

public class ExecuteStoredProcedureResult<TOutput> 
{
    public IEnumerable<TOutput> Output { get; set; } = new List<TOutput>();
    public int Duration {get;set;}
}

 public async Task<ExecuteStoredProcedureResult<TOutput>> ExecuteStoredProcedureAsync<TOutput>(string sql)
 {
     var result = new ExecuteStoredProcedureResult<TOutput>();
     var sw = new Stopwatch();
     sw.Start();

     try
     {
         result.Output = await _connection.QueryAsync<TOutput>(sql, commandType: CommandType.StoredProcedure, commandTimeout: _commandTimeoutInSeconds);
         result.Success = true;
     }
     catch (Exception ex)
     {
         result.Errors.Add($"{ex}");
     }

     sw.Stop();
     result.Duration = sw.Elapsed;

     return result;
 }

On the chance that it is a timeout, I have set

Dapper.SqlMapper.Settings.CommandTimeout = 0;

But the same behavior occurs.

0

There are 0 answers