Embedding StructureMap configuration into the App.config file

The System.Configuration namespace in the .Net framework provides a lot of functionality for caching and encrypting configuration.  To take advantage of this functionality StructureMap offers an option to embed configuration directly into the App.config file (web.config for web development).  Just add a section for StructureMap like this:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="StructureMap" type="StructureMap.Configuration.StructureMapConfigurationSection,StructureMap"/>

  </configSections>

 

  <StructureMap>

    <!– Put StructureMap configuration here –>

  </StructureMap>

</configuration>

Then you need to explicitly tell ObjectFactory to use this configuration:

            ObjectFactory.Initialize(x =>

            {

                // Tell StructureMap to look for configuration

                // from the App.config file

                // The default is false

                x.PullConfigurationFromAppConfig = true;

&            });

About Jeremy Miller

Jeremy is the Chief Software Architect at Dovetail Software, the coolest ISV in Austin. Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy is the author of the open source StructureMap tool for Dependency Injection with .Net, StoryTeller for supercharged acceptance testing in .Net, and one of the principal developers behind FubuMVC. Jeremy's thoughts on all things software can be found at The Shade Tree Developer at http://codebetter.com/jeremymiller.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.