I am using asp.net core 3.1, ADO.NET , Azure SQL Database in this implementation. In this case an api call is made to the service layer -> database layer using stored procedure.

Azure SQL Pricing specification:

GeneralPurpose: Gen5, 8 vCores (Elastic Pool)

Here goes the code :

public dynamic GetData(long param1, long param2, long param3, long param4)
{
    DataTable result = new DataTable();
    DataSet ds = new DataSet();
    using (var connection = new SqlConnection(_objDBContext.Database.GetDbConnection().ConnectionString))
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand())
        {
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "[TT].[GetData]";
            command.Parameters.Add(new SqlParameter("@Param1Id", SqlDbType.BigInt)
            {Value = param1});
            command.Parameters.Add(new SqlParameter("@Param2", SqlDbType.BigInt)
            {Value = param2});
            command.Parameters.Add(new SqlParameter("@Param3", SqlDbType.BigInt)
            {Value = param3 == 0 ? DBNull.Value : (object)param3});
            command.Parameters.Add(new SqlParameter("@Param4", SqlDbType.BigInt)
            {Value = param4});
            using (var sda = new SqlDataAdapter())
            {
                sda.SelectCommand = command;
                sda.Fill(ds);
                if (ds.Tables.Count > 0)
                {
                    result = ds.Tables[0];
                }
            }
        }

        connection.Close();
    }

    return result;
}

The above method is called from asp.net core web api action method. This method works fine sometimes and but also I am seeing the below error :

System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
 ---> System.ComponentModel.Win32Exception (258): The wait operation timed out.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
   at Test.GetData(Int64 param1, Int64 param2, Int64 param3, Int64 param4) in C:\Temp\Test.cs:line 132
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
ClientConnectionId:2f58eb46-aa87-4d1f-b6fc-04bde3bc3195
Error Number:-2,State:0,Class:11
ClientConnectionId before routing:1bcca524-332e-4c54-ab5d-15777c69d928
Routing Destination:a8f123af8ed9.tr2382.westeurope1-a.worker.database.windows.net,11019

The strange this in this case is the method works fine but sometimes throws error.

Can anyone help me with their guidance is there anything wrong done in the code and help me to fix this issue

0

There are 0 best solutions below