Build and Debug a DNN Scheduled Task

Example with source code of how to build and debug a scheduled task for DNN in C#

Gus Beare

I have been building simple DNN scheduled tasks for a while and always found them a real pain to debug. You have to install the DLL in DNN, create the task in a clunky interface and then attach to IIS in Visual Studio. Once ready you hit "run now" in the DNN scheduler to fire off the task. There is usually a long wait before your breakpoints are hit and you can step through the code. It's not very efficient.

So, I decided to set something up that could run the code at the click of a button outside of DNN. This would make debugging so much quicker and easier. So I put the task code into a separate file and made it a static class.

The code in this class can be called from a button click in a separate web site project that is included in the solution. So, now I can build the schedule code and test it without worrying about debugging a DNN instance. I only need to install the task for a final test once it is ready.

For this example the idea was to truncate the Event Log which is a commonly required task. However, the event log is in a period of transition in DNN 7.x. So the code is merely an example that you can modify for your requirements.

I am a big fan of micro ORM's and in particular Simple.Data. So this example uses it to call a DNN proc called "PurgeEventLog". If you are not familiar with Simple.Data you will notice how fluent this code is.

var db = Database.OpenNamedConnection("SiteSQLServer"); // create a db connection to DNN database
db.PurgeEventLog();                                     // call the proc "PurgeEventLog"

To run the task in DNN create a new scheduled task and enable it. For the full class name and assembly use:

DNNScheduledTask.DotNetNuke.MyTask,DNNScheduledTask

Then add the compiled DLL (DNNScheduledTask.dll) to the DNN \bin folder

If you want to use Simple.Data as in this example you will also need to add the Simple.Data DLL's to you DNN \bin folder:

Simple.Data.dll
Simple.Data.Ado.dll
Simple.Data.SqlServer.dll

For more information and the source code please visit my Github repo:

DNN Scheduled Task Example Code