Tortuga.Chain.PostgreSqlDataSource.ExecuteAsync C# (CSharp) Method

ExecuteAsync() protected method

execute as an asynchronous operation.
/// executionToken;executionToken is null. /// or /// implementation;implementation is null. ///
protected ExecuteAsync ( NpgsqlParameter>.CommandExecutionToken executionToken, CommandImplementationAsync implementation, CancellationToken cancellationToken, object state ) : Task
executionToken NpgsqlParameter>.CommandExecutionToken The execution token.
implementation CommandImplementationAsync The implementation that handles processing the result of the command.
cancellationToken System.Threading.CancellationToken The cancellation token.
state object User supplied state.
return Task
        protected override async Task<int?> ExecuteAsync(CommandExecutionToken<NpgsqlCommand, NpgsqlParameter> executionToken, CommandImplementationAsync<NpgsqlCommand> implementation, CancellationToken cancellationToken, object state)
        {
            if (executionToken == null)
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            if (implementation == null)
                throw new ArgumentNullException("implementation", "implementation is null.");

            var startTime = DateTimeOffset.Now;
            OnExecutionStarted(executionToken, startTime, state);

            try
            {
                using (var con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
                {
                    using (var cmd = new NpgsqlCommand())
                    {
                        cmd.Connection = con;
                        if (DefaultCommandTimeout.HasValue)
                            cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                        cmd.CommandText = executionToken.CommandText;
                        cmd.CommandType = executionToken.CommandType;
                        foreach (var param in executionToken.Parameters)
                            cmd.Parameters.Add(param);

                        executionToken.ApplyCommandOverrides(cmd);

                        int? rows;
                        if (((PostgreSqlCommandExecutionToken)executionToken).DereferenceCursors)
                            rows = await DereferenceCursorsAsync(cmd, implementation).ConfigureAwait(false);
                        else
                            rows = await implementation(cmd).ConfigureAwait(false);

                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                        return rows;
                    }
                }
            }
            catch (Exception ex)
            {
                if (cancellationToken.IsCancellationRequested) //convert Exception into a OperationCanceledException 
                {
                    var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                    OnExecutionCanceled(executionToken, startTime, DateTimeOffset.Now, state);
                    throw ex2;
                }
                else
                {
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                    throw;
                }
            }
        }

Same methods

PostgreSqlDataSource::ExecuteAsync ( NpgsqlTransaction>.OperationExecutionToken executionToken, NpgsqlTransaction>.OperationImplementationAsync implementation, CancellationToken cancellationToken, object state ) : Task