Exemplo n.º 1
0
        private static void ChangeWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            var watcher = sender as FileSystemWatcher;

            if (watcher == null)
            {
                return;
            }

            var path = watcher.Path;
            DynamicScriptConfig dc = null;

            try
            {
                watcher.EnableRaisingEvents = false;
                lock (Lock)
                {
                    if (DynamicScriptConfigs.ContainsKey(path))
                    {
                        dc = DynamicScriptConfigs[path];
                        LoadScript(dc.DepConfigFile, dc.Directory);
                        Logger.InfoFormat("成功更新传感器适配器");
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                //reset dc
                dc      = DynamicScriptConfigs[path];
                watcher = dc != null ? dc.ChangeWatcher : null;
                if (watcher != null)
                {
                    watcher.EnableRaisingEvents = true;
                }
            }
        }
Exemplo n.º 2
0
        public static void LoadScript(string depDescFile, string directory)
        {
            if (!Directory.Exists(directory))
            {
                throw new FileNotFoundException(string.Format("Directory {0} not exist.", directory));
            }
            directory = Path.GetFullPath(directory);
            lock (Lock)
            {
                if (DynamicScriptConfigs.ContainsKey(directory))
                {
                    Cleanup(directory);
                }
                var dc = new DynamicScriptConfig
                {
                    ChangeWatcher = new FileSystemWatcher
                    {
                        Path = directory,
                        IncludeSubdirectories = false,
                        Filter = "*.cs"
                    },
                    Directory     = directory,
                    DepConfigFile = depDescFile
                };

                var domain    = "CrossDomainCompilerDomain-" + Guid.NewGuid();
                var appDomain = AppDomain.CreateDomain(domain, new Evidence(), new AppDomainSetup
                {
                    // !! 作为动态库使用时,需要指定自身所在目录
                    ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
                });
                appDomain.SetData("APP_CONFIG_FILE", AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE"));
                try
                {
                    Logger.DebugFormat("Ceating AppDomain: {0}", domain);
                    var compiler =
                        (CrossDomainCompiler)
                        appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName,
                                                          typeof(CrossDomainCompiler).FullName);
                    var scriptFiles = Directory.GetFiles(directory, "*.cs");
                    DynamicScriptConfigs.Add(directory, dc);
                    dc.ChangeWatcher.Changed += ChangeWatcher_Changed;
                    dc.ChangeWatcher.Created += ChangeWatcher_Changed;
                    dc.ChangeWatcher.Deleted += ChangeWatcher_Changed;

                    dc.ChangeWatcher.EnableRaisingEvents = true;
                    if (!compiler.CompileAssembly(depDescFile, scriptFiles))
                    {
                        var error = string.Format("Compiler {0} with {1} failed. See the detail above", directory,
                                                  depDescFile);
                        Logger.Error(error);
                        throw new CompileErrorException(error);
                    }
                    dc.Domain   = appDomain;
                    dc.Compiler = compiler;
                }
                catch (Exception ex)
                {
                    Logger.DebugFormat("Unloading AppDomain: {0}", domain);
                    AppDomain.Unload(appDomain);
                    throw ex;
                }
            }
        }