Esempio n. 1
0
        public static async Task <JobRuntime> CreateJobRuntime(IServiceRoute serviceRoute, JobEntity jobView)
        {
            var assemblyPath = await GetJobAssemblyPath(serviceRoute, jobView);

            if (string.IsNullOrWhiteSpace(assemblyPath))
            {
                throw new FileNotFoundException($"{jobView.FilePath}不存在");
            }

            var domain   = DomainManager.Create(jobView.Id);
            var assembly = domain.LoadFile(assemblyPath);

            Type type     = assembly.GetType(jobView.ClassName, true, true);
            var  instance = Activator.CreateInstance(type) as JobBase;

            if (instance == null)
            {
                throw new InvalidCastException($"程序集: {jobView.AssemblyName} 创建Job实例失败,请确认 {jobView.ClassName} 是否派生自JobBase");
            }

            return(new JobRuntime()
            {
                JobView = jobView,
                Domain = domain,
                Instance = instance
            });
        }