public static void RegisterFuncFactory <TService, TImpl>(
            this Container container,
            Lifestyle lifestyle = null)
            where TService : class
            where TImpl : class, TService
        {
            lifestyle = lifestyle ?? Lifestyle.Transient;

            // Register the Func<T> that resolves that instance.
            container.RegisterSingle(
                () =>
            {
                var producer = new InstanceProducer(
                    typeof(TService),
                    lifestyle.CreateRegistration <TService, TImpl>(container));

                Func <TService> instanceCreator =
                    () => (TService)producer.GetInstance();

                if (container.IsVerifying())
                {
                    instanceCreator.Invoke();
                }

                return(instanceCreator);
            });
        }
        public int IndexOf(TService item)
        {
            // InstanceProducers never return null, so we can short-circuit the operation and return -1.
            if (item == null)
            {
                return(-1);
            }

            for (int index = 0; index < this.producers.Count; index++)
            {
                InstanceProducer producer = this.producers[index].Value;

                // NOTE: We call GetInstance directly as we don't want to notify about the creation to the
                // ContainsServiceCreatedListeners here; created instances will not leak out of this method
                // and can, therefore, never cause Captive Dependencies.
                var instance = producer.GetInstance();

                if (instance.Equals(item))
                {
                    return(index);
                }
            }

            return(-1);
        }
示例#3
0
        public static void RegisterFuncFactory <TService, TImpl>(
            this Container container, Lifestyle lifestyle = null)
            where TService : class
            where TImpl : class, TService
        {
            lifestyle = lifestyle ?? Lifestyle.Transient;

            // Register the Func<T> that resolves that instance.
            container.RegisterSingleton <Func <TService> >(() =>
            {
                var producer = new InstanceProducer(typeof(TService),
                                                    lifestyle.CreateRegistration <TImpl>(container));
                producer.Registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent, "Ignored during explicit Func Registration");

                Func <TService> instanceCreator =
                    () => (TService)producer.GetInstance();

                //if (container.IsVerifying())
                //{
                //    instanceCreator.Invoke();
                //}

                return(instanceCreator);
            });
        }
        private static IEnumerable <T> GetStreamFromRegistration <T>(
            Container container, params Type[] serviceTypes) where T : class
        {
            var reg  = container.Collection.CreateRegistration <T>(serviceTypes);
            var prod = new InstanceProducer <IEnumerable <T> >(reg);

            return(prod.GetInstance());
        }
示例#5
0
        public void GetInstance_ForSingletonValueType_CanBeResolved()
        {
            // Arrange
            int expectedValue = 4;

            var container = new Container();

            var registration = Lifestyle.Singleton.CreateRegistration(typeof(int), expectedValue, container);
            var producer     = new InstanceProducer <int>(registration);

            // Act
            int actualValue = producer.GetInstance();

            // Assert
            Assert.AreEqual(expectedValue, actualValue);
        }
示例#6
0
        private static object GetSingletonInstance(InstanceProducer registration)
        {
            if (registration.Lifestyle != Lifestyle.Singleton)
            {
                throw new InvalidOperationException(string.Format(
                                                        "Service type {0} has been flagged as 'Single' using the ServiceBehaviorAttribute, " +
                                                        "causing WCF to hold on to this instance indefinitely, while {1} has been registered " +
                                                        "with the {2} lifestyle in the container. Please make sure that {1} is registered " +
                                                        "as Singleton as well, or mark {0} with " +
                                                        "[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] instead.",
                                                        registration.ServiceType.FullName,
                                                        registration.Registration.ImplementationType.FullName,
                                                        registration.Lifestyle.Name));
            }

            return(registration.GetInstance());
        }
        private static TService GetInstance(InstanceProducer producer)
        {
            var service = (TService)producer.GetInstance();

            // This check is an optimization that prevents always calling the helper method, while in the
            // happy path it is not needed.
            // This code is in the happy path, so we want the performance penalty to be minimal.
            // That's why we don't have a lock around this field access. This might cause the value to become
            // stale (when read by other threads), but that's not an issue here; other threads might still see
            // an old value (for some time), but we are actually only interested in getting notifications from
            // the same thread anyway.
            if (ControlledCollectionHelper.ContainsServiceCreatedListeners)
            {
                ControlledCollectionHelper.NotifyServiceCreatedListeners(producer);
            }

            return(service);
        }
示例#8
0
        private static object GetSingletonInstance(Type serviceType)
        {
            InstanceProducer registration = container.GetRegistration(serviceType);

            // Call GetInstance BEFORE checking the Lifestyle property; decorators might be applied at this
            // point, causing the lifestyle and registration properties to change.
            object singletonInstance = registration.GetInstance();

            if (registration.Lifestyle != Lifestyle.Singleton)
            {
                throw new InvalidOperationException(string.Format(
                                                        "Service type {0} has been flagged as 'Single' using the ServiceBehaviorAttribute, " +
                                                        "causing WCF to hold on to this instance indefinitely, while {1} has been registered " +
                                                        "with the {2} lifestyle in the container. Please make sure that {1} is registered " +
                                                        "as Singleton as well, or mark {0} with " +
                                                        "[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] instead.",
                                                        serviceType.FullName,
                                                        registration.Registration.ImplementationType.FullName,
                                                        registration.Lifestyle.Name));
            }

            return(singletonInstance);
        }
        private static object GetSingletonInstance(InstanceProducer registration)
        {
            if (registration.Lifestyle != Lifestyle.Singleton)
            {
                throw new InvalidOperationException(string.Format(
                    "Service type {0} has been flagged as 'Single' using the ServiceBehaviorAttribute, " +
                    "causing WCF to hold on to this instance indefinitely, while {1} has been registered " +
                    "with the {2} lifestyle in the container. Please make sure that {1} is registered " +
                    "as Singleton as well, or mark {0} with " +
                    "[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] instead.",
                    registration.ServiceType.FullName,
                    registration.Registration.ImplementationType.FullName,
                    registration.Lifestyle.Name));
            }

            return registration.GetInstance();
        }