Example #1
0
        /// <summary>
        /// Load all the WebApi Controllers in an assembly.
        /// </summary>
        /// <param name="webserver">The webserver instance.</param>
        /// <param name="assembly">The assembly to load WebApi Controllers from. Leave null to load from the currently executing assembly.</param>
        /// <returns>An instance of webserver.</returns>
        /// <exception cref="System.ArgumentNullException">webserver.</exception>
        public static IWebServer LoadApiControllers(this IWebServer webserver, Assembly assembly = null)
        {
            if (webserver == null)
            {
                throw new ArgumentNullException(nameof(webserver));
            }

            var types          = (assembly ?? Assembly.GetEntryAssembly()).GetTypes();
            var apiControllers = types
                                 .Where(x => x.GetTypeInfo().IsClass &&
                                        !x.GetTypeInfo().IsAbstract &&
                                        x.GetTypeInfo().IsSubclassOf(typeof(WebApiController)))
                                 .ToArray();

            foreach (var apiController in apiControllers)
            {
                if (webserver.Module <WebApiModule>() == null)
                {
                    webserver = webserver.WithWebApi();
                }

                webserver.Module <WebApiModule>().RegisterController(apiController);
                $"Registering WebAPI Controller '{apiController.Name}'".Debug();
            }

            return(webserver);
        }