示例#1
0
        public SpiderDomain GetLoadSpiderDomain()
        {
            if (this._spiderDomain != null)
            {
                return(this._spiderDomain);
            }

            // 创建新的 AppDomain
            AppDomainSetup domaininfo = new AppDomainSetup
            {
                ApplicationBase = Environment.CurrentDirectory
            };
            Evidence  adevidence = AppDomain.CurrentDomain.Evidence;
            AppDomain rawDomain  = AppDomain.CreateDomain("NacollectorSpiders", adevidence, domaininfo);

            // 动态加载 dll
            Type type         = typeof(SpiderDomain);
            var  spiderDomain = (SpiderDomain)rawDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);

            spiderDomain.LoadAssembly(Path.Combine(Application.StartupPath, "NacollectorSpiders.dll"));

            this._spiderRawDomain = rawDomain;
            this._spiderDomain    = spiderDomain;
            return(spiderDomain);
        }
示例#2
0
        public void UnloadSpiderDomain()
        {
            if (_spiderRawDomain == null)
            {
                return;
            }

            // 卸载 dll
            AppDomain.Unload(_spiderRawDomain);
            _spiderRawDomain = null;
            _spiderDomain    = null;
        }
示例#3
0
        /// <summary>
        /// 开始执行任务
        /// </summary>
        /// <param name="obj"></param>
        public void StartTask(object obj)
        {
            var settings = (SpiderSettings)obj;

            SpiderDomain spiderDomain = null;

            try
            {
                spiderDomain = GetLoadSpiderDomain();
            }
            catch (Exception ex)
            {
                ShowCreateTaskError((string)settings.TaskId, "NacollectorSpiders.dll 调用失败", ex);
#if DEBUG
                Debugger.Break();
#endif
                return;
            }

#if !DEBUG
            // 调用目标函数
            try
            {
#endif

            string typeName = $"NacollectorSpiders.{settings.ClassName}";
            spiderDomain.NewTask(typeName, settings, _spiderCallback);
#if !DEBUG
        }

        catch (ThreadAbortException)
        {
            // 进程正在被中止
            // 不进行操作
        }
        catch (Exception ex)
        {
            ShowCreateTaskError((string)settings.TaskId, "无法调用 NacollectorSpiders.PokerDealer.NewTask", ex);
#if DEBUG
            Debugger.Break();
#endif
            return;
        }
#endif

            AbortTask((string)settings.TaskId);
        }