Exemplo n.º 1
0
 /// <summary>
 /// Registers the specified service descriptor.
 /// </summary>
 /// <param name="registration"></param>
 void Register(ServiceDescriptor registration)
 {
     if (registration.ServiceType.GetTypeInfo().IsGenericTypeDefinition == false)
     {
         builder.Register(registration.ToComponentRegistration(lifetimeScopeTagForSingletons));
     }
     else
     {
         builder.AddRegistrationSource(registration.ToRegistrationSource(builder, lifetimeScopeTagForSingletons));
     }
 }
Exemplo n.º 2
0
 private void RegisterDefaultAdapters(IComponentRegistryBuilder componentRegistry)
 {
     this.RegisterGeneric(typeof(KeyedServiceIndex <,>)).As(typeof(IIndex <,>)).InstancePerLifetimeScope();
     componentRegistry.AddRegistrationSource(new CollectionRegistrationSource());
     componentRegistry.AddRegistrationSource(new OwnedInstanceRegistrationSource());
     componentRegistry.AddRegistrationSource(new MetaRegistrationSource());
     componentRegistry.AddRegistrationSource(new LazyRegistrationSource());
     componentRegistry.AddRegistrationSource(new LazyWithMetadataRegistrationSource());
     componentRegistry.AddRegistrationSource(new StronglyTypedMetaRegistrationSource());
     componentRegistry.AddRegistrationSource(new GeneratedFactoryRegistrationSource());
 }
        /// <summary>
        /// Registers additional types to be made available in the container.
        /// </summary>
        /// <param name="configurationAction">Configuration for the <see cref="ContainerBuilder"/></param>
        public void RegisterTypes(Action <ContainerBuilder> configurationAction)
        {
            if (configurationAction == null)
            {
                throw new ArgumentNullException(nameof(configurationAction));
            }
            var builder = new ContainerBuilder();

            configurationAction(builder);
            var container = builder.Build();

            _additionalRegistrations.Add(container);
            // We need to re-add components back to the root scope.
            // This is important so that the new registrations are available,
            // for example when resolving singletons that have not yet been resolved.
            // Without this, singletons would not get any of the registrations done with RegisterTypes,
            // even if they were resolved after additional registrations.
            var registrationSource = new ExternalRegistrySource(container.ComponentRegistry);

            _componentRegistration.AddRegistrationSource(registrationSource);
        }
        private static void ScanAssemblies(IEnumerable <Assembly> assemblies, IComponentRegistryBuilder cr, IRegistrationBuilder <object, OpenGenericScanningActivatorData, DynamicRegistrationStyle> rb)
        {
            rb.ActivatorData.Filters.Add(t =>
                                         rb.RegistrationData.Services.OfType <IServiceWithType>().All(swt =>
                                                                                                      t.IsOpenGenericTypeOf(swt.ServiceType)));

            var types = assemblies.SelectMany(a => a.GetLoadableTypes())
                        .Where(t => t.IsGenericTypeDefinition)
                        .CanBeRegistered(rb.ActivatorData);

            Func <Type, IRegistrationBuilder <object, ReflectionActivatorData, DynamicRegistrationStyle> > scannedConstructor =
                (type) => new RegistrationBuilder <object, ReflectionActivatorData, DynamicRegistrationStyle>(
                    new TypedService(type),
                    new ReflectionActivatorData(type),
                    new DynamicRegistrationStyle());

            Action <IComponentRegistryBuilder, IRegistrationBuilder <object, ReflectionActivatorData, DynamicRegistrationStyle> > register =
                (cr, scanned) => cr.AddRegistrationSource(new OpenGenericRegistrationSource(scanned.RegistrationData, scanned.ResolvePipeline, scanned.ActivatorData));

            ScanTypesTemplate(types, cr, rb, scannedConstructor, register);
        }