简体   繁体   中英

Quartz.Net Create Job Plugin Architecture

I am working on a Quartz.Net web API. I am currently trying to crate a plugin architecture for loading IJob as plugins.

Currently, I can load the assemblies from a plugins directory.

        private void LoadPlugins(ISchedulerFactory factory)
        {
            var scheduler = factory.GetScheduler().Result;
            string folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Plugins";
            if (Directory.Exists(folder))
            {
                string[] files = Directory.GetFiles(folder, "*.dll");

                foreach (string file in files)
                {
                    Assembly plugin = AppDomain.CurrentDomain.Load(File.ReadAllBytes(file));

                    foreach (var t in plugin.GetTypes())
                    {
                        if (t.GetInterfaces().Any(i => i == typeof(IJob)))
                        {
                            scheduler.AddJob(JobBuilder.Create(t).WithIdentity(t.Name + " Job").StoreDurably().Build(), true).Wait();
                        }
                    }

                }
            }
        }

The Quartz.Net scheduler does add the job.

If I call the Quartz.Net scheduler.CheckExists(AJobKeyFromThePlugin) Then it does return true.

However, at a latter point in the application If I try to get the ```JobDetail`` from the scheduler I get the following exception.

Couldn't retrieve job because a required type was not found: Could not load type...

I have tried to keep a list of the loaded plugin assemblies in a statically loaded list, but that doesn't seem to help.

How can I better load the assemblies so that Quartz.Net can see them?

You need to implement the ITypeLoadHelper and configure Quartz.net to use your TypeLoadHelper.

The Quartz.Net property

"quartz.scheduler.typeLoadHelper.type"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM