Update Information

  • This article is deprecated and applies to JobScheduler releases up to 1.9
  • Starting from JobScheduler Release 1.10.5 native support for PowerShell is available
  • For detailed information see PowerShell Jobs FEATURE AVAILABILITY STARTING FROM RELEASE 1.10.5

Creating Job Scheduler API Jobs using C# and .NET

In addition to the programming languages natively supported by the JobScheduler API, JobScheduler can call COM Objects with access on its API.

This technique can be used to create C# jobs.

Setting up a project in Visual Studio

  • Create a new project of type "Class Library"
  • Choose .NET Framework 4 or higher
  • In the project properties go to Application->Assembly Information and check "Make assembly COM-Visible"
  • In the project properties go to Build and check "Register for COM interop"
  • In the project properties go to Signing and check "Sign the assembly"
  • Create a Class to implement the job
  • Run Visual Studio as Administrator to enable it to register the assembly on your machine

Implementing the job

JobScheduler will later try to find the com class as "namespace.classname". This combined name may not be longer than 40 characters.

Code for a simple c# job:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpSampleJob
{
    public class SampleJob
    {
        private dynamic SpoolerLog { get; set; }
        private dynamic Spooler { get; set; }
        private dynamic Job { get; set; }
        private dynamic Task { get; set; }
        public void spooler_set_context(dynamic context)
        {
            this.SpoolerLog = context.Log;
            this.Spooler = context.Spooler;
            this.Job = context.Job;
            this.Task = context.Task;
        }
        public bool spooler_init()
        {
            dynamic para = this.Task.Params;
            this.SpoolerLog.Info("This is spooler_init.");
            this.SpoolerLog.Info("TaskID: " + this.Task.Id);
            if (para != null)
            {
                String testParam = para.value("test");
                if (!String.IsNullOrEmpty(testParam))
                {
                    this.SpoolerLog.Info("testParam is: " + testParam);
                }
            }
            return true;
        }
        public bool spooler_open()
        {
            this.SpoolerLog.Info("This is spooler_open.");
            return true;
        }
        public void spooler_close()
        {
            this.SpoolerLog.Info("This is spooler_close.");
        }
        public void spooler_on_success()
        {
            this.SpoolerLog.Info("This is spooler_on_success.");
        }
        public void spooler_on_error()
        {
            this.SpoolerLog.Info("This is spooler_on_error.");
        }
        public bool spooler_process()
        {
            this.SpoolerLog.Info("This is spooler_process.");
            return (this.Task.order != null);
        }
        public void spooler_exit()
        {
            this.SpoolerLog.Info("This is spooler_exit.");
        }
    }
}

 

  • the dynamic keyword is used to avoid reflection code
  • the dynamic keyword requires .NET 4
  • in spooler_set_context the Job Scheduler objects are assigned to local properties

Please refer to these documentations how to use the API: [http://www.sos-berlin.com/doc/en/scheduler.doc/api/api.xml]

The example Visual Studio Project can be downloaded here: http://www.sos-berlin.com/download/scheduler/CSharpSampleJob.zip

Running the job

In JobScheduler the job for the above example is configured as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>

<job>
	<params>
		<param name="test" value"12345"/>
	</params>
	<script com_class="CSharpSampleJob.SampleJob"/>
</job>

Registering the assembly

In order for JobScheduler to find your COM Class, the assembly needs to be registered.

From an administrator command line:

 C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe CSharpSampleJob.dll /codebase

Debugging the job

In order to debug the job in visual studio

  • In the project properties->Debug choose "Start external program" and insert the path to scheduler.exe
  • Copy the command line arguments from the definition of the "SOS JobScheduler" Service and paste them into the "Command line arguments" of the project
  • Set the Scheduler home directory as the working directory in the debug options
  • edit your scheduler.xml and change the "process_classes" element to:

     

    <process_classes ignore"yes">