System.Transactions.CommittableTransaction.Commit C# (CSharp) Method

Commit() public method

public Commit ( ) : void
return void
        public void Commit()
        {
            TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;
            if (etwLog.IsEnabled())
            {
                etwLog.MethodEnter(TraceSourceType.TraceSourceLtm, this);
                etwLog.TransactionCommit(this, "CommittableTransaction");
            }

            if (Disposed)
            {
                throw new ObjectDisposedException(nameof(CommittableTransaction));
            }

            lock (_internalTransaction)
            {
                if (_complete)
                {
                    throw TransactionException.CreateTransactionCompletedException(DistributedTxId);
                }

                _internalTransaction.State.BeginCommit(_internalTransaction, false, null, null);

                // now that commit has started wait for the monitor on the transaction to know
                // if the transaction is done.
                do
                {
                    if (_internalTransaction.State.IsCompleted(_internalTransaction))
                    {
                        break;
                    }
                } while (Monitor.Wait(_internalTransaction));

                _internalTransaction.State.EndCommit(_internalTransaction);
            }

            if (etwLog.IsEnabled())
            {
                etwLog.MethodExit(TraceSourceType.TraceSourceLtm, this);
            }
        }

Usage Example

        public void ProcessData(int employeeId)
        {
            // There is not much processing logic here...
            // We are assuming that the organization has 50 employees with sequential Employee Ids
            // If the employeeId  > 50, we are sending an Http Status code of 500 (Internal Server Error) back to the client
            // else, we assume that the processing is successful and send an Http Status code of 200 (Successfull)
            ReceiveContext receiveContext;
            if (!ReceiveContext.TryGet(OperationContext.Current.IncomingMessageProperties, out receiveContext))
            {
                Console.WriteLine("ReceiveContext property was not found in the message...");
                return;
            }

            if (employeeId > 50)
            {
                // Abandon
                receiveContext.Abandon(TimeSpan.MaxValue);
            }
            else
            {
                // Complete in Transaction block.
                CommittableTransaction committableTransaction = new CommittableTransaction();
                Transaction.Current = committableTransaction;
                try
                {
                    receiveContext.Complete(TimeSpan.MaxValue);
                    committableTransaction.Commit();
                }
                catch
                {
                    committableTransaction.Rollback();
                    // If the transaction was not completed we call Abandon explicitly which sends an Http 500 to the client
                    receiveContext.Abandon(TimeSpan.MaxValue);
                }
            }
        }
All Usage Examples Of System.Transactions.CommittableTransaction::Commit