internal static void AddPerformanceCountersForEndpoint(
            ServiceHostBase serviceHost,
            ContractDescription contractDescription,
            EndpointDispatcher endpointDispatcher)
        {
            Fx.Assert(serviceHost != null, "The 'serviceHost' argument must not be null.");
            Fx.Assert(contractDescription != null, "The 'contractDescription' argument must not be null.");
            Fx.Assert(endpointDispatcher != null, "The 'endpointDispatcher' argument must not be null.");
            
            bool performanceCountersEnabled = PerformanceCounters.PerformanceCountersEnabled;
            bool minimalPerformanceCountersEnabled = PerformanceCounters.MinimalPerformanceCountersEnabled;

            if (performanceCountersEnabled || minimalPerformanceCountersEnabled)
            {
                if (endpointDispatcher.SetPerfCounterId())
                {
                    ServiceModelPerformanceCounters counters;
                    lock (PerformanceCounters.perfCounterDictionarySyncObject)
                    {
                        if (!PerformanceCounters.PerformanceCountersForEndpoint.TryGetValue(endpointDispatcher.PerfCounterId, out counters))
                        {
                            counters = new ServiceModelPerformanceCounters(serviceHost, contractDescription, endpointDispatcher);
                            if (counters.Initialized)
                            {
                                PerformanceCounters.PerformanceCountersForEndpoint.Add(endpointDispatcher.PerfCounterId, counters);

                                int index = PerformanceCounters.PerformanceCountersForEndpointList.FindIndex(c => c == null);
                                if (index >= 0)
                                {
                                    PerformanceCounters.PerformanceCountersForEndpointList[index] = counters;
                                }
                                else
                                {
                                    PerformanceCounters.PerformanceCountersForEndpointList.Add(counters);
                                    index = PerformanceCounters.PerformanceCountersForEndpointList.Count - 1;
                                }
                                endpointDispatcher.PerfCounterInstanceId = index;
                            }
                            else
                            {
                                return;
                            }
                        }
                    }

                    ServiceModelPerformanceCountersEntry countersEntry;
                    lock (PerformanceCounters.perfCounterDictionarySyncObject)
                    {
                        if (!PerformanceCounters.PerformanceCountersForBaseUri.TryGetValue(endpointDispatcher.PerfCounterBaseId, out countersEntry))
                        {
                            if (performanceCountersEnabled)
                            {
                                countersEntry = new ServiceModelPerformanceCountersEntry(serviceHost.Counters);
                            }
                            else if (minimalPerformanceCountersEnabled)
                            {
                                countersEntry = new ServiceModelPerformanceCountersEntry(serviceHost.DefaultCounters);
                            }
                            PerformanceCounters.PerformanceCountersForBaseUri.Add(endpointDispatcher.PerfCounterBaseId, countersEntry);
                        }
                        countersEntry.Add(counters);
                    }
                }
            }
        }