Beispiel #1
0
        /// <summary>
        /// Sets up framework level activators for Controllers, TagHelpers and ViewComponents and adds additional sub dependency resolvers
        /// </summary>
        /// <param name="services"><see cref="IServiceCollection"/></param>
        /// <param name="container"><see cref="IWindsorContainer"/></param>
        /// <param name="configure">Configuration options for controlling registration and lifestyles of controllers, tagHelpers and viewComponents</param>
        /// <param name="serviceProviderFactory">Optional factory for creating a custom <see cref="IServiceProvider"/></param>
        public static IServiceProvider AddWindsor(this IServiceCollection services, IWindsorContainer container, Action <WindsorRegistrationOptions> configure = null, Func <IServiceProvider> serviceProviderFactory = null)
        {
            var options = new WindsorRegistrationOptions();

            configure?.Invoke(options);

            InstallWindsorIntegration(services, container);
            AddApplicationComponentsToWindsor(container, options);
            InstallFrameworkIntegration(services, container);
            return(InitialiseFrameworkServiceProvider(services, serviceProviderFactory, container));
        }
Beispiel #2
0
        private static void AddApplicationComponentsToWindsor(IWindsorContainer container, WindsorRegistrationOptions options)
        {
            // Applying default registrations, Lifestyle here is Scoped

            if (!options.ControllerAssemblyRegistrations.Any() && !options.ControllerComponentRegistrations.Any())
            {
                container.Register(Classes.FromAssemblyInThisApplication(options.EntryAssembly).BasedOn <ControllerBase>().LifestyleScoped());
            }

            if (!options.TagHelperAssemblyRegistrations.Any() && !options.TagHelperComponentRegistrations.Any())
            {
                container.Register(Classes.FromAssemblyInThisApplication(options.EntryAssembly).BasedOn <ITagHelper>().LifestyleScoped());
            }

            if (!options.ViewComponentAssemblyRegistrations.Any() && !options.ViewComponentComponentRegistrations.Any())
            {
                container.Register(Classes.FromAssemblyInThisApplication(options.EntryAssembly).BasedOn <ViewComponent>().LifestyleScoped());
            }

            // Assembly registrations

            foreach (var controllerRegistration in options.ControllerAssemblyRegistrations)
            {
                container.Register(Classes.FromAssemblyInThisApplication(controllerRegistration.Item1).BasedOn <ControllerBase>().Configure(x => x.LifeStyle.Is(controllerRegistration.Item2)));
            }

            foreach (var tagHelperRegistration in options.TagHelperAssemblyRegistrations)
            {
                container.Register(Classes.FromAssemblyInThisApplication(tagHelperRegistration.Item1).BasedOn <ITagHelper>().Configure(x => x.LifeStyle.Is(tagHelperRegistration.Item2)));
            }

            foreach (var viewComponentRegistration in options.ViewComponentAssemblyRegistrations)
            {
                container.Register(Classes.FromAssemblyInThisApplication(viewComponentRegistration.Item1).BasedOn <ViewComponent>().Configure(x => x.LifeStyle.Is(viewComponentRegistration.Item2)));
            }

            // Component registrations

            foreach (var controllerRegistration in options.ControllerComponentRegistrations)
            {
                container.Register(controllerRegistration);
            }

            foreach (var tagHelperRegistration in options.TagHelperComponentRegistrations)
            {
                container.Register(tagHelperRegistration);
            }

            foreach (var viewComponentRegistration in options.ViewComponentComponentRegistrations)
            {
                container.Register(viewComponentRegistration);
            }
        }
        private static void AddApplicationComponentsToWindsor(IWindsorContainer container, WindsorRegistrationOptions options)
        {
            if (!options.ControllerRegistrations.Any())
            {
                container.Register(Classes.FromAssemblyInThisApplication(options.EntryAssembly).BasedOn <Controller>().LifestyleScoped());
            }

            foreach (var controllerRegistration in options.ControllerRegistrations)
            {
                container.Register(Classes.FromAssemblyInThisApplication(controllerRegistration.Item1).BasedOn <Controller>().Configure(x => x.LifeStyle.Is(controllerRegistration.Item2)));
            }

            if (!options.TagHelperRegistrations.Any())
            {
                container.Register(Classes.FromAssemblyInThisApplication(options.EntryAssembly).BasedOn <ViewComponent>().LifestyleScoped());
            }

            foreach (var controllerRegistration in options.TagHelperRegistrations)
            {
                container.Register(Classes.FromAssemblyInThisApplication(controllerRegistration.Item1).BasedOn <ViewComponent>().Configure(x => x.LifeStyle.Is(controllerRegistration.Item2)));
            }

            if (!options.ViewComponentRegistrations.Any())
            {
                container.Register(Classes.FromAssemblyInThisApplication(options.EntryAssembly).BasedOn <TagHelper>().LifestyleScoped());
            }

            foreach (var controllerRegistration in options.ViewComponentRegistrations)
            {
                container.Register(Classes.FromAssemblyInThisApplication(controllerRegistration.Item1).BasedOn <TagHelper>().Configure(x => x.LifeStyle.Is(controllerRegistration.Item2)));
            }
        }