DNP Enterprise Server has a great extensibility mechanism known as "Task Event Handlers".
All methods of ES API (tasks) are characterized by 1) source name (e.g. "WEB_SITE", "FTP_ACCOUNT", etc.) and 2) task name (e.g. "ADD", "UPDATE", "DELETE", etc.).
3rd-party developers has an ability to intersect ES control flow in the beginning and the end of the task execution.
To create task event handler all that you need is VS.NET 2005 and DNP installation.
In this example we create a task handler that runs right after web site creation and do the following:
1. Copy zip file from the local folder on ES server to the web site root.
2. Unzip copied archive to the web site root and then delete it.
3. Create a new virtual directory called "Scripts".
4. Create a new MS Access ODBC DSN called "<website_name> DSN".
Project Template
Open Visual Studio 2005 and create a new C# (or VB.NET) "Class Library" project with "DNP.EnterpriseServer.Extensions" name.
Remove automatically added "Class1.cs" file from the project.
Add reference to the following libraries from DNP ES "bin" folder:
Add a new class file called "CreateWebSiteHandler.cs" and modify it as shown below:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using DotNetPanel.EnterpriseServer;
using DotNetPanel.Providers.Web;
using DotNetPanel.Providers.OS;
namespace DotNetPanel.EnterpriseServer.Extensions
{
public class CreateWebSiteHandler : TaskEventHandler
{
private const int FILE_BUFFER_LENGTH = 5000000; // ~5MB
public override void OnStart()
{
// nothing to do
}
public override void OnComplete()
{
// ... our logic will go here
}
}
}
Implementing Custom Logic
We have an ability to add custom logic on a) start task and b) end task
For "WEB_SITE" source and "ADD" task OnComplete() method is invoked when web site is already created.
Let's implement OnComplete() logic as described in the beginning of the article:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using DotNetPanel.EnterpriseServer;
using DotNetPanel.Providers.Web;
using DotNetPanel.Providers.OS;
namespace DotNetPanel.EnterpriseServer.Extensions
{
public class CreateWebSiteHandler : TaskEventHandler
{
private const int FILE_BUFFER_LENGTH = 5000000; // ~5MB
public override void OnStart()
{
// nothing to do
}
public override void OnComplete()
{
// ID of web site item that has been just created
int webSiteId = TaskManager.ItemId;
// get properties of created web site
WebSite site = WebServerController.GetWebSite(webSiteId);
// deploy local zip to web site root folder
string localZipPath = @"c:\ESFiles\WebSiteFiles.zip";
string remoteZipPath = Path.Combine(site.ContentPath, Path.GetFileName(localZipPath));
try
{
FileStream stream = new FileStream(localZipPath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[FILE_BUFFER_LENGTH];
int readBytes = 0;
do
{
// read package file
readBytes = stream.Read(buffer, 0, FILE_BUFFER_LENGTH);
if (readBytes < FILE_BUFFER_LENGTH)
// resize buffer
Array.Resize<byte>(ref buffer, readBytes);
// write remote backup file
FilesController.AppendFileBinaryChunk(site.PackageId, remoteZipPath, buffer);
}
while (readBytes == FILE_BUFFER_LENGTH);
stream.Close();
}
catch (Exception ex)
{
// log error
TaskManager.WriteError(ex, "Can not copy zip file to web site root folder");
}
// unzip files and delete zip
try
{
// unzip
FilesController.UnzipFiles(site.PackageId, new string[] { remoteZipPath });
// delete
FilesController.DeleteFiles(site.PackageId, new string[] { remoteZipPath });
}
catch (Exception ex)
{
// log error
TaskManager.WriteError(ex, "Can not unzip web site files");
}
// create virtual directory
try
{
string dirPath = Path.Combine(site.ContentPath, "scripts");
int vdirId = WebServerController.AddVirtualDirectory(site.Id, "scripts", dirPath);
}
catch (Exception ex)
{
// log error
TaskManager.WriteError(ex, "Can not create virtual directory");
}
// create Access DSN
try
{
SystemDSN dsn = new SystemDSN();
dsn.PackageId = site.PackageId;
dsn.Name = site.Name + " DSN";
dsn.Driver = "MsAccess";
dsn.DatabaseName = "Database.mdb";
dsn.DatabaseUser = "";
dsn.DatabasePassword = "";
OperatingSystemController.AddOdbcSource(dsn);
}
catch (Exception ex)
{
// log error
TaskManager.WriteError(ex, "Can not create ODBC DSN");
}
}
}
}
Registering Task Event Handler
To register task handler in ES open "TaskEventHandlers.config" file in the root of ES and add the following:
<?xml version="1.0" encoding="utf-8" ?>
<eventHandlers>
<task source="WEB_SITE" name="ADD">
<handler type="DotNetPanel.EnterpriseServer.Extensions.CreateWebSiteHandler, DNP.EnterpriseServer.Extensions" />
</task>
</eventHandlers>
Conclusion
The full sample code can be found in the attached archive.