/// <summary> /// This will generate an anonymous appDomain proxy (good for tests but _note that the AppDomain will not be terminated /// </summary> /// <param name="assemblyName"></param> /// <param name="typeToCreateSimpleName"></param> /// <returns></returns> public static object getProxy(string assemblyName, string typeToCreateSimpleName) { string appDomainName = Path.GetFileNameWithoutExtension(DI.config.TempFileNameInTempDirectory) + "_" + typeToCreateSimpleName; // make sure each appDomainName is unique var tempAppDomainFactory = new O2AppDomainFactory(appDomainName); return(tempAppDomainFactory.getProxyObject(typeToCreateSimpleName)); }
public static void executeInAppDomain(string appDomainName, string scriptToExecute) { O2Thread.mtaThread( () => { var o2AppDomain = new O2.Kernel.Objects.O2AppDomainFactory(appDomainName); try { o2AppDomain.load("O2_XRules_Database"); o2AppDomain.load("O2_Kernel"); o2AppDomain.load("O2_DotNetWrappers"); var o2Proxy = (O2Proxy)o2AppDomain.getProxyObject("O2Proxy"); o2Proxy.InvokeInStaThread = true; o2Proxy.staticInvocation("O2_External_SharpDevelop", "FastCompiler_ExtensionMethods", "executeSourceCode", new object[] { scriptToExecute }); } catch (Exception ex) { ex.log("inside new AppDomain"); } PublicDI.log.showMessageBox("Click OK to close the '{0}' AppDomain (and close all open windows)".format(appDomainName)); o2AppDomain.unLoadAppDomain(); }); }
public static string executeEngineOnSeparateAppDomain(string sourceFolder, string resultsFolder, string methodFilter, bool useCachedData, List <string> references, int numberOfItemsToProcess) { //var script = @"C:\O2\_XRules_Local\Ast_Test.cs"; var script = "MethodMappings_Engine.cs".local(); "Creating new AppDomain".info(); var appDomainName = 4.randomString(); var o2AppDomain = new O2.Kernel.Objects.O2AppDomainFactory(appDomainName); o2AppDomain.load("O2_XRules_Database"); o2AppDomain.load("O2_Kernel"); o2AppDomain.load("O2_DotNetWrappers"); var o2Proxy = (O2Proxy)o2AppDomain.getProxyObject("O2Proxy"); var parameters = new object[] { sourceFolder, resultsFolder, methodFilter, useCachedData, references, numberOfItemsToProcess }; var result = (string)o2Proxy.staticInvocation("O2_External_SharpDevelop", "FastCompiler_ExtensionMethods", "executeFirstMethod", new object[] { script, parameters }); o2AppDomain.unLoadAppDomain(); "AppDomain execution completed, Runing GCCollect".info(); PublicDI.config.gcCollect(); "GCCollect completed, returning result data: {0}".info(result); return(result); }
public static string codeStreams_CreateInAppDomain(this Saved_MethodStream savedMethodStream) { //var script = @"C:\O2\_XRules_Local\Ast_Test.cs"; var script = "Saved_MethodStream.cs".local(); "Creating new AppDomain".info(); var appDomainName = 4.randomString(); var o2AppDomain = new O2.Kernel.Objects.O2AppDomainFactory(appDomainName); o2AppDomain.load("O2_XRules_Database"); o2AppDomain.load("O2_Kernel"); o2AppDomain.load("O2_DotNetWrappers"); var o2Proxy = (O2Proxy)o2AppDomain.getProxyObject("O2Proxy"); var parameters = new object[] { savedMethodStream.Serialized_Saved_MethodStream_FileCache // sourceFolder, // resultsFolder, // methodFilter, // useCachedData, // references, // numberOfItemsToProcess }; var result = (string)o2Proxy.staticInvocation("O2_External_SharpDevelop", "FastCompiler_ExtensionMethods", "executeFirstMethod", new object[] { script, parameters }); "Result: {0}".info(result); o2AppDomain.sleep(2000); o2AppDomain.unLoadAppDomain(); "AppDomain execution completed, Runing GCCollect".info(); PublicDI.config.gcCollect(); "GCCollect completed, returning result data: {0}".info(result); return(result); }
public static string executeScriptInSeparateAppDomain(this string scriptToExecute, bool showLogViewer, bool openScriptGui) { var appDomainName = 12.randomLetters(); var o2AppDomain = new O2AppDomainFactory(appDomainName); o2AppDomain.load("O2_XRules_Database"); o2AppDomain.load("O2_Kernel"); o2AppDomain.load("O2_DotNetWrappers"); var o2Proxy = (O2Proxy)o2AppDomain.getProxyObject("O2Proxy"); if (o2Proxy.isNull()) { "in executeScriptInSeparateAppDomain, could not create O2Proxy object".error(); return null; } o2Proxy.InvokeInStaThread = true; if (showLogViewer) o2Proxy.executeScript( "O2Gui.open<Panel>(\"Util - LogViewer\", 400,140).add_LogViewer();"); if (openScriptGui) o2Proxy.executeScript("O2Gui.open<Panel>(\"Script editor\", 700, 300)".line() + " .add_Script(false)".line() + " .onCompileExecuteOnce()".line() + " .Code = \"hello\";".line() + "//O2File:Scripts_ExtensionMethods.cs"); o2Proxy.executeScript(scriptToExecute); //PublicDI.log.showMessageBox MessageBox.Show("Click OK to close the '{0}' AppDomain (and close all open windows)".format(appDomainName)); o2AppDomain.unLoadAppDomain(); return scriptToExecute; }
public static string executeEngineOnSeparateAppDomain(string sourceFolder, string resultsFolder, string methodFilter, bool useCachedData, List<string> references, int numberOfItemsToProcess) { //var script = @"C:\O2\_XRules_Local\Ast_Test.cs"; var script = "MethodMappings_Engine.cs".local(); "Creating new AppDomain".info(); var appDomainName = 4.randomString(); var o2AppDomain = new O2.Kernel.Objects.O2AppDomainFactory(appDomainName); o2AppDomain.load("O2_XRules_Database"); o2AppDomain.load("O2_Kernel"); o2AppDomain.load("O2_DotNetWrappers"); var o2Proxy = (O2Proxy)o2AppDomain.getProxyObject("O2Proxy"); var parameters = new object[] { sourceFolder, resultsFolder, methodFilter, useCachedData, references, numberOfItemsToProcess }; var result =(string)o2Proxy.staticInvocation("O2_External_SharpDevelop","FastCompiler_ExtensionMethods","executeFirstMethod",new object[]{script, parameters}); o2AppDomain.unLoadAppDomain(); "AppDomain execution completed, Runing GCCollect".info(); PublicDI.config.gcCollect(); "GCCollect completed, returning result data: {0}".info(result); return result; }
public static string codeStreams_CreateInAppDomain(this Saved_MethodStream savedMethodStream) { //var script = @"C:\O2\_XRules_Local\Ast_Test.cs"; var script = "Saved_MethodStream.cs".local(); "Creating new AppDomain".info(); var appDomainName = 4.randomString(); var o2AppDomain = new O2.Kernel.Objects.O2AppDomainFactory(appDomainName); o2AppDomain.load("FluentSharp.CoreLib.dll"); var o2Proxy = (O2Proxy)o2AppDomain.getProxyObject("O2Proxy"); var parameters = new object[] { savedMethodStream.Serialized_Saved_MethodStream_FileCache // sourceFolder, // resultsFolder, // methodFilter, // useCachedData, // references, // numberOfItemsToProcess }; var result =(string)o2Proxy.staticInvocation("O2_External_SharpDevelop","FastCompiler_ExtensionMethods","executeFirstMethod",new object[]{script, parameters}); "Result: {0}".info(result); o2AppDomain.sleep(2000); o2AppDomain.unLoadAppDomain(); "AppDomain execution completed, Runing GCCollect".info(); PublicDI.config.gcCollect(); "GCCollect completed, returning result data: {0}".info(result); return result; }
/// <summary> /// This will generate an anonymous appDomain proxy (good for tests but _note that the AppDomain will not be terminated /// </summary> /// <param name="assemblyName"></param> /// <param name="typeToCreateSimpleName"></param> /// <returns></returns> public static object getProxy(string assemblyName, string typeToCreateSimpleName) { string appDomainName = Path.GetFileNameWithoutExtension(DI.config.TempFileNameInTempDirectory) + "_" + typeToCreateSimpleName; // make sure each appDomainName is unique var tempAppDomainFactory = new O2AppDomainFactory(appDomainName); return tempAppDomainFactory.getProxyObject(typeToCreateSimpleName); }
public static void executeInAppDomain(string appDomainName,string scriptToExecute) { O2Thread.mtaThread( ()=>{ var o2AppDomain = new O2.Kernel.Objects.O2AppDomainFactory(appDomainName); try { o2AppDomain.load("O2_XRules_Database"); o2AppDomain.load("O2_Kernel"); o2AppDomain.load("O2_DotNetWrappers"); var o2Proxy = (O2Proxy)o2AppDomain.getProxyObject("O2Proxy"); o2Proxy.InvokeInStaThread = true; o2Proxy.staticInvocation("O2_External_SharpDevelop","FastCompiler_ExtensionMethods","executeSourceCode",new object[]{ scriptToExecute }); } catch(Exception ex) { ex.log("inside new AppDomain"); } DebugMsg.showMessageBox("Click OK to close the '{0}' AppDomain (and close all open windows)".format(appDomainName)); o2AppDomain.unLoadAppDomain(); }); }
public void Test_LoadingCodeIntoMultipleAppDomains() { // test current AppDomain string pathToproxyDll = DI.config.ExecutingAssembly; // Path.Combine(AppDomain.CurrentDomain.BaseDirectory, assemblyName + ".exe"); string testAppDomainDirectory = copyO2ProxyToTempFolder(pathToproxyDll, assemblyName); object resultInCurrentAppDomain = LoadTypes.loadTypeAndExecuteMethodInAppDomain(AppDomain.CurrentDomain, assemblyName, typeToCreate, methodToInvoke, methodParams); Assert.IsNotNull(resultInCurrentAppDomain, "result"); DI.log.debug("{0} = {1}", "resultIncurrentAppDomain", resultInCurrentAppDomain); // test creating new AppDomain var newAppDomain = new O2AppDomainFactory("newAppDomain", testAppDomainDirectory); Assert.IsNotNull(newAppDomain, "newAppDomain"); newAppDomain.load(assemblyName); object resultInNewAppDomain = LoadTypes.loadTypeAndExecuteMethodInAppDomain(newAppDomain.appDomain, assemblyName, typeToCreate, methodToInvoke, methodParams); Assert.IsNotNull(resultInNewAppDomain, "result"); DI.log.debug("{0} = {1}", "resultInNewAppDomain", resultInNewAppDomain); Assert.That(resultInCurrentAppDomain != resultInNewAppDomain, "resultIncurrentAppDomain == resultInNewAppDomain"); // test creating new AppDomain just using the O2AppDomainFactory var appDomainFactory = new O2AppDomainFactory("appDomainFactory", testAppDomainDirectory); appDomainFactory.load(assemblyName); List<string> loadedAssemblies = appDomainFactory.getAssemblies(false); Assert.That(loadedAssemblies.Count > 0 && loadedAssemblies.Contains(assemblyName), "Loaded assembly was not there"); object proxyObject = appDomainFactory.getProxyObject((typeToCreate)); Assert.IsNotNull(proxyObject, "proxyObject was null"); object resultInAppDomainFactory = appDomainFactory.invoke(proxyObject, methodToInvoke, methodParams); DI.log.debug("{0} = {1}", "resultInAppDomainFactory", resultInAppDomainFactory); Assert.That( (resultInCurrentAppDomain != resultInAppDomainFactory) && (resultInNewAppDomain != resultInAppDomainFactory), "All results should be different"); // test creating objects using the format {type} {assembly} var appDomainFactory2 = new O2AppDomainFactory("appDomainFactory2", testAppDomainDirectory); appDomainFactory.load(assemblyName); object proxyObject2 = appDomainFactory2.getProxyObject(typeToCreate + " " + assemblyName); Assert.IsNotNull(proxyObject2, "proxyObject2 was null"); DI.log.debug("{0} = {1}", "appDomainFactory2", appDomainFactory.invoke(proxyObject2, methodToInvoke)); // test if we can get a MethodInfo using using the format {method} {type} {assembly} var appDomainFactory3 = new O2AppDomainFactory("appDomainFactory3", testAppDomainDirectory); appDomainFactory.load(assemblyName); var methodInfoFromappDomainFactory3 = (MethodInfo) appDomainFactory3.getProxyMethod(methodToInvoke + " " + typeToCreate + " " + assemblyName); Assert.That(methodInfoFromappDomainFactory3 != null, "methodInfoFromappDomainFactory3 was null"); DI.log.debug("{0} MethodInfo = {1}", "from appDomainFactory3", methodInfoFromappDomainFactory3.ToString()); // we can't invoke this guy because we don't have the proxy // test if we can invoke a MethodInfo using using the format {method} {type} {assembly} var appDomainFactory4 = new O2AppDomainFactory("appDomainFactory4", testAppDomainDirectory); appDomainFactory.load(assemblyName); object invocationResult4 = appDomainFactory4.invokeMethod(methodToInvoke + " " + typeToCreate + " " + assemblyName); Assert.That(invocationResult4 != null, "invocationResult4 was null"); DI.log.debug("[{0}] {1} {2} {3} = {4}", "from appDomainFactory4", assemblyName, typeToCreate, methodToInvoke, invocationResult4); // we can't invoke this guy because we don't have the proxy }
public void Test_PropertiesAndMethodsWithParameters() { var propertyValue = "value"; var intvalue = 2009; var appDomain = new O2AppDomainFactory("testAppDomain", AppDomain.CurrentDomain.BaseDirectory); appDomain.load(assemblyName); // invoke using direct cast var proxy = (O2Proxy) appDomain.getProxyObject(typeToCreateSimpleName); Assert.That(proxy != null, "proxy was null"); proxy.Property = propertyValue; Assert.That(proxy.Property == propertyValue, "proxy.Property != propertyValue"); Assert.That(proxy.returnConcatParamData(proxy.Property, intvalue) == proxy.Property + intvalue, "error in returnConcatParamData"); // invoke using O2AppDomainFactory methods Assert.Ignore( "todo: invoke using O2AppDomainFactory methods (need to implement Set and Get wrapper methods)"); }