I am trying to create a set of C # classes that I plan to run in the future. The goal is to programmatically plan them through other parts of my code.
This is the current class I'm trying to call through COM.
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Win32.TaskScheduler;
namespace SchedulableTasks
{
[Guid("F5CAE94C-BCC7-4304-BEFB-FE1E5D56309A")]
public class TaskRegistry : ITaskHandler
{
private ITaskHandler _taskHandler;
public void Start(object pHandlerServices, string data)
{
var arguments = data.Split('|');
var taskTypeName = arguments.FirstOrDefault();
var taskArguments = arguments.Skip(1).FirstOrDefault();
var taskType = Type.GetType(taskTypeName);
_taskHandler = (ITaskHandler) Activator.CreateInstance(taskType);
_taskHandler.Start(pHandlerServices, taskArguments);
}
public void Stop(out int pRetCode)
{
var retCode = 1;
_taskHandler?.Stop(out retCode);
pRetCode = retCode;
}
public void Pause()
{
_taskHandler.Pause();
}
public void Resume()
{
_taskHandler.Resume();
}
}
}
This is how I try to schedule a task
using System;
using Microsoft.Win32.TaskScheduler;
using Newtonsoft.Json;
using Action = Microsoft.Win32.TaskScheduler.Action;
namespace MyProject.Service
{
public static class SchedulingService
{
public enum ScheduledTask
{
BillingQuery
}
private static readonly Guid RegistryGUI = new Guid("F5CAE94C-BCC7-4304-BEFB-FE1E5D56309A");
public static void ScheduleAction(string name, string description, Trigger trigger, Action action)
{
using (var taskService = new TaskService())
{
var task = taskService.NewTask();
task.RegistrationInfo.Description = description;
task.Triggers.Add(trigger);
task.Actions.Add(action);
taskService.RootFolder.RegisterTaskDefinition(name, task);
}
}
public static Action CreateCSharpAction(ScheduledTask task, object data)
{
var taskData = $"{task.ToString()}|{JsonConvert.SerializeObject(data)}";
return new ComHandlerAction(RegistryGUI, taskData);
}
}
}
Microsoft.Win32.TaskScheduler- version 2.7.2 this library
I can create a scheduled task without problems, but when I try to run it, I get it Class not registered (0x80040154).
In my .csproj, I register the assembly as a COM object. For the attribute, PlatformTargetI tried everything AnyCPU, x86and x64with the appropriate one regasm.exe.
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RegisterForComInterop>true</RegisterForComInterop>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
I am using the following post-build event to register .dll.
powershell C:\Windows\Microsoft.Net\Framework64\v4*\RegAsm.exe /codebase '$(ProjectDir)$(OutDir)SchedulableTasks.dll'
As far as I can tell, it is correctly registered.

Procmon.exe, , . , "NAME NOT FOUND" \TreatAs, \InprocHandler \InprocServer32\InprocServer32
