Source reference

Originally posted at: Windows service for scheduling Forefront Identity Manager @ IS4U Blog



All FIM projects need to be able to schedule management agent to run automatically.

This article describes how we implemented a windows service to schedule Forefront Identity Manager synchronization service. You can find the source code and an installer at GitHub: FIM-Scheduler.


Basic implementation

We started off by implementing the management agent run functionality in C# by calling the WMI interface using the System.Management library.

ManagementScope ms = new ManagementScope(wmiNs);
string query = "Select * from MIIS_ManagementAgent";
SelectQuery sq = new SelectQuery(query);
using (ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, sq)) {
 foreach (ManagementObject obj in mos.Get()){
  using (ManagementObject ma = obj){
   object[] param = new  object[] {runProfile};
   ma.InvokeMethod("Execute", param);

So far so good, but we do not want to recompile our code every time we want to change our schedule. Therefore, we put our scheduling configuration in an XML file and let the program read our XML configuration. We defined two kinds of objects: runConfiguration and sequence. Both objects contain one or more step objects. There is no difference in the definition of runConfiguration and sequence, but only runConfigurations can be used as the starting point of a schedule. A step can be one of three types:

  1. A linear sequence: all steps are executed one after the other
  2. A parallel sequence: all steps are executed in parallel
  3. A management agent: the run profile defined in the Action attribute is run for the management agent
As you would expect, you can define sequences of sequences of sequences... And of course, you can use a sequence in more than one runConfiguration and/or other sequence.

We gave every type of step a different implementation, so the result has the following structure:



Windows Service

It is very easy to create a Windows Service with Visual Studio since it is a standard project template called, surprisingly, Windows Service.

All we needed to do was add an installer so that the executable can be installed as a service using sc.exe or installUtil.exe. There are some very good tutorials on how to create a Windows Service but here is the link used: Creating a Windows Service Application.



At the moment we have a program that is capable of running a given schedule and a Windows Service. The last issue we need to resolve is how we are going to perform the actual scheduling. Some Googling gives you some options, and we chose to use the Quartz .NET library.

Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems. Quartz.NET is a pure .NET library written in C# and is a port of very popular open source Java job scheduling framework, Quartz. This project owes very much to original Java project, its father James House and the project's contributors.

After some trial and error we succeeded in configuring a Quartz scheduler that reads the configuration from a file and started a job. Then it was just a matter of implementing this job so that it would start the steps in the run configuration specified by the Quartz configuration file.




See Also