public static void InitRoutingDepedencies(IServiceCollection services, ServiceProvider serviceProvider) { var routing = serviceProvider.GetService <IRouting>(); foreach (var route in routing.GetRoutes()) { var type = _assemblyLoader.GetType(route.ExecultAssembly, route.ExecuteType); if (type != null) { // Do Not add AppInstallerContoller controller if AppInstallStatus is Finish for security reason if (!(ApplicationConfig.AppInstallStatus == AppInstallStatus.Finish && type == typeof(AppInstallerContoller))) { services.AddTransient(type, type); } } } }
public object Exec(string execultAssembly, string executeType, string executeMethod, ParamContainer helper) { Type exeType = _assemblyLoader.GetType(execultAssembly, executeType); if (exeType == null) { string error = string.Format("Execute Type is null for {0} :: {1}", execultAssembly, executeType); _logger.Error(error, null); throw new Exception(error); } Type[] argTypes = new Type[] { typeof(string) }; object[] argValues = new object[] { helper }; dynamic obj = Activator.CreateInstance(exeType, argValues); var methodInfo = exeType.GetMethod(executeMethod); if (methodInfo != null) { return(methodInfo.Invoke(obj, null)); } else { return(null); } }
/// <summary> /// Initializes this DualityApp. Should be called before performing any operations within Duality. /// </summary> /// <param name="env"></param> /// <param name="context">The <see cref="ExecutionContext"/> in which Duality runs.</param> /// <param name="plugins"></param> /// <param name="launcherArgs"> /// Command line arguments to run this DualityApp with. /// Usually these are just the ones from the host application, passed on. /// </param> public static void Init(ExecutionEnvironment env, ExecutionContext context, IAssemblyLoader plugins, LauncherArgs launcherArgs) { if (initialized) { return; } // Process command line options if (launcherArgs.IsDebugging) { System.Diagnostics.Debugger.Launch(); } // Run from editor if (launcherArgs.IsRunFromEditor) { runFromEditor = true; } // If the core was compiled in debug mode and a debugger is attached, log // to the Debug channel, so we can put the VS output window to good use. #if DEBUG bool isDebugging = System.Diagnostics.Debugger.IsAttached; if (isDebugging) { // Only add a new Debug output if we don't already have one, and don't // log to a Console channel either. VS will automatically redirect Console // output to the Output window when debugging a non-Console application, // and we don't want to end up with double log entries. bool hasDebugOut = Logs.GlobalOutput.OfType <DebugLogOutput>().Any(); bool hasConsoleOut = Logs.GlobalOutput.OfType <TextWriterLogOutput>().Any(w => w.GetType().Name.Contains("Console")); if (!hasDebugOut && !hasConsoleOut) { Logs.AddGlobalOutput(new DebugLogOutput()); } } #endif environment = env; execContext = context; // Initialize the plugin manager { assemblyLoader = plugins ?? new Duality.Backend.Dummy.DummyAssemblyLoader(); Logs.Core.Write("Using '{0}' to load plugins.", assemblyLoader.GetType().Name); assemblyLoader.Init(); // Log assembly loading data for diagnostic purposes { Logs.Core.Write("Currently Loaded Assemblies:" + Environment.NewLine + "{0}", assemblyLoader.LoadedAssemblies.ToString( assembly => " " + LogFormat.Assembly(assembly), Environment.NewLine)); Logs.Core.Write("Plugin Base Directories:" + Environment.NewLine + "{0}", assemblyLoader.BaseDirectories.ToString( path => " " + path, Environment.NewLine)); Logs.Core.Write("Available Assembly Paths:" + Environment.NewLine + "{0}", assemblyLoader.AvailableAssemblyPaths.ToString( path => " " + path, Environment.NewLine)); } pluginManager.Init(assemblyLoader); pluginManager.PluginsRemoving += pluginManager_PluginsRemoving; pluginManager.PluginsRemoved += pluginManager_PluginsRemoved; } // Load all plugins. This needs to be done first, so backends and Types can be located. pluginManager.LoadPlugins(); // Initialize the system backend for system info and file system access InitBackend(out systemBack); // Load application and user data and submit a change event, so all settings are applied DualityApp.AppData.Load(); DualityApp.UserData.Load(); // Initialize the graphics backend InitBackend(out graphicsBack); // Initialize the audio backend InitBackend(out audioBack); sound = new SoundDevice(); // Initialize all core plugins, this may allocate Resources or establish references between plugins pluginManager.InitPlugins(); initialized = true; // Write environment specs as a debug log Logs.Core.Write( "DualityApp initialized" + Environment.NewLine + "Debug Mode: {0}" + Environment.NewLine + "Command line arguments: {1}", System.Diagnostics.Debugger.IsAttached, launcherArgs.ToString()); }
public virtual async Task Invoke(HttpContext context, IAuthorizationService authorizationService) { var route = _routing.GetRoute(_httpContextProxy.GetHttpMethod(), _httpContextProxy.GetURIAbsolutePath()); if (route != null && !string.IsNullOrEmpty(route.ExecultAssembly) && !string.IsNullOrEmpty(route.ExecuteType)) { if (AuthorizedRoute(context, route, authorizationService)) { var type = _assemblyLoader.GetType(route.ExecultAssembly, route.ExecuteType); if (type != null) { context.Response.Headers[CommonConst.CommonField.MODULE_NAME] = route.module_name; context.Response.Headers[CommonConst.CommonField.EXECUTE_TYPE] = route.ExecuteType; context.Response.Headers[CommonConst.CommonField.ROUTE] = $"{route.Method}:{route.Route}"; _logger.Debug(string.Format("Executing route:{0}", route.ToString())); var controller = _serviceResolver.Resolve(type); var method = controller.GetType().GetMethods().FirstOrDefault(f => f.Name == route.ExecuteMethod); if (method != null) { context.Response.ContentType = route.ContentType; object response = null; if (method.ReturnType.BaseType == typeof(Task)) { response = await(dynamic) method.Invoke(controller, null); } else { response = method.Invoke(controller, null); } RemoveHeaders(context); if (response != null) { if (method.ReturnType == typeof(string)) { await context.Response.WriteAsync((response as string)); } else if (method.ReturnType == typeof(byte[])) { var byteResponse = response as byte[]; await context.Response.Body.WriteAsync(byteResponse, 0, byteResponse.Length); } else { await context.Response.WriteAsync(JsonConvert.SerializeObject(response)); } } else { await context.Response.Body.WriteAsync(new byte[] { }); } } else { RemoveHeaders(context); _logger.Error($"Method not found for route : {route.ToString()}"); context.Response.StatusCode = (int)HttpStatusCode.NotFound; await context.Response.WriteAsync(_responseBuilder.NotFound().ToString()); } } else { _logger.Error($"Type not found for route : {route.ToString()}"); context.Response.StatusCode = (int)HttpStatusCode.NotFound; await context.Response.WriteAsync(_responseBuilder.NotFound().ToString()); } } else { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; await context.Response.WriteAsync(_responseBuilder.Unauthorized().ToString()); } } else { try { route = await _apiGatewayService.GetRouteAsync(_httpContextProxy.GetHttpMethod(), _httpContextProxy.GetURIAbsolutePath()); if (route != null) { try { context.Response.ContentType = route.ContentType; JObject response = await CallRemoteRouteCalls(context, route); if (response != null) { RemoveHeaders(context); if (response["content_type"] != null && response["data"] != null) { await context.Response.WriteAsync(response["data"].ToString()); } else { await context.Response.WriteAsync(response.ToString()); } } else { await _next(context); } } catch (UnauthorizedAccessException ex) { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; _logger.Error($"UnauthorizedAccessException remote route. {route.ToString() } . {ex.Message}", ex); } catch (Exception ex) { _logger.Error($"Error Executing remote route. {route.ToString() } . {ex.Message}", ex); context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; } } else { await _next(context); } } catch (Exception ex) { _logger.Error($"Error Executing remote route. {route.ToString() } . {ex.Message}", ex); await _next(context); } } }
/// <summary> /// Initializes this DualityApp. Should be called before performing any operations within Duality. /// </summary> /// <param name="context">The <see cref="ExecutionContext"/> in which Duality runs.</param> /// <param name="commandLineArgs"> /// Command line arguments to run this DualityApp with. /// Usually these are just the ones from the host application, passed on. /// </param> public static void Init(ExecutionContext context, IAssemblyLoader plugins, string[] commandLineArgs) { if (initialized) { return; } // Process command line options if (commandLineArgs != null) { // Enter debug mode if (commandLineArgs.Contains("/debug")) { System.Diagnostics.Debugger.Launch(); } } execContext = context; // Initialize the plugin manager { assemblyLoader = plugins ?? new Duality.Backend.Dummy.DummyAssemblyLoader(); Log.Write(LogType.Verbose, "Using '" + assemblyLoader.GetType().Name + "' to load plugins."); assemblyLoader.Init(); // Log assembly loading data for diagnostic purposes { Log.Write(LogType.Verbose, "Currently Loaded Assemblies:" + Environment.NewLine + "{0}", assemblyLoader.LoadedAssemblies.ToString( assembly => " " + assembly, Environment.NewLine)); Log.Write(LogType.Verbose, "Plugin Base Directories:" + Environment.NewLine + assemblyLoader.BaseDirectories.ToString( path => " " + path, Environment.NewLine)); Log.Write(LogType.Verbose, "Available Assembly Paths:" + Environment.NewLine + assemblyLoader.AvailableAssemblyPaths.ToString( path => " " + path, Environment.NewLine)); } pluginManager.Init(assemblyLoader); pluginManager.PluginsRemoving += pluginManager_PluginsRemoving; pluginManager.PluginsRemoved += pluginManager_PluginsRemoved; } // Load all plugins. This needs to be done first, so backends and Types can be located. pluginManager.LoadPlugins(); // Initialize the system backend for system info and file system access InitBackend(out systemBack); // Initialize the graphics backend InitBackend(out graphicsBack); // Initialize the audio backend InitBackend(out audioBack); sound = new SoundDevice(); // Initialize all core plugins, this may allocate Resources or establish references between plugins pluginManager.InitPlugins(); initialized = true; // Write environment specs as a debug log Log.Write(LogType.Info, "DualityApp initialized" + Environment.NewLine + "Debug Mode: {0}" + Environment.NewLine + "Command line arguments: {1}", System.Diagnostics.Debugger.IsAttached, commandLineArgs != null ? commandLineArgs.ToString(", ") : "null"); }