Exemplo n.º 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>
        /// <param name="responseJsonException">if set to <c>true</c> [response json exception].</param>
        /// <returns>
        /// An instance of webserver.
        /// </returns>
        /// <exception cref="ArgumentNullException">webserver.</exception>
        /// <exception cref="System.ArgumentNullException">webserver.</exception>
        public static IWebServer LoadApiControllers(this IWebServer webserver, Assembly assembly = null, bool responseJsonException = false)
        {
            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(responseJsonException: responseJsonException);
                }

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

            return(webserver);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Add WebApi Controller to WebServer.
        /// </summary>
        /// <typeparam name="T">The type of Web API Controller.</typeparam>
        /// <param name="webserver">The webserver instance.</param>
        /// <param name="responseJsonException">if set to <c>true</c> [response json exception].</param>
        /// <returns>
        /// An instance of webserver.
        /// </returns>
        /// <exception cref="ArgumentNullException">webserver.</exception>
        public static IWebServer WithWebApiController <T>(this IWebServer webserver, bool responseJsonException = false)
            where T : WebApiController
        {
            if (webserver == null)
            {
                throw new ArgumentNullException(nameof(webserver));
            }

            if (webserver.Module <WebApiModule>() == null)
            {
                webserver.RegisterModule(new WebApiModule(responseJsonException));
            }

            webserver.Module <WebApiModule>().RegisterController <T>();

            return(webserver);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Load all the WebSockets in an assembly.
        /// </summary>
        /// <param name="webserver">The webserver instance.</param>
        /// <param name="assembly">The assembly to load WebSocketsServer types 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 LoadWebSockets(this IWebServer webserver, Assembly assembly = null)
        {
            if (webserver == null)
            {
                throw new ArgumentNullException(nameof(webserver));
            }

            var types = (assembly ?? Assembly.GetEntryAssembly()).GetTypes();

            foreach (var socketServer in types.Where(x => x.GetTypeInfo().BaseType == typeof(WebSocketsServer)))
            {
                if (webserver.Module <WebSocketsModule>() == null)
                {
                    webserver = webserver.WithWebSocket();
                }

                webserver.Module <WebSocketsModule>().RegisterWebSocketsServer(socketServer);
                $"Registering WebSocket Server '{socketServer.Name}'".Debug(nameof(LoadWebSockets));
            }

            return(webserver);
        }