TransactionBridge.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / TransactionBridge / Microsoft / Transactions / Bridge / TransactionBridge.cs / 1 / TransactionBridge.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic; 
using System.Configuration;
using System.Diagnostics; 
using System.Globalization; 
using System.Reflection;
using System.Runtime.InteropServices; 
using System.ServiceModel.Diagnostics;
using System.Threading;
using Microsoft.Transactions.Bridge;
using Microsoft.Transactions.Bridge.Configuration; 

using DiagnosticUtility = Microsoft.Transactions.Bridge.DiagnosticUtility; 
 
namespace System.ServiceModel.Internal
{ 
    [ComImport]
    [Guid("d860d655-0b79-4aa6-a741-ab216007ef55")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface ITransactionBridge 
    {
        void Init([MarshalAs(UnmanagedType.IUnknown)] object bridgeNetworkConfiguration); 
        void Start(); 
        void Shutdown();
    } 

    [ComVisible(true)]
    [Guid("eec5dcca-05dc-4b46-8af7-2881c1635aea")]
    [ProgId("")] 
    public class TransactionBridge : ITransactionBridge
    { 
        TransactionBridgeSection config; 
        object bridgeConfig;
        List transactionManagers; 

        public TransactionBridge()
        {
            PropagationProtocolsTracing.TraceVerbose("Transaction Bridge Created"); 
            this.transactionManagers = new List();
        } 
 
        public void Init(object bridgeConfig)
        { 
            this.bridgeConfig = bridgeConfig;

            PropagationProtocolsTracing.TraceVerbose("Initializing...");
            try 
            {
                this.config = TransactionBridgeSection.GetSection(); 
            } 
            catch(Exception e)
            { 
                PropagationProtocolsTracing.TraceError("Error reading configuration: " + e);
                throw;
            }
 
            // assert that both protocol version are configured to start up
            this.config.Protocols.AssertBothWsatProtocolVersions(); 
 
            try
            { 
                PropagationProtocolsTracing.TraceVerbose("Reading transaction managers from configuration...");

                if (!IsAssemblyMicrosoftSigned(this.config.TransactionManagerType))
                { 
                    PropagationProtocolsTracing.TraceVerbose("Transaction manager type has wrong signature: " +
                                                             this.config.TransactionManagerType); 
 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        new ConfigurationErrorsException(Microsoft.Transactions.SR.GetString( 
                                                         Microsoft.Transactions.SR.TransactionManagerTypeWrongSignature)));
                }

                PropagationProtocolsTracing.TraceVerbose("Loading transaction manager " + 
                                                         this.config.TransactionManagerType);
 
                Type tmType = Type.GetType(this.config.TransactionManagerType); 
                if (tmType == null)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                        new ConfigurationErrorsException(Microsoft.Transactions.SR.GetString(
                                                         Microsoft.Transactions.SR.CouldNotLoadTM,
                                                         this.config.TransactionManagerType)));
 
                PropagationProtocolsTracing.TraceVerbose("Initializing transaction managers...");
 
                try 
                {
                    foreach (ProtocolElement protocol in this.config.Protocols) 
                    {
                        TransactionManager transactionManager;
                        transactionManager = (TransactionManager)Activator.CreateInstance(tmType);
 
                        transactionManager.Initialize(protocol.Type, this.bridgeConfig);
                        this.transactionManagers.Add(transactionManager); 
                    } 
                }
                catch 
                {
                    // if any transaction manager (protocol provider)
                    // fails to initialize, none will be started
                    this.transactionManagers.Clear(); 
                    throw;
                } 
            } 
            // Any exception caught here indicates that the protocol has failed to initialize
            // We re-throw the exception to COM interop when we're done. It will be converted 
            // to an HRESULT and propagated to the ITransactionBridge caller.
            catch (Exception e)
            {
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Error); 
                PropagationProtocolsTracing.TraceError("Error initializing: " + e);
                throw; 
            } 
        }
 
        public void Start()
        {

            List startedTMs = new List(); 
            try
            { 
                PropagationProtocolsTracing.TraceVerbose("Starting..."); 

                foreach (TransactionManager transactionManager in this.transactionManagers) 
                {
                    transactionManager.Start();
                    startedTMs.Add(transactionManager);
 
                    if (!ThreadPool.QueueUserWorkItem(DiagnosticUtility.Utility.ThunkCallback(new WaitCallback(RecoverWorkItem)), transactionManager))
                    { 
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                            new TransactionBridgeException(
                                Microsoft.Transactions.SR.GetString( 
                                Microsoft.Transactions.SR.CouldNotQueueStartUserWorkItem)));
                    }
                }
            } 
            // Any exception caught here indicates that the protocol has failed to initialize
            // We re-throw the exception to COM interop when we're done. It will be converted 
            // to an HRESULT and propagated to the ITransactionBridge caller. 
            catch (Exception e)
            { 
                // if any transaction manager fails to start, stop all started TMs
                foreach (TransactionManager transactionManager in startedTMs)
                {
                    transactionManager.Stop(); 
                }
 
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Error); 
                PropagationProtocolsTracing.TraceError("Error starting: " + e);
                throw; 
            }
        }

        public void Shutdown() 
        {
            // Nothing: We don't support clean shutdown right now! 
        } 

        void RecoverWorkItem(object obj) 
        {
            try
            {
                PropagationProtocolsTracing.TraceVerbose("Recovering..."); 
                TransactionManager transactionManager = (TransactionManager)obj;
                transactionManager.Recover(); 
            } 
            // A PluggableProtocolException indicates that the protocol has failed to recover,
            // but has handled the error gracefully, so processing may continue 
            // Any other exception is a fatal condition due to the sensitive nature
            // of recovery handling.
            catch (PluggableProtocolException e)
            { 
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Error);
                PropagationProtocolsTracing.TraceError("Error recovering: " + e); 
            } 
            catch (Exception e)
            { 
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Critical);
                PropagationProtocolsTracing.TraceError("Unknown error recovering: " + e);

                // This is a bit of a layering violation, but such is life 
                Microsoft.Transactions.Wsat.Protocol.TransactionBridgeRecoveryFailureRecord.TraceAndLog(e);
 
                DiagnosticUtility.InvokeFinalHandler(e); 
            }
        } 

        internal static bool IsAssemblyMicrosoftSigned(string assemblyQualifiedTypeName)
        {
            string[] split = assemblyQualifiedTypeName.Split(new char[] { ',' }, 2); 
            if (split.Length == 2)
            { 
                AssemblyName assemblyName = new AssemblyName(split[1].Trim()); 
                byte[] publicKeyToken = assemblyName.GetPublicKeyToken();
                if (publicKeyToken != null) 
                {
                    string publicKeyTokenHexString = string.Empty;
                    foreach (byte b in publicKeyToken)
                    { 
                        publicKeyTokenHexString += b.ToString("x2", CultureInfo.InvariantCulture);
                    } 
 
                    return string.Compare(publicKeyTokenHexString,
                                          AssemblyRef.MicrosoftPublicKeyToken, 
                                          StringComparison.OrdinalIgnoreCase) == 0;
                }
            }
 
            return false;
        } 
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK