protected override void AttachToRegistrationSource(IComponentRegistry componentRegistry, IRegistrationSource registrationSource)
        {
            base.AttachToRegistrationSource(componentRegistry, registrationSource);

            var message = new RegistrationSourceAddedMessage(_modelMapper.GetRegistrationSourceModel(registrationSource));
            Send(message);
        }
示例#2
0
 public void Include(IRegistrationSource source)
 {
     if (IsInitialized)
         BeginInitialization(new[] { source });
     else if (IsInitializing)
         _sourcesToQuery.Enqueue(source);
 }
 /// <summary>
 /// Construct an instance of the <see cref="RegistrationSourceAddedEventArgs"/> class.
 /// </summary>
 /// <param name="componentRegistry">The registry to which the source was added.</param>
 /// <param name="registrationSource">The source that was added.</param>
 /// <exception cref="ArgumentNullException"></exception>
 public RegistrationSourceAddedEventArgs(IComponentRegistry componentRegistry, IRegistrationSource registrationSource)
 {
     if (componentRegistry == null) throw new ArgumentNullException("componentRegistry");
     if (registrationSource == null) throw new ArgumentNullException("registrationSource");
     _componentRegistry = componentRegistry;
     _registrationSource = registrationSource;
 }
 public void AddRegistrationSource(IRegistrationSource source)
 {
     lock (_synchRoot)
     {
         _dynamicRegistration.Push(source);
     }
 }
示例#5
0
 public void RegisterSource(IRegistrationSource source)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     sources.Add(source);
 }
        /// <summary>
        /// Construct an instance of the <see cref="RegistrationSourceAddedEventArgs"/> class.
        /// </summary>
        /// <param name="componentRegistry">The registry to which the source was added.</param>
        /// <param name="registrationSource">The source that was added.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public RegistrationSourceAddedEventArgs(IComponentRegistry componentRegistry, IRegistrationSource registrationSource)
        {
            if (componentRegistry == null) throw new ArgumentNullException(nameof(componentRegistry));
            if (registrationSource == null) throw new ArgumentNullException(nameof(registrationSource));

            ComponentRegistry = componentRegistry;
            RegistrationSource = registrationSource;
        }
示例#7
0
        /// <summary>
        /// Add a registration source to the container.
        /// </summary>
        /// <param name="registrationSource">The registration source to add.</param>
        /// <returns>
        /// The <see cref="ISourceRegistrar"/> to allow additional chained registration source registrations.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="registrationSource" /> is <see langword="null" />.
        /// </exception>
        public ISourceRegistrar RegisterSource(IRegistrationSource registrationSource)
        {
            if (registrationSource == null)
            {
                throw new ArgumentNullException(nameof(registrationSource));
            }

            _builder.RegisterCallback(cr => cr.AddRegistrationSource(registrationSource));
            return(this);
        }
        internal ContainerRuntime(
            IRegistrationSource registrationSource,
            IContainerBackend backend)
        {
            _registrationSource = registrationSource ?? throw new ArgumentNullException(nameof(registrationSource));
            _backend            = backend ?? throw
                                      new ArgumentNullException(nameof(backend));

            // We create a runtime scope to handle the storage and retrieval of singleton services.
            _runtimeScope = (ContainerScope)CreateScope();
        }
示例#9
0
        /// <summary>
        /// Obtains fake <see cref="ServiceDescriptor"/> entries for the given <see cref="IRegistrationSource"/>.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        IEnumerable <ServiceDescriptor> GetServiceDescriptors(IRegistrationSource source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (source is ServiceDescriptorRegistrationSource serviceDescriptorSource)
            {
                yield return(serviceDescriptorSource.ServiceDescriptors);
            }
        }
 public static void RegisterSource(this ContainerBuilder builder, IRegistrationSource registrationSource)
 {
     if (builder == null)
     {
         throw new ArgumentNullException("builder");
     }
     if (registrationSource == null)
     {
         throw new ArgumentNullException("registrationSource");
     }
     builder.RegisterCallback(cr => cr.AddRegistrationSource(registrationSource));
 }
 /// <summary>
 /// Construct an instance of the <see cref="RegistrationSourceAddedEventArgs"/> class.
 /// </summary>
 /// <param name="componentRegistry">The registry to which the source was added.</param>
 /// <param name="registrationSource">The source that was added.</param>
 /// <exception cref="ArgumentNullException"></exception>
 public RegistrationSourceAddedEventArgs(IComponentRegistry componentRegistry, IRegistrationSource registrationSource)
 {
     if (componentRegistry == null)
     {
         throw new ArgumentNullException("componentRegistry");
     }
     if (registrationSource == null)
     {
         throw new ArgumentNullException("registrationSource");
     }
     _componentRegistry  = componentRegistry;
     _registrationSource = registrationSource;
 }
        /// <inheritdoc />
        public void AddRegistrationSource(IRegistrationSource source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            _dynamicRegistrationSources.Insert(0, source);

            var handler = RegistrationSourceAdded;

            handler?.Invoke(this, source);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RegistrationSourceAddedEventArgs"/> class.
        /// </summary>
        /// <param name="componentRegistry">The registry to which the source was added.</param>
        /// <param name="registrationSource">The source that was added.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public RegistrationSourceAddedEventArgs(IComponentRegistry componentRegistry, IRegistrationSource registrationSource)
        {
            if (componentRegistry == null)
            {
                throw new ArgumentNullException(nameof(componentRegistry));
            }
            if (registrationSource == null)
            {
                throw new ArgumentNullException(nameof(registrationSource));
            }

            ComponentRegistry  = componentRegistry;
            RegistrationSource = registrationSource;
        }
示例#14
0
        /// <summary>
        /// Add a registration source to the container.
        /// </summary>
        /// <param name="builder">The builder to register the registration source via.</param>
        /// <param name="registrationSource">The registration source to add.</param>
        /// <returns>
        /// The <see cref="ISourceRegistrar"/> to allow additional chained registration source registrations.
        /// </returns>
        public static ISourceRegistrar RegisterSource(this ContainerBuilder builder, IRegistrationSource registrationSource)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (registrationSource == null)
            {
                throw new ArgumentNullException(nameof(registrationSource));
            }

            var registrar = new SourceRegistrar(builder);

            return(registrar.RegisterSource(registrationSource));
        }
        /// <inheritdoc />
        public void AddRegistrationSource(IRegistrationSource source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            lock (_synchRoot)
            {
                _dynamicRegistrationSources.Insert(0, source);
                foreach (var serviceRegistrationInfo in _serviceInfo)
                {
                    serviceRegistrationInfo.Value.Include(source);
                }
            }
        }
示例#16
0
        /// <summary>
        ///     Resolves a contract (include subcontracts).
        /// </summary>
        /// <param name="contractType">The contract type.</param>
        /// <param name="arguments">The arguments.</param>
        /// <param name="namedArguments">The named arguments.</param>
        /// <returns>The resolved instance as object.</returns>
        private object ResolveInternal(Type contractType, IEnumerable <object> arguments,
                                       IDictionary <string, object> namedArguments)
        {
            RegistrationItem registrationItem;

            if (!_registrationContainer.TryGetRegistration(contractType, out registrationItem))
            {
                if (_registrationContainer.HasDuplicateRegistration(contractType))
                {
                    throw new RegistrationNotFoundException(
                              Resources.RegistrationNotFoundBecauseDuplicate.FormatWith(contractType), contractType);
                }

                // No registration found yet, try to create one with available registration sources.
                IRegistrationSource registrationSourceToUse = null;

                for (int i = 0; i < _registrationContainer.RegistrationSources.Count; i++)
                {
                    var currentRegistrationSource = _registrationContainer.RegistrationSources[i];

                    if (currentRegistrationSource.SourceSupportsTypeSelector(contractType))
                    {
                        registrationSourceToUse = currentRegistrationSource;
                        break;
                    }
                }

                if (registrationSourceToUse == null)
                {
                    throw new RegistrationNotFoundException(
                              Resources.RegistrationNotFoundFormat.FormatWith(contractType), contractType);
                }

                registrationItem = registrationSourceToUse.GetRegistrationFor(contractType, this);

                _registrationContainer.AddRegistration(registrationItem);
            }

            // Add runtime arguments to registration.
            AddArgumentsToRegistration(registrationItem, arguments, namedArguments);

            // Activate existing registration.
            var result = this.Resolve(registrationItem);

            return(result);
        }
        public override RegistrationResult Register(DependencyMetadata dependencyMetadata)
        {
            if (dependencyMetadata.ImplementedInterfaces.Contains(typeof(IRegistrationSource)))
            {
                //we are using AutoFac's module registration
                IRegistrationSource module = Activator.CreateInstance(dependencyMetadata.DependencyType.AsType()) as IRegistrationSource;

                if (module != null)
                {
                    Builder.RegisterSource(module);
                    return(new RegistrationResult(this, true, Produce(new DependencyRegistration {
                        DependencyTypeName = dependencyMetadata.DependencyType.Name
                    })));
                }
            }
            return(new RegistrationResult(this, false));
        }
        /// <summary>
        /// Add a registration source that will provide registrations on-the-fly.
        /// </summary>
        /// <param name="source">The source to register.</param>
        public void AddRegistrationSource(IRegistrationSource source)
        {
            //if (source == null) throw new ArgumentNullException("source");
            lock (_synchRoot)
            {
                _dynamicRegistrationSources.AddFirst(source);
                _dynamicRegistrationSourcesArray = null;
                foreach (var siv in _serviceInfo.Values)
                {
                    siv.Include(source);
                }

                var rsa = RegistrationSourceAdded;
                if (rsa != null)
                {
                    rsa(this, new RegistrationSourceAddedEventArgs(this, source));
                }
            }
        }
示例#19
0
        /// <summary>
        /// Add a registration source that will provide registrations on-the-fly.
        /// </summary>
        /// <param name="source">The source to register.</param>
        public void AddRegistrationSource(IRegistrationSource source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            lock (_synchRoot)
            {
                _dynamicRegistrationSources.Insert(0, source);
                foreach (var serviceRegistrationInfo in _serviceInfo)
                {
                    serviceRegistrationInfo.Value.Include(source);
                }

                var handler = RegistrationSourceAdded;
                handler?.Invoke(this, new RegistrationSourceAddedEventArgs(this, source));
            }
        }
示例#20
0
        private ServiceRegistrationInfo GetInitializedServiceInfo(Service service)
        {
            ServiceRegistrationInfo serviceInfo = this.GetServiceInfo(service);

            if (!serviceInfo.IsInitialized)
            {
                if (!serviceInfo.IsInitializing)
                {
                    serviceInfo.BeginInitialization(this._dynamicRegistrationSources);
                }
                while (serviceInfo.HasSourcesToQuery)
                {
                    Func <IRegistrationSource, bool> predicate = null;
                    IRegistrationSource next = serviceInfo.DequeueNextSource();
                    foreach (IComponentRegistration registration in next.RegistrationsFor(service, new Func <Service, IEnumerable <IComponentRegistration> >(this.RegistrationsFor)))
                    {
                        foreach (Service service2 in registration.Services)
                        {
                            ServiceRegistrationInfo info2 = this.GetServiceInfo(service2);
                            if (!info2.IsInitialized)
                            {
                                if (!info2.IsInitializing)
                                {
                                    if (predicate == null)
                                    {
                                        predicate = src => src != next;
                                    }
                                    info2.BeginInitialization(this._dynamicRegistrationSources.Where <IRegistrationSource>(predicate));
                                }
                                else
                                {
                                    info2.SkipSource(next);
                                }
                            }
                        }
                        this.AddRegistration(registration, true);
                    }
                }
                serviceInfo.CompleteInitialization();
            }
            return(serviceInfo);
        }
        void UpdateInitialisedAdapters(IComponentRegistration registration)
        {
            var adapterServices = new List <Service>(_serviceInfo.Count);

            foreach (var kv in _serviceInfo)
            {
                if (kv.Value.ShouldRecalculateAdaptersOn(registration))
                {
                    adapterServices.Add(kv.Key);
                }
            }

            if (adapterServices.Count == 0)
            {
                return;
            }

            Debug.WriteLine(
                "[Autofac] Component '{0}' provides services that have already been adapted. Consider refactoring to ContainerBuilder.Build() rather than Update().",
                registration);

            var drs = _dynamicRegistrationSourcesArray;

            if (drs == null)
            {
                drs = new IRegistrationSource[_dynamicRegistrationSources.Count];
                _dynamicRegistrationSources.CopyTo(drs, 0);
                _dynamicRegistrationSourcesArray = drs;
            }

            var adaptationSandbox = new AdaptationSandbox(
                drs.Where(rs => rs.IsAdapterForIndividualComponents),
                registration,
                adapterServices);

            var adapters = adaptationSandbox.GetAdapters();

            foreach (var adapter in adapters)
            {
                AddRegistration(adapter, true);
            }
        }
示例#22
0
 public void AddRegistrationSource(IRegistrationSource source)
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     lock (this._synchRoot)
     {
         this._dynamicRegistrationSources.Insert(0, source);
         foreach (KeyValuePair <Service, ServiceRegistrationInfo> pair in this._serviceInfo)
         {
             pair.Value.Include(source);
         }
         EventHandler <RegistrationSourceAddedEventArgs> registrationSourceAdded = this.RegistrationSourceAdded;
         if (registrationSourceAdded != null)
         {
             registrationSourceAdded(this, new RegistrationSourceAddedEventArgs(this, source));
         }
     }
 }
示例#23
0
        public IContainerBuilder UseRegistrationSource(IRegistrationSource source)
        {
            _registrationSource = source ?? throw new ArgumentNullException(nameof(source));

            return(this);
        }
        protected override void AttachToRegistrationSource(IComponentRegistry componentRegistry, IRegistrationSource registrationSource)
        {
            base.AttachToRegistrationSource(componentRegistry, registrationSource);

            var message = new RegistrationSourceAddedMessage(ModelMapper.GetRegistrationSourceModel(registrationSource));

            Send(message);
        }
        public void SkipSource(IRegistrationSource source)
        {
            EnforceDuringInitialization();

            _sourcesToQuery = new Queue<IRegistrationSource>(_sourcesToQuery.Where(rs => rs != source));
        }
示例#26
0
        /// <summary>
        /// Add a registration source that will provide registrations on-the-fly.
        /// </summary>
        /// <param name="source">The source to register.</param>
        public void AddRegistrationSource(IRegistrationSource source)
        {
            if (source == null) throw new ArgumentNullException(nameof(source));

            lock (_synchRoot)
            {
                _dynamicRegistrationSources.Insert(0, source);
                foreach (var serviceRegistrationInfo in _serviceInfo)
                    serviceRegistrationInfo.Value.Include(source);

                var handler = RegistrationSourceAdded;
                handler?.Invoke(this, new RegistrationSourceAddedEventArgs(this, source));
            }
        }
示例#27
0
		void UpdateInitialisedAdapters(IComponentRegistration registration)
		{
			var adapterServices = new List<Service>(_serviceInfo.Count);
			foreach (var kv in _serviceInfo)
				if (kv.Value.ShouldRecalculateAdaptersOn(registration))
					adapterServices.Add(kv.Key);

			if (adapterServices.Count == 0)
				return;

			Debug.WriteLine(String.Format(
				"[Autofac] Component '{0}' provides services that have already been adapted. Consider refactoring to ContainerBuilder.Build() rather than Update().",
				registration));

			var drs = _dynamicRegistrationSourcesArray;
			if (drs == null)
			{
				drs = new IRegistrationSource[_dynamicRegistrationSources.Count];
				_dynamicRegistrationSources.CopyTo(drs, 0);
				_dynamicRegistrationSourcesArray = drs;
			}

			var adaptationSandbox = new AdaptationSandbox(
				drs.Where(rs => rs.IsAdapterForIndividualComponents),
				registration,
				adapterServices);

			var adapters = adaptationSandbox.GetAdapters();
			foreach (var adapter in adapters)
				AddRegistration(adapter, true);
		}
示例#28
0
 public void AddRegistrationSource(IRegistrationSource source)
 {
     WriteRegistry.AddRegistrationSource(source);
 }
示例#29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RegistrationSourceAddedEventArgs"/> class.
 /// </summary>
 /// <param name="componentRegistry">The registry to which the source was added.</param>
 /// <param name="registrationSource">The source that was added.</param>
 /// <exception cref="ArgumentNullException"></exception>
 public RegistrationSourceAddedEventArgs(IComponentRegistryBuilder componentRegistry, IRegistrationSource registrationSource)
 {
     ComponentRegistry  = componentRegistry ?? throw new ArgumentNullException(nameof(componentRegistry));
     RegistrationSource = registrationSource ?? throw new ArgumentNullException(nameof(registrationSource));
 }
 /// <summary>
 /// Override to perform module-specific processing on a registration source.
 /// </summary>
 /// <remarks>This method will be called for all existing <i>and future</i> sources
 /// - ordering is not important.</remarks>
 /// <param name="componentRegistry">The component registry into which the source was added.</param>
 /// <param name="registrationSource">The registration source.</param>
 protected virtual void AttachToRegistrationSource(
     IComponentRegistry componentRegistry,
     IRegistrationSource registrationSource)
 {
 }
示例#31
0
 public void AddRegistrationSource(IRegistrationSource source)
 {
     _componentRegistry.AddRegistrationSource(source);
 }
示例#32
0
 public RegistrationSourceModel GetRegistrationSourceModel(IRegistrationSource registrationSource)
 {
     return new RegistrationSourceModel(NewId(), registrationSource.GetType().AssemblyQualifiedName, registrationSource.ToString());
 }
示例#33
0
 public void AddRegistrationSource(IRegistrationSource source)
 {
     WriteRegistry.AddRegistrationSource(source);
 }
示例#34
0
 /// <summary>
 /// Override to perform module-specific processing on a registration source.
 /// </summary>
 /// <remarks>This method will be called for all existing <i>and future</i> sources
 /// - ordering is not important.</remarks>
 /// <param name="componentRegistry">The component registry into which the source was added.</param>
 /// <param name="registrationSource">The registration source.</param>
 protected virtual void AttachToRegistrationSource(
     IComponentRegistry componentRegistry, 
     IRegistrationSource registrationSource)
 {
 }
        public void SkipSource(IRegistrationSource source)
        {
            EnforceDuringInitialization();

            _sourcesToQuery = new Queue <IRegistrationSource>(_sourcesToQuery.Where(rs => rs != source));
        }
 private static IEnumerable <IRegistrationSource> ExcludeSource(IEnumerable <IRegistrationSource> sources, IRegistrationSource exclude)
 {
     foreach (var item in sources)
     {
         if (item != exclude)
         {
             yield return(item);
         }
     }
 }
 public void Include(IRegistrationSource source)
 {
     if (IsInitialized)
         BeginInitialization(new[] { source });
     else if (IsInitializing)
         _sourcesToQuery.Enqueue(source);
 }
示例#38
0
 public ContainerBuilder()
 {
     _registrationSource = new DefaultRegistrationSource();
     _backend            = new DefaultContainerBackend();
 }
示例#39
0
 /// <summary>
 /// Add a registration source to the container.
 /// </summary>
 /// <param name="registrationSource">The registration source to add.</param>
 public void RegisterSource(IRegistrationSource registrationSource)
 {
     global::Autofac.RegistrationExtensions.RegisterSource(this, registrationSource);
 }
示例#40
0
		/// <summary>
		/// Add a registration source that will provide registrations on-the-fly.
		/// </summary>
		/// <param name="source">The source to register.</param>
		public void AddRegistrationSource(IRegistrationSource source)
		{
			//if (source == null) throw new ArgumentNullException("source");
			lock (_synchRoot)
			{
				_dynamicRegistrationSources.AddFirst(source);
				_dynamicRegistrationSourcesArray = null;
				foreach (var siv in _serviceInfo.Values)
					siv.Include(source);

				var rsa = RegistrationSourceAdded;
				if (rsa != null)
					rsa(this, new RegistrationSourceAddedEventArgs(this, source));
			}
		}