/// <summary> /// Responsible for releasing the lease on message failure and removing the message on transaction commit /// </summary> /// <param name="context">Transaction context of the message processing</param> /// <param name="messageId">Identifier of the message currently being processed</param> private void ApplyTransactionSemantics(ITransactionContext context, long messageId) { AutomaticLeaseRenewer renewal = null; if (_automaticLeaseRenewal == true) { renewal = new AutomaticLeaseRenewer(ReceiveTableName.QualifiedName, messageId, ConnectionProvider, _automaticLeaseRenewalIntervalMilliseconds, _leaseIntervalMilliseconds); } context.OnAborted( () => { renewal?.Dispose(); AsyncHelpers.RunSync(() => UpdateLease(ConnectionProvider, ReceiveTableName.QualifiedName, messageId, null)); } ); context.OnCommitted( async() => { renewal?.Dispose(); // Delete the message using (var deleteConnection = await ConnectionProvider.GetConnection()) { using (var deleteCommand = deleteConnection.CreateCommand()) { deleteCommand.CommandType = CommandType.Text; deleteCommand.CommandText = $@" DELETE FROM {ReceiveTableName.QualifiedName} WITH (ROWLOCK) WHERE id = @id "; deleteCommand.Parameters.Add("@id", SqlDbType.BigInt).Value = messageId; deleteCommand.ExecuteNonQuery(); } await deleteConnection.Complete(); } } ); }
/// <summary> /// Responsible for releasing the lease on message failure and removing the message on transaction commit /// </summary> /// <param name="context">Transaction context of the message processing</param> /// <param name="messageId">Identifier of the message currently being processed</param> /// <param name="cancellationToken">Token to abort processing</param> private void ApplyTransactionSemantics(ITransactionContext context, long messageId, CancellationToken cancellationToken) { AutomaticLeaseRenewer renewal = null; if (_automaticLeaseRenewal == true) { renewal = new AutomaticLeaseRenewer( this, ReceiveTableName.QualifiedName, messageId, ConnectionProvider, _automaticLeaseRenewalInterval, _leaseInterval, cancellationToken); } context.OnAborted( ctx => { renewal?.Dispose(); try { AsyncHelpers.RunSync(() => UpdateLease(ConnectionProvider, ReceiveTableName.QualifiedName, messageId, null, cancellationToken)); } catch (Exception ex) { Log.Error(ex, "While Resetting Lease"); } } ); context.OnCommitted( async ctx => { renewal?.Dispose(); try { await DeleteMessage(messageId, cancellationToken); } catch (Exception ex) { Log.Error(ex, "While Deleteing Message"); } } ); }