Faults.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 / Wsat / InputOutput / Faults.cs / 1 / Faults.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

// Implement special-case faults that require dynamic reason generation 

using System; 
using System.ServiceModel.Channels; 
using System.Diagnostics;
using System.ServiceModel; 
using System.Xml;
using Microsoft.Transactions.Wsat.Messaging;
using Microsoft.Transactions.Wsat.Protocol;
 
using DiagnosticUtility = Microsoft.Transactions.Bridge.DiagnosticUtility;
using Fault = Microsoft.Transactions.Wsat.Messaging.Fault; 
 
namespace Microsoft.Transactions.Wsat.InputOutput
{ 
    class CoordinatorRegistrationFailedFault : Fault
    {
        // This fault is used only with WSAT 1.0
        CoordinatorRegistrationFailedFault(string coordinatorReason) 
            :
            base(AtomicTransactionStrings.Version(ProtocolVersion.Version10).FaultAction, 
                 Faults.CoordinatorRegistrationFailedCode, 
                 coordinatorReason)
        { 
        }

        public static CoordinatorRegistrationFailedFault CreateFault(MessageFault fault)
        { 
            string coordinatorReason;
            if (fault == null) 
            { 
                coordinatorReason = SR.GetString(SR.CoordinatorRegistrationFailedReason);
            } 
            else
            {
                string coordinatorCode = Library.GetFaultCodeName(fault);
 
                if (coordinatorCode == null)
                { 
                    coordinatorReason = SR.GetString(SR.CoordinatorRegistrationFaultedUnknownReason); 
                }
                else 
                {
                    coordinatorReason = SR.GetString(SR.CoordinatorRegistrationFaultedReason, coordinatorCode);
                }
            } 

            return new CoordinatorRegistrationFailedFault(coordinatorReason); 
        } 
    }
 
    class FaultSender
    {
        ProtocolState state;
        AsyncCallback sendFaultComplete; 

        public FaultSender(ProtocolState state) 
        { 
            this.state = state;
            this.sendFaultComplete = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(SendFaultComplete)); 
        }

        public void TrySendCompletionParticipantFault(EndpointAddress faultTo,
                                                      UniqueId messageID, 
                                                      Fault fault)
        { 
            DatagramProxy proxy = state.TryCreateCompletionParticipantProxy(faultTo); 
            if (proxy == null)
            { 
                if (DebugTrace.Warning)
                    DebugTrace.Trace(TraceLevel.Warning,
                                     "Could not create a proxy to send {0} fault",
                                     fault.Code.Name); 
            }
            else 
            { 
                try
                { 
                    TrySendFault(proxy, messageID, fault);
                }
                finally
                { 
                    proxy.Release();
                } 
            } 
        }
 
        public void TrySendTwoPhaseCommitParticipantFault(EndpointAddress faultTo,
                                                          UniqueId messageID,
                                                          Fault fault)
        { 
            DatagramProxy proxy = state.TryCreateTwoPhaseCommitParticipantProxy(faultTo);
            if (proxy == null) 
            { 
                if (DebugTrace.Warning)
                    DebugTrace.Trace(TraceLevel.Warning, 
                                     "Could not create a proxy to send {0} fault",
                                     fault.Code.Name);
            }
            else 
            {
                try 
                { 
                    TrySendFault(proxy, messageID, fault);
                } 
                finally
                {
                    proxy.Release();
                } 
            }
        } 
 
        public void TrySendTwoPhaseCommitCoordinatorFault(EndpointAddress faultTo,
                                                          UniqueId messageID, 
                                                          Fault fault)
        {
            DatagramProxy proxy = state.TryCreateTwoPhaseCommitCoordinatorProxy(faultTo);
            if (proxy == null) 
            {
                if (DebugTrace.Warning) 
                    DebugTrace.Trace(TraceLevel.Warning, 
                                     "Could not create a proxy to send {0} fault",
                                     fault.Code.Name); 
            }
            else
            {
                try 
                {
                    TrySendFault(proxy, messageID, fault); 
                } 
                finally
                { 
                    proxy.Release();
                }
            }
        } 

        void TrySendFault(DatagramProxy proxy, UniqueId messageID, Fault fault) 
        { 
            if (proxy == null)
            { 
                if (DebugTrace.Warning)
                    DebugTrace.Trace(TraceLevel.Warning,
                                     "Could not create a proxy to send {0} fault",
                                     fault.Code.Name); 
            }
            else 
            { 
                state.Perf.FaultsSentCountPerInterval.Increment();
 
                if (DebugTrace.Info)
                    DebugTrace.Trace(TraceLevel.Info,
                                     "Sending {0} fault to {1}",
                                     fault.Code.Name, 
                                     Ports.TryGetAddress(proxy));
 
                IAsyncResult ar = proxy.BeginSendFault(messageID, fault, sendFaultComplete, proxy); 
                if (ar.CompletedSynchronously)
                { 
                    OnSendFaultComplete(ar, proxy);
                }
            }
        } 

        void SendFaultComplete(IAsyncResult ar) 
        { 
            if (!ar.CompletedSynchronously)
            { 
                OnSendFaultComplete(ar, (DatagramProxy) ar.AsyncState);
            }
        }
 
        void OnSendFaultComplete(IAsyncResult ar, DatagramProxy proxy)
        { 
            try 
            {
                proxy.EndSendMessage(ar); 
            }
            catch (WsatSendFailureException e)
            {
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning); 
                DebugTrace.TraceSendFailure(e);
 
                state.Perf.MessageSendFailureCountPerInterval.Increment(); 
            }
        } 
    }
}

// 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