简体   繁体   English

在WCF服务中添加汇编指令

[英]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. 我是使用Quartz.net的新手,我的问题是:我创建了一个WCF服务器,该服务器包括一个接口,该接口具有调度程序可以执行的操作,以及一个类,该类实现了在其中分配接口的功能。实例化调度程序和方法的构造函数。

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. 当我与客户端连接时,一切正常,但我需要以其他方式包括作业,包括在石英服务文件夹中动态包含.dll But I don't know how. 但是我不知道如何。

I have been searching for a solution, and I have found something in relation with System.Reflection ; 我一直在寻找解决方案,并且发现了一些与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. 似乎您需要从.dll文件动态加载程序集,这可以通过反射来完成。

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 现在您有了类型,您仍然需要创建此类型的实例,这可以使用Activator完成

object obj=Activator.CreateInstance(type);

I have the typical ScheduleJob method like this 我有像这样的典型ScheduleJob方法

    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! 非常感谢您之前的回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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