简体   繁体   中英

Adding assembly instruction in WCF service

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

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);

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!

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