This sample illustrates the use of a .NET Timer to trigger scheduled tasks. To leverage the benefits of NServiceBus retries and consistency of outgoing messages with the transport transaction, the tasks are implemented as regular message handlers. This also gives full traceability of the invoked tasks in platform tools like ServicePulse and ServiceInsight.
var interval = TimeSpan.FromSeconds(5);
var timer = new Timer(async state =>
{
try
{
await endpointInstance.SendLocal(new MyScheduledTask());
log.Info(nameof(MyScheduledTask) + " scheduled");
}
catch (Exception ex)
{
log.Error(nameof(MyScheduledTask) + " could not be scheduled", ex);
}
}, null, interval, interval);