public static TInterface Resolve <TInterface>()
            where TInterface : class
        {
            var type = typeof(TInterface);
            var id   = GetTypeId(type);

            lock (internals)
            {
                object   obj;
                IFactory factory;
                if (services.ContainsKey(id) && (obj = services[id]) != null)
                {
                    return((TInterface)services[id]);
                }
                else if (factories.ContainsKey(id) && (factory = factories[id]) != null)
                {
                    var instance = factory.Create();
                    if (!(factory is FactoryFactoryService))
                    {
                        services[id] = instance;
                    }
                    TraceEx.Info("ApplicationServices", "Created instance of " + (instance != null ? instance.ToString() : id.ToString()));
                    return((TInterface)instance);
                }
            }

            return(null);
        }
        /// <summary>
        /// Unregisters a service with from specified interface type.
        /// </summary>
        /// <typeparam name="TInterface">The type of the interface.</typeparam>
        public static void Unregister <TInterface>()
            where TInterface : class
        {
            var type = typeof(TInterface);
            var id   = type.GUID;

            lock (internals)
            {
                if (services.ContainsKey(id))
                {
                    services.Remove(id);
                    TraceEx.Info("ApplicationServices", "Removed instance for " + type.Name);
                }
            }
        }
        /// <summary>
        /// Drops the instance of a service registered with the TInterface type (does not call <see cref="IDisposable.Dispose"/>).
        /// Does not unregister the factory (the object will be recreated if resolved).
        /// </summary>
        public static void DropInstance <TInterface>()
            where TInterface : class
        {
            var type = typeof(TInterface);
            var id   = GetTypeId(type);

            lock (internals)
            {
                if (services.ContainsKey(id))
                {
                    var instance = services[id];
                    services[id] = null;
                    TraceEx.Info("ApplicationServices", "Removed instance of " + (instance != null ? instance.ToString() : id.ToString()));
                }
            }
        }
        /// <summary>
        /// Registers a service with a specified interface type.
        /// The service is automatically instanciated on the first call to it using the default constructor.
        /// </summary>
        /// <typeparam name="TInterface">The resolving type (interface).</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
        /// <exception cref="ArgumentException">If service or factory already exists</exception>
        public static void Register <TInterface, TImplementation>()
            where TInterface : class
            where TImplementation : class, TInterface, new()
        {
            var type     = typeof(TInterface);
            var implType = typeof(TImplementation);
            var id       = GetTypeId(type);

            lock (internals)
            {
                ThrowIfRegisteredOrInstanciated(type, id);

                factories.Add(id, new LazyEmptyService(implType));
                TraceEx.Info("ApplicationServices", "Registered factory for " + type.Name);
            }
        }
        /// <summary>
        /// Registers a service with a specified interface type.
        /// The service is automatically instanciated on ALL call to it.
        /// </summary>
        /// <typeparam name="TInterface">The resolving type (interface).</typeparam>
        /// <param name="factory">The factory for the instantiation.</param>
        /// <exception cref="ArgumentException">If service or factory already exists</exception>
        public static void RegisterFactory <TInterface>(Func <TInterface> factory)
            where TInterface : class
        {
            var type = typeof(TInterface);
            var id   = GetTypeId(type);

            lock (internals)
            {
                ThrowIfRegisteredOrInstanciated(type, id);

#if SILVERLIGHT || NETFX_CORE
                factories.Add(id, new FactoryFactoryService(() => factory()));
#else
                factories.Add(id, new FactoryFactoryService(factory));
#endif
                TraceEx.Info("ApplicationServices", "Registered factory for " + type.Name);
            }
        }
        /// <summary>
        /// Drops an instance of a service with the specified reference (does not call <see cref="IDisposable.Dispose"/>).
        /// Does not unregister the factory (the object will be recreated if resolved).
        /// </summary>
        /// <param name="obj">The object to drop.</param>
        /// <exception cref="ArgumentNullException">if the argument is null</exception>
        public static void DropInstance(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            lock (internals)
            {
                foreach (var item in services.ToArray())
                {
                    if (item.Value == obj)
                    {
                        services.Remove(item.Key);
                        TraceEx.Info("ApplicationServices", "Removed instance for " + (item.Value != null ? item.Value.ToString() : item.Key.ToString()));
                    }
                }
            }
        }
        /// <summary>
        /// Registers a service with a specified resolving type and instance.
        /// </summary>
        /// <typeparam name="TImplementation">The resolving type (interface).</typeparam>
        /// <param name="service">The reference to the service instance.</param>
        /// <exception cref="ArgumentException">If service or factory already exists</exception>
        /// <exception cref="ArgumentNullException"></exception>
        public static void Register <TImplementation>(TImplementation service)
            where TImplementation : class
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }

            var type = typeof(TImplementation);
            var id   = GetTypeId(type);

            lock (internals)
            {
                ThrowIfRegisteredOrInstanciated(type, id);

                services.Add(id, service);
                TraceEx.Info("ApplicationServices", "Registered instance for " + type.Name);
            }
        }
        /// <summary>
        /// Registers the a service with a specified interface type.
        /// The service is automatically instanciated on the first call to it.
        /// </summary>
        /// <typeparam name="TInterface">The type of the interface.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
        /// <param name="service">The reference to the service instance.</param>
        public static void Register <TInterface, TImplementation>()
            where TInterface : class
            where TImplementation : class, TInterface, new()
        {
            var type     = typeof(TInterface);
            var implType = typeof(TImplementation);
            var id       = type.GUID;

            lock (internals)
            {
                if (services.ContainsKey(id))
                {
                    throw new ArgumentException("Service of type '" + type.Name + "' is already registered");
                }

                services.Add(id, new LazyEmptyService(implType));
                TraceEx.Info("ApplicationServices", "Registered implementation for " + type.Name);
            }
        }
        /// <summary>
        /// Clears all instances (and calls <see cref="IDisposable.Dispose"/>).
        /// </summary>
        public static void ClearInstances()
        {
            lock (internals)
            {
                if (services.Count > 0)
                {
                    foreach (var item in services.Values)
                    {
                        var disposable = item as IDisposable;
                        if (disposable != null)
                        {
                            disposable.Dispose();
                        }
                    }
                }

                services.Clear();
                TraceEx.Info("ApplicationServices", "Cleared");
            }
        }
        /// <summary>
        /// Registers the a service with a specified interface type.
        /// The service is automatically instanciated on the first call to it.
        /// </summary>
        /// <typeparam name="TInterface">The type of the interface.</typeparam>
        /// <param name="factory">The factory for the instantiation.</param>
        public static void Register <TInterface>(Func <TInterface> factory)
            where TInterface : class
        {
            var type = typeof(TInterface);
            var id   = type.GUID;

            lock (internals)
            {
                if (services.ContainsKey(id))
                {
                    throw new ArgumentException("Service of type '" + type.Name + "' is already registered");
                }

#if SILVERLIGHT
                services.Add(id, new LazyFactoryService(() => factory()));
#else
                services.Add(id, new LazyFactoryService(factory));
#endif
                TraceEx.Info("ApplicationServices", "Registered factory for " + type.Name);
            }
        }
        /// <summary>
        /// Registers the a service with a specified interface type.
        /// </summary>
        /// <typeparam name="TInterface">The type of the interface.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
        /// <param name="service">The reference to the service instance.</param>
        public static void Register <TImplementation>(TImplementation service)
            where TImplementation : class
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }

            var type = typeof(TImplementation);
            var id   = type.GUID;

            lock (internals)
            {
                if (services.ContainsKey(id))
                {
                    throw new ArgumentException("Service of type '" + type.Name + "' is already registered");
                }

                services.Add(id, service);
                TraceEx.Info("ApplicationServices", "Registered instance for " + type.Name);
            }
        }