ServiceModelPerformanceCounters.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 / ServiceModelPerformanceCounters.cs / 1 / ServiceModelPerformanceCounters.cs

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

namespace System.ServiceModel.Diagnostics 
{
    using System; 
    using System.ServiceModel; 
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Description; 
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized; 
    using System.Diagnostics;
    using System.Globalization; 
    using System.Runtime.Remoting.Messaging; 
    using System.ServiceModel.Channels;
    using System.ServiceModel.Configuration; 
    using System.ServiceModel.Diagnostics;
    using System.Text;
    using System.Xml;
 
    internal class ServiceModelPerformanceCounters
    { 
        Dictionary operationPerfCounters; 
        SortedList actionToOperation;
        EndpointPerformanceCounters endpointPerfCounters; 
        ServicePerformanceCounters servicePerfCounters;
        DefaultPerformanceCounters defaultPerfCounters;
        bool initialized;
        string perfCounterId; 

        internal ServiceModelPerformanceCounters( 
            ServiceHostBase serviceHost, 
            ContractDescription contractDescription,
            EndpointDispatcher endpointDispatcher) 
        {
            this.perfCounterId = endpointDispatcher.PerfCounterId;

            if (PerformanceCounters.Scope == PerformanceCounterScope.All) 
            {
                this.operationPerfCounters = new Dictionary(contractDescription.Operations.Count); 
                this.actionToOperation = new SortedList(contractDescription.Operations.Count); 

                foreach (OperationDescription opDescription in contractDescription.Operations) 
                {
                    DiagnosticUtility.DebugAssert(null != opDescription.Messages, "OperationDescription.Messages should not be null");
                    DiagnosticUtility.DebugAssert(opDescription.Messages.Count > 0, "OperationDescription.Messages should not be empty");
                    DiagnosticUtility.DebugAssert(null != opDescription.Messages[0], "OperationDescription.Messages[0] should not be null"); 
                    if (null != opDescription.Messages[0].Action && !this.actionToOperation.Keys.Contains(opDescription.Messages[0].Action))
                    { 
                        this.actionToOperation.Add(opDescription.Messages[0].Action, opDescription.Name); 
                    }
                    OperationPerformanceCounters c; 
                    if (!this.operationPerfCounters.TryGetValue(opDescription.Name, out c))
                    {
                        OperationPerformanceCounters counters =
                            new OperationPerformanceCounters(serviceHost.Description.Name, contractDescription.Name, opDescription.Name, endpointDispatcher.PerfCounterBaseId); 
                        if (counters.Initialized)
                        { 
                            this.operationPerfCounters.Add(opDescription.Name, counters); 
                        }
                        else 
                        {
                            // cleanup the others and return.
                            this.ReleasePerformanceCounters();
                            this.initialized = false; 
                            return;
                        } 
                    } 
                }
 
                // add endpoint scoped perf counters
                EndpointPerformanceCounters endpointCounters = new EndpointPerformanceCounters(serviceHost.Description.Name, contractDescription.Name, endpointDispatcher.PerfCounterBaseId);
                if (endpointCounters.Initialized)
                { 
                    this.endpointPerfCounters = endpointCounters;
                } 
            } 

            if (PerformanceCounters.PerformanceCountersEnabled) 
            {
                this.servicePerfCounters = serviceHost.Counters;
            }
            if (PerformanceCounters.MinimalPerformanceCountersEnabled) 
            {
                this.defaultPerfCounters = serviceHost.DefaultCounters; 
            } 
            this.initialized = true;
        } 

        internal OperationPerformanceCounters GetOperationPerformanceCountersFromMessage(Message message)
        {
            DiagnosticUtility.DebugAssert(null != message, "message must not be null"); 
            DiagnosticUtility.DebugAssert(null != message.Headers, "message headers must not be null");
            DiagnosticUtility.DebugAssert(null != message.Headers.Action, "action must not be null"); 
 
            string operation;
            if (this.actionToOperation.TryGetValue(message.Headers.Action, out operation)) 
            {
                return this.GetOperationPerformanceCounters(operation);
            }
            else 
            {
                return null; 
            } 
        }
 
        internal OperationPerformanceCounters GetOperationPerformanceCounters(string operation)
        {
            DiagnosticUtility.DebugAssert(PerformanceCounters.Scope == PerformanceCounterScope.All, "Only call GetOparationPerformanceCounters when performance counter scope is All");
 
            OperationPerformanceCounters counters;
            Dictionary opPerfCounters = this.operationPerfCounters; 
            if (opPerfCounters != null && opPerfCounters.TryGetValue(operation, out counters)) 
            {
                return counters; 
            }
            return null;
        }
 
        internal bool Initialized
        { 
            get { return this.initialized; } 
        }
 
        internal EndpointPerformanceCounters EndpointPerformanceCounters
        {
            get { return this.endpointPerfCounters; }
        } 

        internal ServicePerformanceCounters ServicePerformanceCounters 
        { 
            get { return this.servicePerfCounters; }
        } 

        internal DefaultPerformanceCounters DefaultPerformanceCounters
        {
            get { return this.defaultPerfCounters; } 
        }
 
        internal string PerfCounterId 
        {
            get { return this.perfCounterId; } 
        }


        internal void ReleasePerformanceCounters() 
        {
            if (PerformanceCounters.Scope == PerformanceCounterScope.All) 
            { 
                if (this.operationPerfCounters != null)
                { 
                    try
                    {
                        IDictionaryEnumerator e = this.operationPerfCounters.GetEnumerator();
                        while (e.MoveNext()) 
                        {
                            OperationPerformanceCounters op = (OperationPerformanceCounters)e.Value; 
                            op.ReleasePerformanceCounters(); 
                        }
                    } 
#pragma warning suppress 56500 // covered by FxCOP
                    catch (Exception e)
                    {
                        if (ExceptionUtility.IsFatal(e)) 
                            throw;
                        if (DiagnosticUtility.ShouldTraceError) 
                            TraceUtility.TraceEvent(TraceEventType.Error, 
                                TraceCode.PerformanceCountersFailedOnRelease,
                                null, e); 
                    }
                    this.operationPerfCounters.Clear();
                    this.operationPerfCounters = null;
                } 
                if (this.endpointPerfCounters != null)
                { 
                    this.endpointPerfCounters.ReleasePerformanceCounters(); 
                    this.endpointPerfCounters = null;
                } 
            }
            // ServicePerformanceCounters will be released by ServiceHost
            this.servicePerfCounters = null;
            this.defaultPerfCounters = null; 
        }
    } 
 

    internal class ServiceModelPerformanceCountersEntry 
    {
        ServicePerformanceCounters servicePerformanceCounters;
        DefaultPerformanceCounters defaultPerformanceCounters;
        List performanceCounters; 

        public ServiceModelPerformanceCountersEntry(ServicePerformanceCounters serviceCounters) 
        { 
            this.servicePerformanceCounters = serviceCounters;
            this.performanceCounters = new List(); 
        }

        public ServiceModelPerformanceCountersEntry(DefaultPerformanceCounters defaultServiceCounters)
        { 
            this.defaultPerformanceCounters = defaultServiceCounters;
            this.performanceCounters = new List(); 
        } 

        public void Add(ServiceModelPerformanceCounters counters) 
        {
            this.performanceCounters.Add(counters);
        }
 
        public void Remove(string id)
        { 
            for (int i = 0; i < this.performanceCounters.Count; ++i) 
            {
                if (this.performanceCounters[i].PerfCounterId.Equals(id)) 
                {
                    this.performanceCounters.RemoveAt(i);
                    break;
                } 
            }
        } 
 
        public void Clear()
        { 
            this.performanceCounters.Clear();
        }

        public ServicePerformanceCounters ServicePerformanceCounters 
        {
            get { return this.servicePerformanceCounters; } 
            set { this.servicePerformanceCounters = value; } 
        }
 
        public DefaultPerformanceCounters DefaultPerformanceCounters
        {
            get { return this.defaultPerformanceCounters; }
            set { this.defaultPerformanceCounters = value; } 
        }
 
        public List CounterList 
        {
            get { return this.performanceCounters; } 
        }
    }
}
 


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