// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #if DNX451 using System; using System.Collections.Concurrent; using Autofac; using Autofac.Core; namespace MvcSample.Web { /// /// Summary description for MonitoringModule /// public class MonitoringModule : Module { private static ConcurrentDictionary, int> _registrations = new ConcurrentDictionary, int>(); private static ConcurrentDictionary, object> _instances = new ConcurrentDictionary, object>(); public static readonly ConcurrentDictionary, int> InstanceCount = new ConcurrentDictionary, int>(); protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) { registration.Activating += Registration_Activating; registration.Activated += Registration_Activated; } public Tuple GetKey(IComponentRegistration context) { var activator = context.Activator; var lifeTime = context.Lifetime; var limitType = context.Activator.LimitType; var key = new Tuple(limitType, lifeTime); return key; } private void Registration_Activated(object sender, ActivatedEventArgs e) { object instance; var key = GetKey(e.Component); if (_instances.TryGetValue(key, out instance)) { bool same = (e.Instance == instance); InstanceCount.AddOrUpdate(key, 1, (_, count) => same ? 1 : count + 1); } } private void Registration_Activating(object sender, ActivatingEventArgs e) { var key = GetKey(e.Component); _registrations.AddOrUpdate(key, 1, (k, value) => value + 1); _instances.GetOrAdd(key, e.Instance); } private void Registration_Preparing(object sender, PreparingEventArgs e) { foreach (var param in e.Parameters) { Console.WriteLine(param.ToString()); } } public static void Clear() { //string count = InstanceCount.Select(kvp => kvp.Value).Aggregate((c, n) => c + n).ToString() + " instances from " + InstanceCount.Count + " types"; InstanceCount.Clear(); _instances.Clear(); _registrations.Clear(); // return count; } } } #endif