Microsoft Knowledge Base Email Alertz

KBAlertz.com: You cannot determine whether the transaction is in doubt when you try to run a transaction by using the ServiceDomain object in an application that is based on the .NET Framework 1.1

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **


Community Site



We Send hundreds of thousands of emails using ASP.NET Email


ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 949760 - Last Review: March 25, 2008 - Revision: 1.1

You cannot determine whether the transaction is in doubt when you try to run a transaction by using the ServiceDomain object in an application that is based on the .NET Framework 1.1

On This Page

SYMPTOMS

In an application that is based on the Microsoft .NET Framework 1.1, you try to run a transaction by using the ServiceDomain object. When you do this, you cannot determine whether the transaction is in doubt during a call to the ServiceDomain.Leave method.

WORKAROUND

To work around this behavior, use one of the following methods.

Method 1

Subscribe to the ITransactionOutcomeEvents event notification. To do this, follow these steps:
  1. In the Microsoft Visual Studio .NET 2003 project, right-click the project name in Solution Explorer, and then click Add New Item.
  2. Click Class, type TransactionOutcomeEvent.cs in the Name box, and then click OK.
  3. In the TransactionOutcomeEvent.cs file, replace the existing code by using the following code.
     //TransactionOutcomeEvent.cs
    using System;
    using System.Runtime.InteropServices;
    using System.Threading;
    
    namespace DtcInterop
    {
    
        public class TransactionOutcomeEvent : ITransactionOutcomeEvents
        {
            private ManualResetEvent _evt;
            private DtcTransactionStatus _txStatus;
    
            public TransactionOutcomeEvent(ref ManualResetEvent evt)
            {
                _evt = evt;
                _txStatus = DtcTransactionStatus.Unknown;
            }
    
            #region ITransactionOutcomeEvents Members
    
            // Implement the ITransactionOutcomeEvents members
            
            public void Committed(bool fRetaining, IntPtr pNewUOW, Int32 hResult)
            {
                _txStatus = DtcTransactionStatus.Commited;
                _evt.Set();
            }
    
            public void Aborted(IntPtr pBoidReason, bool fRetaining, IntPtr pNewUOW, Int32 hResult)
            {
                _txStatus = DtcTransactionStatus.Aborted;
                _evt.Set();
            }
    
            public void HeuristicDecision(UInt32 decision, IntPtr pBoidReason, Int32 hResult)
            {
                _txStatus = DtcTransactionStatus.Error;
                _evt.Set();
            }
    
            public void Indoubt()
            {
                _txStatus = DtcTransactionStatus.InDoubt;
                _evt.Set();
            }
    
            #endregion  
    
    
            public DtcTransactionStatus TransactionOutcomeStatus
            {
                get { return _txStatus; }
            }
        }
    
    
        #region ITransactionOutcomeEvents Definition
    
            // Here is the definition of the ITransactionOutcomeEvents interface
            // Your class must implement this interface before trying to subscribe to the TransactionOutcomeEvents events
            [
            ComImport,
            Guid("3A6AD9E2-23B9-11cf-AD60-00AA00A74CCD"),
            InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
            ]
            internal interface ITransactionOutcomeEvents 
            {
            void Committed([MarshalAs(UnmanagedType.Bool)] bool fRetaining,
            IntPtr pNewUOW,
            Int32 hResult);
            void Aborted(IntPtr pBoidReason,
            [MarshalAs(UnmanagedType.Bool)] bool fRetaining,
            IntPtr pNewUOW,
            Int32 hResult);
            void HeuristicDecision(UInt32 decision,
            IntPtr pBoidReason,
            Int32 hResult);
            void Indoubt();
            }
    
        #endregion
    
    
        public enum DtcTransactionStatus
        {
            Commited,
            Aborted,
            InDoubt,
            Error,
            Unknown
        }
    
    }
    
    
  4. In Solution Explorer, right-click the project name, and then click Add New Item.
  5. Click Class, type Class1.cs in the Name box, and then click OK.
  6. In the Class1.cs file, replace the existing code by using the following code.
    //Class1.cs
    using System;
    using System.Runtime.InteropServices;
    using System.EnterpriseServices;
    using System.Data.SqlClient;
    using System.Data;
    using System.Threading;
    
    namespace DtcInterop
    {
    
        public class Class1
        {
            // Member variables used to set up ITransactionOutcomeEvents notifier
            private UCOMIConnectionPointContainer _iConPointContainer;
            private UCOMIConnectionPoint _iConnectionPoint;
            private int _adviseCookie;
            private TransactionOutcomeEvent _objEvent;
            private ManualResetEvent[] _txStatusSignaled = new ManualResetEvent[1]; 
           // You cannot execute the 'finally' code until you receive the result from DTC about the transaction status
    
            public Class1()
            {
                _txStatusSignaled[0] = new ManualResetEvent(false);
            }
    
    
            public void Run(string CustomerId, string ContactName)
            {
                // Here you enter into ServiceConfig, register for ITransactionOutcomeEvents
                // Execute the transactional code
                ServiceConfig sc = new ServiceConfig();
                sc.Transaction = TransactionOption.Required;
                sc.IsolationLevel = TransactionIsolationLevel.ReadCommitted;
                sc.TransactionDescription = "Test Transactions"
                sc.TransactionTimeout = -1;
    
                ServiceDomain.Enter(sc);
                try 
                {
                    RegisterForITransactionOutcomeEvents();
                    ExecuteTransactionCode(CustomerId, ContactName);
                    ContextUtil.SetComplete();
                }
                    catch(Exception ex)
                {
                    ContextUtil.SetAbort();
                    Console.WriteLine("\nError while executing client!!!");
                    Console.WriteLine("Message: " + ex.Message);
                    Console.WriteLine("Source: " +ex.Source);
                    Console.WriteLine("Callstack:\n" +ex.StackTrace);
                }
                finally
                {
                    // Leave the ServiceDomain
                    ServiceDomain.Leave();
    
                    // Wait until you are notified of the transaction outcome event from DTC
                    int index = WaitHandle.WaitAny(_txStatusSignaled);
    
                    if (index == WaitHandle.WaitTimeout)
                    {
                        Console.WriteLine("We did not receive Transaction outcome event within 60 seconds");
                    }
                    else
                    {
                        // You are notified of the transaction outcome event from DTC
                        // Now take action based on the transaction outcome event
    
                        switch (this.TransactionOutcomeStatus)
                        {
                            case DtcTransactionStatus.Commited:
                                Console.WriteLine("ITransactionOutcomeEvent Status -> COMMITED");
                                break;
                            case DtcTransactionStatus.Aborted:
                                Console.WriteLine("ITransactionOutcomeEvent Status -> ABORTED");
                                break;
                            case DtcTransactionStatus.Error:
                                Console.WriteLine("ITransactionOutcomeEvent Status -> ERROR");
                                break;
                            case DtcTransactionStatus.InDoubt:
                                Console.WriteLine("ITransactionOutcomeEvent Status -> INDOUBT");
                                break;
                            case DtcTransactionStatus.Unknown:
                            default:
                                Console.WriteLine("Tx Outcome Status not set.");
                                break;
                        }
                    }
    
                    // You have the Transaction status. Un-register for the ITransactionOutcomeEvents notifications
                    _iConnectionPoint.Unadvise(_adviseCookie); 
                }
            }
    
    
            private void RegisterForITransactionOutcomeEvents()
            { 
                System.EnterpriseServices.ITransaction iTxn;
                // UUID of the ITransactionOutcomeEvents interface
                Guid GuidOutcomeEvents = new Guid("{3A6AD9E2-23B9-11CF-AD60-00AA00A74CCD}"); 
                GetCurrentITransaction(out iTxn);
                _objEvent = new TransactionOutcomeEvent(ref _txStatusSignaled[0]);
                _iConPointContainer = (UCOMIConnectionPointContainer)iTxn;
                _iConPointContainer.FindConnectionPoint(ref GuidOutcomeEvents, out _iConnectionPoint);
                _iConnectionPoint.Advise(_objEvent, out _adviseCookie);
            }
    
            private void ExecuteTransactionCode(string custId, string custName)
            {
                string connString="server=servername;uid=sa;pwd=*****;database=Northwind;"
                string commandName = "UpdateCustomer"
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand(commandName, conn);
                    command.CommandType = CommandType.StoredProcedure;
                    // @customerId parameter
                    SqlParameter pCustID = new SqlParameter("@CustomerId", SqlDbType.NChar, 5);
                    pCustID.Direction = ParameterDirection.Input;
                    pCustID.Value = custId;
                    command.Parameters.Add(pCustID);
                    // @ContactName parameter
                    SqlParameter pContactName = new SqlParameter("@ContactName", SqlDbType.NVarChar, 30);
                    pContactName.Direction = ParameterDirection.Input;
                    pContactName.Value = custName;
                    command.Parameters.Add(pContactName);
                    // Execute update stored proc
                    command.ExecuteNonQuery();
                }
            }
    
            private void GetCurrentITransaction(out System.EnterpriseServices.ITransaction ITxn)
            {
                if (ContextUtil.IsInTransaction)
                ITxn = (System.EnterpriseServices.ITransaction) ContextUtil.Transaction;
                else
                ITxn = null;
            }
    
            public DtcTransactionStatus TransactionOutcomeStatus
            {
                get { return _objEvent.TransactionOutcomeStatus; }
            }
        }
    }

Method 2

Upgrade the application to the Microsoft .NET Framework 2.0. For more information about how to obtain the .NET Framework 2.0 redistributable package, visit the following Microsoft Web site:
http://www.microsoft.com/downloads/details.aspx?FamilyID=0856EACB-4362-4B0D-8EDD-AAB15C5E04F5&displaylang=en (http://www.microsoft.com/downloads/details.aspx?FamilyID=0856EACB-4362-4B0D-8EDD-AAB15C5E04F5&displaylang=en )
When you call the ServiceDomain.Leave method, you can obtain the System.Transactions.Transaction object from the ContextUtil.SystemTransaction property. Then, you can examine whether the TransactionStatus property is equal to the TransactionStatus.InDoubt member.

STATUS

This behavior is by design.

REFERENCES

For more information about the ITransactionOutcomeEvents.Indoubt method, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/ms687104(VS.85).aspx (http://msdn2.microsoft.com/en-us/library/ms687104(VS.85).aspx)
For more information about the ServiceDomain.Leave method, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/system.enterpriseservices.servicedomain.leave.aspx (http://msdn2.microsoft.com/en-us/library/system.enterpriseservices.servicedomain.leave.aspx)
For more information about the ContextUtil.SystemTransaction property, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/system.enterpriseservices.contextutil.systemtransaction.aspx (http://msdn2.microsoft.com/en-us/library/system.enterpriseservices.contextutil.systemtransaction.aspx)
For more information about the TransactionStatus enumeration, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/system.transactions.transactionstatus(VS.80).aspx (http://msdn2.microsoft.com/en-us/library/system.transactions.transactionstatus(VS.80).aspx)

APPLIES TO
  • Microsoft .NET Framework 1.1
Keywords: 
kbcode kbexpertiseinter kbfix kbtshoot kbprb KB949760
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please