/// <summary>
 /// Returns true if the parameter is able to provide a value to a particular site.
 /// </summary>
 /// <param name="pi">Constructor, method, or property-mutator parameter.</param>
 /// <param name="context">The component context in which the value is being provided.</param>
 /// <param name="valueProvider">If the result is true, the valueProvider parameter will
 /// be set to a function that will lazily retrieve the parameter value. If the result is false,
 /// will be set to null.</param>
 /// <returns>True if a value can be supplied; otherwise, false.</returns>
 public override bool CanSupplyValue(ParameterInfo pi, IComponentContext context, out Func<object> valueProvider)
 {
     IComponentRegistration registration;
     if (context.ComponentRegistry.TryGetRegistration(new TypedService(pi.ParameterType), out registration))
     {
         valueProvider = () => context.ResolveComponent(registration, Enumerable.Empty<Parameter>());
         return true;
     }
     valueProvider = null;
     return false;
 }
Example #2
0
        /*
         * This method create instance via reflection. Try evaluating its parameters and
         * inject them using componentContext.
         *
         * No public members are allowed to add.
         */

        public object Activate(IComponentContext componentContext)
        {
            var constructors = serviceType.GetConstructors();

            if (constructors.Length != 1)
            {
                throw new DependencyResolutionException();
            }
            var constructor = constructors.First();

            ParameterInfo[] parametesTypess = constructor.GetParameters();
            var             parameters      = parametesTypess.Select(p => componentContext.ResolveComponent(new TypedService(p.ParameterType))).ToArray();

            return(Activator.CreateInstance(serviceType, parameters));
        }
Example #3
0
        /*
         * This method create instance via reflection. Try evaluating its parameters and
         * inject them using componentContext.
         *
         * No public members are allowed to add.
         */

        public object Activate(IComponentContext componentContext)
        {
            var constructorInfos = serviceType
                                   .GetConstructors(BindingFlags.Instance | BindingFlags.Public).ToList();

            if (!constructorInfos.Any() || constructorInfos.Count > 1)
            {
                throw new DependencyResolutionException();
            }

            return(Activator
                   .CreateInstance(
                       serviceType, constructorInfos.Single().GetParameters()
                       .Select(param => componentContext.ResolveComponent(new TypedService(param.ParameterType))).ToArray()));
        }
        /*
         * This method create instance via reflection. Try evaluating its parameters and
         * inject them using componentContext.
         *
         * No public members are allowed to add.
         */

        public object Activate(IComponentContext componentContext)
        {
            var constructorInfos = serviceType.GetConstructors();

            if (!constructorInfos.Any() || constructorInfos.Length > 1)
            {
                throw new DependencyResolutionException();
            }
            ConstructorInfo constructorInfo = constructorInfos[0];

            var parameters = constructorInfo.GetParameters()
                             .Select(p => componentContext.ResolveComponent(new TypedService(p.ParameterType)))
                             .ToArray();

            return(Activator.CreateInstance(serviceType, parameters));
        }
Example #5
0
        public static T ResolveUnregistered <T>(
            this IComponentContext context,
            Type serviceType,
            IEnumerable <Parameter> parameters)
        {
            var scope = context.Resolve <ILifetimeScope>();

            using (var innerScope = scope.BeginLifetimeScope(b => b.RegisterType(serviceType)))
            {
                innerScope.ComponentRegistry.TryGetRegistration(
                    new TypedService(serviceType),
                    out var reg);

                return((T)context.ResolveComponent(reg, parameters));
            }
        }
Example #6
0
        public override bool CanSupplyValue(ParameterInfo pi, IComponentContext context, out Func <object> valueProvider)
        {
            IComponentRegistration registration;
            Func <object>          func = null;

            if (context.ComponentRegistry.TryGetRegistration(new TypedService(pi.ParameterType), out registration))
            {
                if (func == null)
                {
                    func = () => context.ResolveComponent(registration, Enumerable.Empty <Parameter>());
                }
                valueProvider = func;
                return(true);
            }
            valueProvider = null;
            return(false);
        }
Example #7
0
        /*
         * This method create instance via reflection. Try evaluating its parameters and
         * inject them using componentContext.
         *
         * No public members are allowed to add.
         */

        public object Activate(IComponentContext componentContext)
        {
            var constructors = serviceType.GetConstructors();

            if (1 != constructors.Length)
            {
                throw new DependencyResolutionException();
            }
            var constructorInfo = constructors[0];
            var parameters      = constructorInfo
                                  .GetParameters()
                                  .Select(p =>
                                          componentContext.ResolveComponent(new TypedService(p.ParameterType)))
                                  .ToArray();

            return(constructors[0].Invoke(parameters));
        }
Example #8
0
        /*
         * This method create instance via reflection. Try evaluating its parameters and
         * inject them using componentContext.
         *
         * No public members are allowed to add.
         */

        public object Activate(IComponentContext componentContext)
        {
            var constructorInfos = serviceType.GetConstructors(BindingFlags.Instance | BindingFlags.Public);

            if (constructorInfos.Length != 1)
            {
                throw new DependencyResolutionException();
            }
            var constructor = constructorInfos.Single();
            var parameters  = new List <object>();

            foreach (var i in constructor.GetParameters())
            {
                Type paramtype = i.ParameterType;
                var  instance  = componentContext.ResolveComponent(new TypedService(paramtype));
                parameters.Add(instance);
            }
            return(constructor.Invoke(parameters.ToArray()));
        }
Example #9
0
        /// <summary>
        /// Returns true if the parameter is able to provide a value to a particular site.
        /// </summary>
        /// <param name="pi">Constructor, method, or property-mutator parameter.</param>
        /// <param name="context">The component context in which the value is being provided.</param>
        /// <param name="valueProvider">If the result is true, the valueProvider parameter will
        /// be set to a function that will lazily retrieve the parameter value. If the result is false,
        /// will be set to null.</param>
        /// <returns>True if a value can be supplied; otherwise, false.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="pi" /> or <paramref name="context" /> is <see langword="null" />.
        /// </exception>
        public override bool CanSupplyValue(ParameterInfo pi, IComponentContext context, out Func <object> valueProvider)
        {
            if (pi == null)
            {
                throw new ArgumentNullException("pi");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            IComponentRegistration registration;

            if (context.ComponentRegistry.TryGetRegistration(new TypedService(pi.ParameterType), out registration))
            {
                valueProvider = () => context.ResolveComponent(registration, Enumerable.Empty <Parameter>());
                return(true);
            }
            valueProvider = null;
            return(false);
        }
        /*
         * This method create instance via reflection. Try evaluating its parameters and
         * inject them using componentContext.
         *
         * No public members are allowed to add.
         */

        public object Activate(IComponentContext componentContext)
        {
            ConstructorInfo constructorInfo;

            try
            {
                constructorInfo = serviceType.GetConstructors(BindingFlags.Instance | BindingFlags.Public).Single();
            }
            catch
            {
                throw new DependencyResolutionException();
            }

            var parameters = constructorInfo
                             .GetParameters()
                             .Select(p => componentContext.ResolveComponent(new TypedService(p.ParameterType)))
                             .ToArray();

            return(constructorInfo.Invoke(parameters));
        }
        public bool TryResolve(
            Type serviceType,
            out object instance)
        {
            if (_componentContext.TryResolve(serviceType, out instance))
            {
                return(true);
            }

            // Try resolving registrationless.

            var scope = _componentContext.Resolve <ILifetimeScope>();

            using (var innerScope = scope.BeginLifetimeScope(b => b.RegisterType(serviceType)))
            {
                var componentRegistration = default(IComponentRegistration);

                if (innerScope.ComponentRegistry.TryGetRegistration(
                        new TypedService(serviceType),
                        out componentRegistration))
                {
                    try
                    {
                        instance = _componentContext.ResolveComponent(
                            componentRegistration,
                            new Parameter[0]);

                        return(true);
                    }
                    catch (DependencyResolutionException)
                    {
                    }
                }
            }

            instance = null;

            return(false);
        }
Example #12
0
        /// <summary>
        /// Returns true if the parameter is able to provide a value to a particular site.
        /// </summary>
        /// <param name="pi">Constructor, method, or property-mutator parameter.</param>
        /// <param name="context">The component context in which the value is being provided.</param>
        /// <param name="valueProvider">If the result is true, the valueProvider parameter will
        /// be set to a function that will lazily retrieve the parameter value. If the result is false,
        /// will be set to null.</param>
        /// <returns>True if a value can be supplied; otherwise, false.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="pi" /> or <paramref name="context" /> is <see langword="null" />.
        /// </exception>
        public override bool CanSupplyValue(ParameterInfo pi, IComponentContext context, [NotNullWhen(returnValue: true)] out Func <object?>?valueProvider)
        {
            if (pi == null)
            {
                throw new ArgumentNullException(nameof(pi));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var service = new TypedService(pi.ParameterType);

            if (context.ComponentRegistry.TryGetServiceRegistration(service, out var implementation))
            {
                valueProvider = () => context.ResolveComponent(new ResolveRequest(service, implementation, Enumerable.Empty <Parameter>()));
                return(true);
            }

            valueProvider = null;
            return(false);
        }
Example #13
0
        static void StartStartableComponents(IComponentContext componentContext)
        {
            // We track which registrations have already been auto-activated by adding
            // a metadata value. If the value is present, we won't re-activate. This helps
            // in the container update situation.
            const string started = "__AutoActivated";
            object meta;

            foreach (var startable in componentContext.ComponentRegistry.RegistrationsFor(new TypedService(typeof(IStartable))).Where(r => !r.Metadata.TryGetValue(started, out meta)))
            {
                try
                {
                    var instance = (IStartable)componentContext.ResolveComponent(startable, Enumerable.Empty<Parameter>());
                    instance.Start();
                }
                finally
                {
                    startable.Metadata[started] = true;
                }
            }

            foreach (var registration in componentContext.ComponentRegistry.RegistrationsFor(new AutoActivateService()).Where(r => !r.Metadata.TryGetValue(started, out meta)))
            {
                try
                {
                    componentContext.ResolveComponent(registration, Enumerable.Empty<Parameter>());
                }
                catch (DependencyResolutionException ex)
                {
                    throw new DependencyResolutionException(String.Format(CultureInfo.CurrentCulture, ContainerBuilderResources.ErrorAutoActivating, registration), ex);
                }
                finally
                {
                    registration.Metadata[started] = true;
                }
            }
        }
 public static T ResolveNamed <T>(
     this IComponentContext componentContext,
     string name)
 {
     return((T)componentContext.ResolveComponent(new TypedNameService(typeof(T), name)));
 }
 public static object Resolve(
     this IComponentContext componentContext,
     Type serviceType)
 {
     return(componentContext.ResolveComponent(new TypedService(serviceType)));
 }
Example #16
0
 /// <inheritdoc/>
 protected override object ResolveInstance <T>(IComponentContext ctx, ResolveRequest request)
 => new Meta <T>((T)ctx.ResolveComponent(request), request.Registration.Target.Metadata);
 public object ResolveComponent(IComponentRegistration registration, IEnumerable <Parameter> parameters)
 {
     _profile.RecordDependency(_registration, registration);
     return(_context.ResolveComponent(registration, parameters));
 }
Example #18
0
 static void StartStartableComponents(IComponentContext componentContext)
 {
     var ts = new TypedService(typeof(IStartable));
     foreach (var startable in componentContext.ComponentRegistry.RegistrationsFor(ts))
     {
         var instance = (IStartable)componentContext.ResolveComponent(ts, startable, Enumerable.Empty<Parameter>());
         instance.Start();
     }
 }
 protected override object ResolveInstance <T>(IComponentContext ctx, ResolveRequest request)
 {
     return(new Mapped <T>((T)ctx.ResolveComponent(request)));
 }
Example #20
0
 private static THandler ResolveComponent(IComponentContext context, IComponentRegistration registration)
 {
     return((THandler)context.ResolveComponent(registration, AutofacHelper.NoParameters));
 }
Example #21
0
 public object ResolveComponent(global::Autofac.Core.IComponentRegistration registration, IEnumerable <global::Autofac.Core.Parameter> parameters)
 {
     return(_context.ResolveComponent(registration, parameters));
 }