Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Configuration Section Designer

Blogs

Mike Taulty's Blog

Elsewhere

Archives

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;

image

I then get a design surface, a bunch of files in my project;

image

and also some toolbox items to drag to the designer;

image

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;

image

Then I drag a new configuration section to the designer and configure its properties;

image image

Then I go and drag onto the designer surface configuration elements for my process and my argument and set their properties;

image

And then for an attribute like executable above I can go ahead and set properties like;

image

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;

image

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;

image image

So now my diagram looks like this;

image

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;

image

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
Filed under:

Comments

Christopher Steen wrote Link Listing - February 20, 2008
on Thu, Feb 21 2008 5:18 AM
Announcements  CI Factory Version 1.0 [Via: jflowers ] ASP.NET  Digg.com like Login Control [Via: mikedopp...
Christopher Steen wrote Link Listing - February 20, 2008
on Thu, Feb 21 2008 5:19 AM
Link Listing - February 20, 2008
Jason Haley wrote Interesting Finds: February 21, 2008
on Thu, Feb 21 2008 7:02 AM
Mike Taulty's Blog wrote WPF and &amp;quot;Twistori&amp;quot;: Part 2
on Wed, Nov 19 2008 3:14 AM
Following on from the previous post, to get this work started I wanted to build a little library that...
ISharedMemos wrote Configuration Section Designer
on Sat, Feb 20 2010 4:07 AM

Configuration Section Designer