I picked this up from Dominick's post the other week but I'd not tried it myself until today.
I wanted to create my own little config file section that looks something like this;
<processLauncherConfig xmlns="urn:my-config">
<processes>
<process executable="c:\temp\foo.exe"
restart="true">
<arguments>
<argument value="one"/>
<argument value="two"/>
</arguments>
</process>
<process executable="c:\temp\foo.exe"
restart="false"/>
</processes>
</processLauncherConfig>
So, pretty simple thing to want to do but it's painful having to write the code for this whereas the Configuration Section Designer lets me do it graphically and does a nice job of generating artifacts for me.
This was my first experiment with it. I made a quick console application then added into it a new Configuration Section;
I then get a design surface, a bunch of files in my project;
and also some toolbox items to drag to the designer;
The basic structure that I want is;
Configuration Section (processsLauncherConfig) -> processes collection -> process -> arguments collection -> argument
First thing I do is click on the background of the designer and set the properties for the whole thing;
Then I drag a new configuration section to the designer and configure its properties;
Then I go and drag onto the designer surface configuration elements for my process and my argument and set their properties;
And then for an attribute like executable above I can go ahead and set properties like;
Now I want to build 2 collections - processes and arguments so I can drag a couple of "Configuration Element Collection" items from the toolbox. So, now I've got the following;
where the Processes collection contains "Item Type" of process.
Nearly there! I need to add a processes element to the configuration section and I need to set the type of the arguments element on the process configuration element to refer to my Arguments collection;
So now my diagram looks like this;
I can now build and test out what I've built. One of the things the tool spits out for is a sample config file;
so I can open that up and use it as the basis for my app.config;
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="processLauncherConfig"
type="ConsoleApplication10.ProcessLauncherConfig,ConsoleApplication10"/>
</configSections>
<processLauncherConfig xmlns="urn:processlauncher-com">
<processes>
<process executable="c:\temp\foo.exe"
restart="true"/>
<process executable="c:\temp\bar.exe"
restart="false">
<arguments>
<argument value="one"/>
<argument value="two"/>
</arguments>
</process>
</processes>
</processLauncherConfig>
</configuration>
and from there I can use the code that's been generated for me by the tool to read the config file back;
ProcessLauncherConfig config = (ProcessLauncherConfig)
ConfigurationManager.GetSection("processLauncherConfig");
Debug.Assert(config != null);
foreach (process p in config.processes)
{
Console.WriteLine("Exec [{0}], restart [{1}]", p.executable, p.restart);
if (p.arguments != null)
{
foreach (argument a in p.arguments)
{
Console.WriteLine("Argument [{0}]", a.value);
}
}
}
and I'm done ( Ok, some of the namings are a little wonky but I could refine that ).
This is a lot more fun that hand-cranking all this stuff yourself :-)
Posted
Wed, Feb 20 2008 7:14 AM
by
mtaulty