Introduction

The TaskPlugin library is set of classes, interfaces, enumerations and attributes that collectively support the ITaskPlugin interface.  The ITaskPlugin interface is a .NET interface created to demonstrate how to create a “Plug-in” architecture utilizing the Microsoft .NET framework.

 

The solution provided, Plug-in, includes 5 C# projects:

Technologies Used

 

Quick and Dirty – How to

Plug-in Host

Step 1: Include a reference to the TaskPluginInterface.dll in your solution.

Step 2: Get the folder location for the plug-ins.  I use \Plugins. Call the Utilities.GetPluginFolder() method.

Step 3:  Get a list of plug-ins.  Call Utilities.GetPlugins(folderName).

Step 4: Call the plug-ins Execute Method.

 

Plug-in

Option 1: Code from scratch.

Step 1: Include a reference to the TaskPluginInterface.dll in your solution.

Step 2: Create a class that inherits from ITaskPlugin.

public class RunCommand : ITaskPlugin

{

}

Step 3: If you are using Visual Studio 2005 you can use the Intellisense or SmartTag to create the stub implementation of ITaskPlugin.

Step 4: Place your code in the Execute method.

 

Option 2: Use the PluginShellPlugin class.

Step 1: Rename the class to what ever name you want.

Step 2: Change the resource file strings to accurately describe your plug-in.

Step 3: Change the code in the DoWork() method.

TaskPluginInterface Namespace

ITaskPlugin Interface

The ITaskPlugin interface defines the methods, events, and properties that the plug-ins must implement.

Methods

Enumeration Name

Description

Execute

Executes the task

 

Properties

Enumeration Name

Description

Author

The author of the plug-in

Description

The description of the plug-in

Extension

The file extension(s) that this plug-in handles.  This is currently not used.

Extension Description

A description of the extension(s) that this plug-in handles.  This is currently not used.

Name

The name of the plug-in.

Schedule

The schedule for the plug-in.  This was created for a future project, the pluggable task scheduler.

Version

The version of the plug-in.

 

Events

Enumeration Name

Description

EventEnd

This is used to notify the host about the ending of the Execute Method.

EventException

This is used to notify the host about any Exceptions that have occurred.

EventProcessing

This is used to notify the host about the processing of the Execute Method.

EventStart

This is used to notify the host about the starting of the Execute Method.

PluginAttribute Class

The PluginAttribute class defines the class attributes that can be applied to a class that is implementing the ITaskPlugin interface.

 

This class defines on Attribute which is PluginType.  The PluginType corresponds to the PluginType class.

 

Syntax:

[TaskPluginInterface.Plugin(PluginType.Executable)]

public class RunCommand : ITaskPlugin

{

}

Enumerations

ExecuteResult

This enumeration is used as the return value of Execute method for the ITaskPlugin interface.

 

Enumeration Name

Description

Cancelled

Indicates that the Execute method of the plug-in was cancelled.

Exception

Indicates that an exception was raised by the plug-in.

Failed

Indicates that the plug-in has failed.

Ok

Indicates that everything went OK with execute of the plug-in.

PluginType

This enumeration is used to indicate the type of plug-in that is being created.  Applying the PluginType attribute to a class does not have an affect the functionality of the class.  Its only purpose is to provide a classification of the plug-ins available for list boxes or separate functionality.

 

Enumeration Name

Description

Executable

Indicates the plug-in is used for launching executables.

Import

Indicates the plug-in is used for importing data.

SQL

Indicates the plug-in is used for processing SQL statements.

Unknown

Denotes the type of plug-in is not known.

 

PluginEventArgs Class

The PluginEventArgs class provides information back to the plug-in host.  This information is provided back to the plug-in host via one of the four events.

 

PluginEventArgs properties:

Name

Description

Cancel

Indicates if the operation should be canceled.

 

Message

A message returned from the plug-in.

 

Progress

Indicates the current progress of the plug-in. The Progress is returned as a struct which is defined in the Utilities class.

 

RaisedException

An exception raised from the plug-in.

 

ScheduleConfig Class

The ScheduleConfig class provides functions that will load the schedule configuration. 

 

Note: This was created for a future version of the application.

Utilities Class

This Utilities class provides helper functions to interact with and discover plug-ins. All of the provided methods are static so there is no need to create an instance of the Utilities class.

 

Utilities class methods:

Method Name

Description

FileContainsPlugins

Determines if the file contains at least one plug-in.  There is one overload which allows you specify a PluginType.

GetPlugin

Returns a plug-in of the specified class from the specified file.

GetPluginFileList

Gets a list files that contain plug-ins within the specified directory. There is one overload which allows you specify a PluginType.

GetPluginFolder

Returns the plug-in directory.

GetPlugins

Gets a List of plug-ins from the specified file. There is one overload which allows you specify a PluginType.

 

Included Plugins

FileCopy

The FileCopy plug-in provides the ability to copy files that fit a mask from one directory to another.

 

This plug-in reads the configuration file (FileCopyPlugin.Settings.xml) for all of the Directory nodes listed in the Directories element.

 

Sample:

<Directories>

<Directory FromDir="C:\Temp\" ToDir="d:\Temp\" FileMask="*.*" />

<Directory FromDir="C:\Temp\junk" ToDir="d:\Temp\junk" FileMask="*.txt" />

     <Directory FromDir="C:\Temp\junk" ToDir="d:\Temp\junk" FileMask="*.xyz" />

      </Directories>

The following configuration contains three copy commands.

PluginShell

The PluginShell plug-in provides a class that contains everything needed to implement the ITaskPluginInterface.

RunCommand

The RunCommand plug-in provides the ability to execute processes using the System.Diagnostics.Process namespace.

 

This plug-in reads the configuration file (RunCommandPlugin.Settings.xml) for Command elements listed in the Commands node.  Each command element has XmlChildNodes that correspond to parameters of the ProcessStartInfo struct of the Process object.

TaskPluginTest

The TaskPluginTest is sample application that demonstrates the use of the TaskPluginInterface library.  This application utilizes the TaskPluginInterface .Utilities to load the list of application plug-ins.  In addition, this application displays the plug-in meta-data, executes the plug-in and handles events from the plug-in.

Future Enhancements

History

Version

Date

 

What was done

1.0

4/21/07

 

Initial Release.

 

References