Inheritance: IRegistration
Esempio n. 1
0
        private ServiceEntry <TService, TFunc> GetEntry <TService, TFunc>(string serviceName, bool throwIfMissing)
        {
            try
            {
                TService resolved;
                if (CheckAdapterFirst &&
                    Adapter != null &&
                    !Equals(default(TService), (resolved = Adapter.TryResolve <TService>())))
                {
                    return(new ServiceEntry <TService, TFunc>(
                               (TFunc)(object)(Func <Container, TService>)(c => resolved))
                    {
                        Owner = DefaultOwner,
                        Container = this,
                    });
                }
            }
            catch (Exception ex)
            {
                throw CreateAdapterException <TService>(ex);
            }

            var          key       = new ServiceKey(typeof(TFunc), serviceName);
            ServiceEntry entry     = null;
            Container    container = this;

            // Go up the hierarchy always for registrations.
            while (!container.TryGetServiceEntry(key, out entry) && container.parent != null)
            {
                container = container.parent;
            }

            if (entry != null)
            {
                if (entry.Reuse == ReuseScope.Container && entry.Container != this)
                {
                    entry = SetServiceEntry(key, ((ServiceEntry <TService, TFunc>)entry).CloneFor(this));
                }
            }
            else
            {
                try
                {
                    //i.e. if called Resolve<> for Constructor injection
                    if (throwIfMissing)
                    {
                        if (Adapter != null)
                        {
                            return(new ServiceEntry <TService, TFunc>(
                                       (TFunc)(object)(Func <Container, TService>)(c => Adapter.Resolve <TService>()))
                            {
                                Owner = DefaultOwner,
                                Container = this,
                            });
                        }
                        ThrowMissing <TService>(serviceName);
                    }
                    else
                    {
                        if (Adapter != null)
                        {
                            return(new ServiceEntry <TService, TFunc>(
                                       (TFunc)(object)(Func <Container, TService>)(c => Adapter.TryResolve <TService>()))
                            {
                                Owner = DefaultOwner,
                                Container = this,
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw CreateAdapterException <TService>(ex);
                }
            }

            return((ServiceEntry <TService, TFunc>)entry);
        }
Esempio n. 2
0
        private ServiceEntry <TService, TFunc> SetServiceEntry <TService, TFunc>(ServiceKey key, ServiceEntry <TService, TFunc> entry)
        {
            lock (services)
            {
                services[key] = entry;
                Interlocked.Exchange(ref servicesReadOnlyCopy, null);
            }

            return(entry);
        }