示例#1
0
        /// <summary>Registers automatically discovered <see cref="IController" /> implementations.</summary>
        /// <param name="container">The container.</param>
        /// <param name="controllerDetailsAction">The controller details action.</param>
        /// <returns>The <paramref name="container" /> itself.</returns>
        public static IComponentProvider WithAutodiscoveredControllers(this IComponentProvider container, Action <Type, ControllerInfo> controllerDetailsAction = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            var assemblies = UrsaConfigurationSection.GetInstallerAssemblies();

            container.RegisterAll <IController>(assemblies);
            var controllers = container.ResolveAllTypes <IController>();

            container.RegisterControllerRelatedTypes(controllers);
            var registeredEntryPoints = new List <string>();

            foreach (var controller in controllers.Where(controller => !controller.IsDescriptionController()))
            {
                var descriptionBuilder = (IHttpControllerDescriptionBuilder)container.Resolve(typeof(IHttpControllerDescriptionBuilder <>).MakeGenericType(controller));
                var description        = descriptionBuilder.BuildDescriptor();
                if ((description.EntryPoint != null) && (!registeredEntryPoints.Contains(description.EntryPoint.ToString())))
                {
                    container.RegisterEntryPointControllerDescriptionBuilder(description.EntryPoint.Url);
                    registeredEntryPoints.Add(description.EntryPoint.ToString());
                }

                if (controllerDetailsAction != null)
                {
                    controllerDetailsAction(controller, description);
                }
            }

            return(container);
        }