Пример #1
0
        public AppDomainMessageSink(AppDomainAttribute appDomainAttribute, MarshalByRefObject obj, IMessageSink nextSink)
        {
            _appDomainAttribute = appDomainAttribute;
            _obj      = obj;
            _nextSink = nextSink;

            string baseDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));

            Directory.CreateDirectory(baseDir);
            string         friendlyName = appDomainAttribute.FriendlyName;
            AppDomainSetup setup        = new AppDomainSetup();

            setup.ApplicationBase = baseDir;
            string configFile = appDomainAttribute.ConfigFile;
            string config     = appDomainAttribute.Config;

            if (config != null)
            {
                if (configFile == null)
                {
                    configFile = friendlyName + ".config";
                }
                configFile = Path.Combine(baseDir, configFile);
                setup.ConfigurationFile = configFile;
                using (StreamWriter writer = new StreamWriter(configFile))
                {
                    writer.Write(appDomainAttribute.Config);
                }
            }

            Type activationType = appDomainAttribute.ActivationType;

            XmlDocument doc = new XmlDocument();

            if (configFile != null)
            {
                doc.Load(configFile);
            }
            mergeDependentAssemblies(doc, activationType);
            if (doc.DocumentElement != null)
            {
                if (configFile == null)
                {
                    configFile = Path.GetTempFileName();
                }
                doc.Save(configFile);
            }

            string binDir = baseDir;

            if (appDomainAttribute.PrivateBinPath != null)
            {
                string privateBinPath = appDomainAttribute.PrivateBinPath;
                setup.PrivateBinPath = privateBinPath;
                privateBinPath       = privateBinPath.Split(new char[] { ';' })[0];
                binDir = Path.Combine(binDir, privateBinPath);
                Directory.CreateDirectory(binDir);
            }

            // Copy activated type assembly.
            string activationFile = new Uri(activationType.Assembly.CodeBase).LocalPath;
            string destFile       = Path.Combine(binDir, Path.GetFileName(activationFile));

            if (activationFile != destFile)
            {
                File.Copy(activationFile, destFile, true);
            }

            // Copy context attribute assembly.
            string contextsFile     = new Uri(GetType().Assembly.CodeBase).LocalPath;
            string destContextsFile = Path.Combine(binDir, Path.GetFileName(contextsFile));

            if (destContextsFile != destFile)
            {
                File.Copy(contextsFile, destContextsFile, true);
            }

            copyFiles(activationType, baseDir);

            if (appDomainAttribute.ShadowCopyFiles)
            {
                setup.ShadowCopyFiles = "true";
            }

            _domain       = AppDomain.CreateDomain(friendlyName, null, setup);
            _remoteObject = (MarshalByRefObject)_domain.CreateInstanceAndUnwrap(
                activationType.Assembly.FullName, activationType.FullName);
        }
        public AppDomainMessageSink(AppDomainAttribute appDomainAttribute, MarshalByRefObject obj, IMessageSink nextSink)
        {
            _appDomainAttribute = appDomainAttribute;
            _obj = obj;
            _nextSink = nextSink;

            string baseDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
            Directory.CreateDirectory(baseDir);
            string friendlyName = appDomainAttribute.FriendlyName;
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationBase = baseDir;
            string configFile = appDomainAttribute.ConfigFile;
            string config = appDomainAttribute.Config;
            if(config != null)
            {
                if(configFile == null) { configFile = friendlyName + ".config"; }
                configFile = Path.Combine(baseDir, configFile);
                setup.ConfigurationFile = configFile;
                using(StreamWriter writer = new StreamWriter(configFile))
                {
                    writer.Write(appDomainAttribute.Config);
                }
            }

            Type activationType = appDomainAttribute.ActivationType;

            XmlDocument doc = new XmlDocument();
            if(configFile != null) { doc.Load(configFile); }
            mergeDependentAssemblies(doc, activationType);
            if(doc.DocumentElement != null)
            {
                if(configFile == null) { configFile = Path.GetTempFileName(); }
                doc.Save(configFile);
            }

            string binDir = baseDir;
            if(appDomainAttribute.PrivateBinPath != null)
            {
                string privateBinPath = appDomainAttribute.PrivateBinPath;
                setup.PrivateBinPath = privateBinPath;
                privateBinPath = privateBinPath.Split(new char[] {';'})[0];
                binDir = Path.Combine(binDir, privateBinPath);
                Directory.CreateDirectory(binDir);
            }

            // Copy activated type assembly.
            string activationFile = new Uri(activationType.Assembly.CodeBase).LocalPath;
            string destFile = Path.Combine(binDir, Path.GetFileName(activationFile));
            if(activationFile != destFile) { File.Copy(activationFile, destFile, true); }

            // Copy context attribute assembly.
            string contextsFile = new Uri(GetType().Assembly.CodeBase).LocalPath;
            string destContextsFile = Path.Combine(binDir, Path.GetFileName(contextsFile));
            if(destContextsFile != destFile) { File.Copy(contextsFile, destContextsFile, true); }

            copyFiles(activationType, baseDir);

            if(appDomainAttribute.ShadowCopyFiles) { setup.ShadowCopyFiles = "true"; }

            _domain = AppDomain.CreateDomain(friendlyName, null, setup);
            _remoteObject = (MarshalByRefObject)_domain.CreateInstanceAndUnwrap(
                activationType.Assembly.FullName, activationType.FullName);
        }