//todo remove this ugly hack
        internal ResolvedService Resolve(Type type, IEnumerable <string> contracts, bool isEnumerable)
        {
            EnsureNotDisposed();
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            var name = CreateServiceName(type, contracts);
            ContainerService result;

            if (ContainerService.ThreadInitializing)
            {
                const string formatMessage = "attempt to resolve [{0}] is prohibited to prevent possible deadlocks";
                var          message       = string.Format(formatMessage, name.Type.FormatName());
                result = ContainerService.Error(name, message);
            }
            else
            {
                var id = instanceCache.GetOrAdd(name, createId);
                if (!id.TryGet(out result))
                {
                    var activation = ResolutionContext.Push(this);
                    result = ResolveCore(name, false, null, activation.activated);
                    PopResolutionContext(activation, result, isEnumerable);
                }
            }
            return(new ResolvedService(result, containerContext, isEnumerable));
        }
Esempio n. 2
0
        internal object Create(ServiceName name, bool isEnumerable, object arguments)
        {
            Func <object> compiledFactory;
            var           hasPendingResolutionContext = ResolutionContext.HasPendingResolutionContext;

            if (arguments == null && factoryCache.TryGetValue(name, out compiledFactory) && !hasPendingResolutionContext)
            {
                return(compiledFactory());
            }
            var activation                = ResolutionContext.Push(this);
            ContainerService result       = null;
            List <string>    oldContracts = null;

            try
            {
                if (hasPendingResolutionContext)
                {
                    oldContracts = activation.activated.Contracts.Replace(name.Contracts);
                    name         = new ServiceName(name.Type);
                }
                result = ResolveCore(name, true, ObjectAccessor.Get(arguments), activation.activated);
            }
            finally
            {
                if (oldContracts != null)
                {
                    activation.activated.Contracts.Restore(oldContracts);
                }
                PopResolutionContext(activation, result, isEnumerable);
            }
            if (!hasPendingResolutionContext)
            {
                result.EnsureInitialized(containerContext, result);
            }
            result.CheckStatusIsGood(containerContext);
            if (isEnumerable)
            {
                return(result.GetAllValues());
            }
            result.CheckSingleValue(containerContext);
            return(result.Instances[0].Instance);
        }