Ejemplo n.º 1
0
        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
        }