0

I am a novice with the use of Quartz.net, and my question is the following: I have created a WCF server that includes an interface with the operations that the scheduler can do, and a class that implements the interface in which is allocated the the constructor that instantiates the scheduler and the methods.

In other place, inside of the same project, I can create a library with the definition of a simple job:

public class MyJob : IJob
{
    public virtual void Execute(IJobExecutionContext context)
    {
       // Body of the job
    }

All works fine when I connect with a client, but I need to include jobs in a different way, including the .dll dynamically in the quartz service folder. But I don't know how.

I have been searching for a solution, and I have found something in relation with System.Reflection; but I don't know where I have to situate a possible code

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    _"I need to include jobs in a different way, including the .dll dynamically in the quartz service folder"_ - can you elaborate on this? What exactly are you trying to do? – CodeCaster Apr 17 '13 at 11:03
  • Thanks for your attention. Instead of include "manually" the Job.dll to the quartz service folder, i want to include them remotely. Then, i need to schedule the job with it trigger, and i don´t know what is the method to do that. My test of the service include the description of the job on a library inside the project, and in the remote server class, i have the method sched.ScheduleJob(jobDetail, trigger). But if i have only the Jobs.dll i don´t know to "associate" the trigger. – user2290341 Apr 17 '13 at 11:08
  • For example, if i have a list of dll of different jobs, jobA.dll, jobB.dll, etc..., inside the quartz service folder, i would like to know how can i say to the scheduler that i want to schedule for examplo the jobA with the triggerA. – user2290341 Apr 17 '13 at 11:25

2 Answers2

0

It seems that you need to load assemblies dynamically from .dll files, this can be done by reflection.

var assembly = Assembly.LoadFile(@"yourDllFilesPath.dll"); // this loads the assembly

Now, you can get this assembly classes

var type = assembly.GetType("Foo");  // a class Foo 

Now you have the type and what you still need is to create instances of this type, this can be done using Activator

object obj=Activator.CreateInstance(type);
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
0

I have the typical ScheduleJob method like this

    public void ScheduleJob(string jobName, string groupName, string triggerName, string cron, bool a)
    {            
        // Load the assembly
        AssemblyName assembly = AssemblyName.GetAssemblyName(@"C:\Program Files\Quartz.net\Jobs2.dll");
        System.Reflection.Assembly obj = System.Reflection.Assembly.Load(assembly);
        Type type = obj.GetType("MyJob2");

        if (!(type is IJob))
        {
            // Nothing, because the Job only have one method from IJob
        }

        // Contructor
        ConstructorInfo ctor = type.GetConstructor(new Type[] { });

        object loadedObject = ctor.Invoke(new object[] { obj });
        IJob importedObject = (IJob)loadedObject;                       


        JobKey jobKey = new JobKey(jobName, groupName);
        IJobDetail jobDetail = JobBuilder.Create(importedObject.GetType())
            .WithIdentity(jobName, groupName)
            .Build();
        TriggerKey tkey = new TriggerKey(triggerName, groupName);
        ITrigger cronTrigger = TriggerBuilder.Create()
            .WithIdentity(tkey)
            .ForJob(jobDetail)
            .WithCronSchedule(cron)
            .Build();
        AddJob(jobName, groupName, a);
        GetScheduler().ScheduleJob(cronTrigger);

But, when i use my client to execute this method, i recieve the following exception:

--Object reference not set to an instance of an object--

Thank you very mucho for your previous answers!