/// <summary> /// /// This code loads the DLL specified, finds the first type in the Assembly with the IRunnable type, /// and then instantiates and executes that class. /// /// </summary> /// <param name="RootPath">Directory where the desired DLL is to be found</param> /// <param name="Parameters">Parameters that might be needed in the future</param> /// <param name="ExecDLL">The filename of the desired DLL</param> /// <param name="Body">The values needed by the code in order to successfully execute the request</param> /// <returns>The value(s) sought by the caller and returned by the loaded DLL</returns> public static List <Dictionary <string, string> > LoadAndExecuteDLL(string RootPath, string Parameters, string ExecDLL, List <Dictionary <string, string> > Body) { var ExecDllFilepath = RootPath + "\\" + ExecDLL; var oResultBody = new List <Dictionary <string, string> >(); AppDomain domain = null; try { var asm = Assembly.LoadFile(ExecDllFilepath); //get types from assembly var typesInAssembly = asm.GetTypes(); var type = typesInAssembly.First(); return(RunnableExecutor.InvokeRunnable(type, Body)); } finally { if (domain != null) { AppDomain.Unload(domain); } } }
/// <summary> /// /// This code compiles a block of code into an Assembly, embeds it within a safe Application Domain, /// finds the first type in the Assembly with the IRunnable type, and then instantiates and executes that class. /// /// UNDER CONSTRUCTION - DOES NOT CURRENTLY WORK /// /// </summary> /// <param name="UrlParameters">Parameters that might be needed in the future</param> /// <param name="ExecCode">The code to be compiled and assembled into an assembly</param> /// <param name="Body">The values needed by the code in order to successfully execute the code</param> /// <returns>The value(s) sought by the caller and returned by the compiled code block</returns> public static List <Dictionary <string, string> > CompileAndExecuteCodeSafe(string UrlParameters, string ExecCode, List <Dictionary <string, string> > Body) { AppDomain sandbox = null; HashSet <string> additionalAssemblyDirs = new HashSet <string>(); try { string targetAssemblyPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var assemblyNames = (from a in AppDomain.CurrentDomain.GetAssemblies() where !a.IsDynamic select a.Location).ToArray(); foreach (string tmpAssembly in assemblyNames) { // additionalAssemblyDirs.Add(Path.GetDirectoryName(tmpAssembly)); if (tmpAssembly.Contains("MetadataApiCommon")) { targetAssemblyPath = tmpAssembly; additionalAssemblyDirs.Add(Path.GetDirectoryName(targetAssemblyPath)); } } Assembly dynamicAssembly = RunnableExecutor.CompileCode(ExecCode, assemblyNames, false); additionalAssemblyDirs.Add(RunnableExecutor.GetAssemblyDirectory(dynamicAssembly)); sandbox = ProduceSecureDomain(additionalAssemblyDirs.ToArray()); var typesInAssembly = dynamicAssembly.GetTypes(); var type = typesInAssembly.First(); RunnableExecutor runnableExecutor = (RunnableExecutor)sandbox.CreateInstanceFromAndUnwrap(targetAssemblyPath, "MetadataApiCommon.RunnableExecutor") as RunnableExecutor; IRunnable runnable = runnableExecutor.Create(GetAssemblyPath(dynamicAssembly), type.FullName, null); return(runnable.Run(Body)); } finally { if (sandbox != null) { AppDomain.Unload(sandbox); } } }
/// <summary> /// /// This code compiles a block of code into an Assembly, finds the first type in the Assembly with the IRunnable type, /// and then instantiates and executes that class. /// /// </summary> /// <param name="UrlParameters">Parameters that might be needed in the future</param> /// <param name="ExecCode">The code to be compiled and assembled into an assembly</param> /// <param name="Body">The values needed by the code in order to successfully execute the code</param> /// <param name="Assemblies">Any other assemblies needed for successful compilation</param> /// <returns>The value(s) sought by the caller and returned by the compiled code block</returns> public List <Dictionary <string, string> > CompileAndExecuteCode(string UrlParameters, string ExecCode, List <Dictionary <string, string> > Body, string[] Assemblies) { Assembly dynamicAssembly = RunnableExecutor.CompileCode(ExecCode, Assemblies); var typesInAssembly = dynamicAssembly.GetTypes(); var type = typesInAssembly.First(); return(RunnableExecutor.InvokeRunnable(type, Body)); }
/// <summary> /// /// This code makes the call to compile and execute a block of code /// /// </summary> /// <param name="UrlParameters">The parameters used for the code compilation/execution</param> /// <param name="ExecCode">The block of code that will be compiled and then executed</param> /// <param name="Body">The values that will be used by the block of code</param> /// <returns>The return value(s) desired by the code execution</returns> private List<Dictionary<string, string>> CompileAndExecuteCode(string UrlParameters, string ExecCode, List<Dictionary<string, string>> Body) { bool bSafeMode = (GetMode(Body) == "safe"); if (bSafeMode) return RunnableExecutor.CompileAndExecuteCodeSafe(UrlParameters, ExecCode, Body); else { RunnableExecutor executor = new RunnableExecutor(); return executor.CompileAndExecuteCode(UrlParameters, ExecCode, Body); } }