ActivityIdHeader.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 / ServiceModel / System / ServiceModel / Diagnostics / ActivityIdHeader.cs / 1 / ActivityIdHeader.cs

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

namespace System.ServiceModel.Diagnostics 
{
    using System; 
    using System.ServiceModel.Channels; 
    using System.Diagnostics;
    using System.Xml; 


    class ActivityIdHeader : DictionaryHeader
    { 
        Guid guid;
        Guid headerId; 
 
        internal ActivityIdHeader(Guid activityId)
            : base() 
        {
            this.guid = activityId;
            this.headerId = Guid.NewGuid();
        } 

        internal static Guid ExtractActivityId(Message message) 
        { 
            if (message == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
            }
            Guid guid = Guid.Empty;
            try 
            {
                if (message.State != MessageState.Closed && message.Headers != null) 
                { 
                    int index = message.Headers.FindHeader(DiagnosticStrings.ActivityId, DiagnosticStrings.DiagnosticsNamespace);
 
                    // Check the state again, in case the message was closed after we found the header
                    if (index >= 0)
                    {
                        using (XmlDictionaryReader reader = message.Headers.GetReaderAtHeader(index)) 
                        {
                            guid = reader.ReadElementContentAsGuid(); 
                        } 
                    }
                } 
            }
#pragma warning suppress 56500 // covered by FxCOP
            catch (Exception e)
            { 
                if (DiagnosticUtility.IsFatal(e))
                { 
                    throw; 
                }
                if (DiagnosticUtility.ShouldTraceError) 
                {
                    TraceUtility.TraceEvent(TraceEventType.Error,
                        TraceCode.FailedToReadAnActivityIdHeader,
                        null, 
                        e);
                } 
            } 

            return guid; 
        }

        internal void AddTo(Message message)
        { 
            if (message == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message"); 
            }
            if (message.State != MessageState.Closed && message.Headers.MessageVersion.Envelope != EnvelopeVersion.None) 
            {
                int index = message.Headers.FindHeader(DiagnosticStrings.ActivityId, DiagnosticStrings.DiagnosticsNamespace);
                if (index < 0)
                { 
                    message.Headers.Add(this);
                } 
            } 
        }
 
        protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
        {
            if (writer == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
            } 
            writer.WriteAttributeString("CorrelationId", this.headerId.ToString()); 
            writer.WriteValue(this.guid);
        } 

        public override XmlDictionaryString DictionaryName
        {
            get { return XD.ActivityIdFlowDictionary.ActivityId; } 
        }
 
        public override XmlDictionaryString DictionaryNamespace 
        {
            get { return XD.ActivityIdFlowDictionary.ActivityIdNamespace; } 
        }
    }
}

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