Trying to create a custom config for a custom block. Running into problems.

Topics: Building and extending application blocks, Enterprise Library Core, General discussion
Feb 28, 2013 at 3:42 PM
I am trying to create a new Enterprise Library (5.0) block for use within my company. It basically has a format similar to the one below in the config file:
<manager type="ManagerType..." name="TheManager>
   <jobs name="WebJob">
       <worker type="HTMLWorker" Name="Web Worker" url="http...."/>
       <scheduler type="FixedIntervalScheduler" name="Fixed Scheduler" interval="5min"/>
       <notifier type="EmailNotifier" name="Emailer" to="someone@somewhere.com"/>
   </jobs>
   <jobs name="FTPJob">
       <worker type="FTPWorker" Name="FTP Worker" url="ftp...."/>
       <scheduler type="WeekdayScheduler" name="Weekday Scheduler" dayOfWeek="Tuesday"/>
       <notifier type="FileNotifier" name="Logger" to="c:\logs\some.log"/>
   </jobs>
</manager>
All the "types" are defined as actual classes and dlls as in a normal config file.

The problem is I am able to get the Manager Class created fine, but when i tried to add the "jobs" section (I am adding to the config file piecemeal) to get it working, I first got an error that the name attribute could not be found. I removed the name from jobs in the config file and then got an error that the "jobs element could only occur once in that section". All the info I can find on custom config leads to a self built config file. Not one that works with Enterprise Library.

The Ent Lib labs are helpful, but use the
<group><add .... /> </group> 
paradigm. Not what i am looking for.

Any ideas?
Mar 1, 2013 at 5:39 AM
Edited Mar 1, 2013 at 5:40 AM
Are you looking at the Enterprise Library 5.0 - Extensibility Labs? In Lab 4 there is a GreetingsBlock example which would be useful.

For configuration, I would recommend a small change to the configuration to create a top level collection for jobs. This is the typical configuration pattern used and will make things easier. For example:
<manager type="ManagerType..." name="TheManager">
  <jobs> 
    <job name="WebJob">
       <worker type="HTMLWorker" Name="Web Worker" url="http...."/>
       <scheduler type="FixedIntervalScheduler" name="Fixed Scheduler" interval="5min"/>
       <notifier type="EmailNotifier" name="Emailer" to="someone@somewhere.com"/>
    </job>
    <job name="FTPJob">
       <worker type="FTPWorker" Name="FTP Worker" url="ftp...."/>
       <scheduler type="WeekdayScheduler" name="Weekday Scheduler" dayOfWeek="Tuesday"/>
       <notifier type="FileNotifier" name="Logger" to="c:\logs\some.log"/>
    </job>
 </jobs>
</manager>
In the configuration classes you can specify what the tag for "add" will be. It might look something like:
[System.Configuration.ConfigurationPropertyAttribute("jobs")]
[System.Configuration.ConfigurationCollectionAttribute(typeof(JobsElementCollection.JobElement), AddItemName = "job")]
public NameTypeConfigurationElementCollection<JobData, CustomJobData> Jobs
{
    get
    {
        return (NameTypeConfigurationElementCollection<JobData, CustomJobData>)base["jobs"];
    }
}
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to