public IJobAssembly Load(string jobsPath) { var dllFiles = Directory.GetFiles(jobsPath, "*.dll"); var jobFilePath = ""; foreach (var dllFile in dllFiles) { var tempAlc = new HostAssemblyLoadContext(dllFile); Assembly tempAssembly; using (var stream = File.OpenRead(dllFile)) { tempAssembly = tempAlc.LoadFromStream((Stream)stream); } var types = tempAssembly.GetTypes(); var jobClass = types.SingleOrDefault(x => x.GetInterfaces().Contains(typeof(IJob))); tempAlc.Unload(); if (jobClass != null) { jobFilePath = dllFile; break; } } if (string.IsNullOrEmpty(jobFilePath)) { return(null); } var alc = new HostAssemblyLoadContext(jobFilePath); var alcWeakRef = new WeakReference(alc); Assembly assembly; using (var stream = File.OpenRead(jobFilePath)) { assembly = alc.LoadFromStream((Stream)stream); } var jobType = assembly.GetTypes().SingleOrDefault(x => x.GetInterfaces().Contains(typeof(IJob))); if (jobType == null) { return(null); } var containerBuilder = new ContainerBuilder(); var configurationBuilder = new ConfigurationBuilder() .SetBasePath(jobsPath) .AddJsonFile($"appsettings.json", true, true) .AddEnvironmentVariables(); foreach (var appSettingFile in Directory.GetFiles(jobsPath, "appsettings.*.json")) { configurationBuilder.AddJsonFile(Path.GetFileName(appSettingFile)); } var config = configurationBuilder.Build(); containerBuilder.RegisterInstance((ConfigurationRoot)config); MethodInfo methodInfo = jobType.GetMethod("Register"); if (methodInfo == null) { return(null); } methodInfo.Invoke(null, new object[] { containerBuilder }); var container = containerBuilder.Build(); var jobInstance = (IJob)Instance(jobType, container); var jobAssembly = new JobAssembly() { Instance = jobInstance, UniqueId = Guid.NewGuid(), Assembly = assembly, HostAssemblyLoadContext = alc, WeakReference = alcWeakRef, JobFullPath = jobFilePath, FileMD5Hash = _fileHelper.CalculateMD5(jobFilePath) }; return(jobAssembly); }