New in 5.5: Provide your classes from App_Code
5.5 is out for some time and you sure deserve to know what is new. You sure know about the standard features you can see, but there are also some API improvements or things that haven't yet been in the Beta version. So here is the first one of them ...
Hi there,
There are certain tasks, that require you to do more than you actually want for quite a simple actions. One of them is defining your custom Scheduled task.
The scheduled task requires you to provide the assembly name and class name so it can load the class dynamically and execute it. This clearly results in you having to open Visual studio, add new class library, and implement the class in there, taking care about the references, and recompilation of the project if a hotfix is applied.
It is kind of too complicated if you ask me. That is why 5.5 offers better, alternative solution to it.
Providing class from App_Code
As the title indicates, in 5.5 there is a way how you can provide your custom classes (of any kind) from the App_Code folder. There isn't any DLL you could refer to for the App_Code of your web site project, but now there is a handler to take care of this. Scheduled task configuration then looks like this:
Task assembly name: App_Code (case sensitive)
Task class name: Custom.MyTask (any selector that you handle and provide the task class)
Now when we have the task defined, we can look how this is handled in the App_Code so you can provide the class object.
Open the file ~/App_Code/Global/CMS/CMSCustom.cs, you can see this code in there (I removed comments to make it shorter):
public static object GetCustomClass(string className)
{
switch (className)
{
case "Custom.MyTask":
return new MyTask();
}
return null;
}
public class MyTask : ITask
{
public string Execute(TaskInfo ti)
{
EventLogProvider ev = new EventLogProvider();
ev.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "MyTask", "Execute", null, "This task was executed from '~/App_Code/Global/CMS/CMSCustom.cs'.");
return null;
}
}
As you can see, there is a handler that you can implement to provide the custom objects. It is basically called with the class name you enter to the task which in our case is "Custom.MyTask". And since you know this targets the scheduled task, you provide a new object with the ITask interface just as you do in your DLL.
This particular sample task just logs it's execution to the Kentico
CMS event log so you can see that it really works. Yours can do just anything.
Where can it be used?
You can use this anywhere in the system where you provide the assembly name (such as Notification/Payment gateways, etc.), if you put in App_Code, it will just ask for the class through the handler instead of loading the DLL.
Advantages of this approach
The advantages are pretty clear, but just to summarize:
-
No need to recompile your DLLs after applying hotfix or upgrade
-
No need to recompile any DLL if you change the task code
-
Able to provide different task handlers based on the context of execution (e.g. for each web farm server / site / etc.)
And that is pretty much all for today, just as in previous post, this will also be used in our simple Chat module I will elaborate on later.
Enjoy!