Search code examples
asp.net-coredapper

Dapper QueryFirst Too slow if no connection


it takes almost up to 30 seconds, mostly 20ish avg

    public OperadorViewModel GetById(int id)
    {
        IDbConnection con = new SqlConnection(_configuration.GetConnectionString("DefaultConnection"));

        var query = @"SELECT 
                          * 
                      FROM 
                        Operadores
                      WHERE Id= @id";

        return con.QueryFirstOrDefault<OperadorViewModel>(query, new { id = id });
    }

Is there a way to make it faster to detect there is no connection so i can show error message to user fast instead waiting 30 seconds?...


Solution

  • You can set value of SqlConnection.ConnectionTimeout property to some lower value.

    Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.

    This way, if there is no connection, timeout will expire earlier and exception will be thrown earlier.

    This can also be done through Connection String or SqlCommand.